如何在Graphview(Android)中准确地在数据点下设置数据标签,并使用数据点的数据值?

问题描述 投票:0回答:1

我正在使用Graphview与数据库中的DataPoint[]。DataPoint[]的数据点如下。

  1. 1.582381477E120.0](日期是2020-02-22 14:24:37)。
  2. [1.58238148E121.0](日期是2020-02-22 14:24:40)。
  3. [1.582381489E122.0](日期是2020-02-22 14:24:49)。
  4. [1.582381491E123.0](日期是2020-02-22 14:24:51)。
  5. [1.582381497E124.0](日期是2020-02-22 14:24:57)。
  6. [1.582381511E125.0] (日期是2020-02-22 14:25:11)

我的GraphView使用了这段代码。

    DataPoint[] dataPoints = getSucccessData(exercise.getExersiseNumber());
    LineGraphSeries<DataPoint> mLGS = new LineGraphSeries<>(dataPoints);
    mLGS.setDrawDataPoints(true);
    graphView = findViewById(R.id.gvGraphView);

    simpleDateFormat = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");

    graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
        @Override
        public String formatLabel(double value, boolean isValueX) {

            if(isValueX){
                return simpleDateFormat.format(new Date((long)value));
            }
            else {
                return super.formatLabel(value, isValueX);
            }
        }
    });
        graphView.removeAllSeries();
        graphView.addSeries(mLGS);
        graphView.getViewport().setMinY(0);
        graphView.getViewport().setMaxY(5);
        graphView.getViewport().setYAxisBoundsManual(true);
        graphView.getViewport().setMinX(dataPoints[0].getX());
        graphView.getViewport().setMaxX(dataPoints[dataPoints.length - 1].getX());
        graphView.getGridLabelRenderer().setNumHorizontalLabels(dataPoints.length -1);
        graphView.getViewport().setXAxisBoundsManual(true);
        graphView.getGridLabelRenderer().setHumanRounding(false, true);
        graphView.getGridLabelRenderer().setHorizontalLabelsAngle(90);
        graphView.getViewport().setScrollableY(true);
        graphView.getViewport().setScrollableY(true);
        graphView.getViewport().setScalable(true);
        graphView.getViewport().scrollToEnd();
        graphView.getViewport().setScrollable(true);

结果就是这个图形。

Graphview

正如你所看到的,X轴上除了第一个和最后一个以外,其他的都不是我的数据点的x值,也不是直接在一个特定的数据点下。它们是在我的数据点的第一个和最后一个x值之间的范围内的数据值,它们之间的距离是不一样的。时间越长,x值之间的距离就越大。

我想用我的数据点的精确x值作为X轴标签,而不是其他X轴标签,并且无论日期之间的时间有多长,数据点之间的距离都应该是相同的。

我如何才能做到这一点?

java android android-graphview
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.