带有日期标签和可滚动的GraphView

时间:2017-06-05 11:45:55

标签: android graph android-graphview

我正在尝试使用GraphView库使X轴具有数据标签,Y轴具有数字。问题是当使用日期作为标签时,我无法使X轴可滚动。 这是代码:

    Calendar calendar = Calendar.getInstance();
    DataPoint[] points = new DataPoint[31];
    Random rand = new Random();
    calendar.add(Calendar.DATE, 0);
    Date startDate = calendar.getTime();
    points[0] = new DataPoint(startDate, rand.nextInt(9) + 1);
    List<String> dataPointsString = new ArrayList<>();
    dataPointsString.add(calendar.getTime().toString());
    calendar.add(Calendar.DATE, -20);
    Date endDate = calendar.getTime();
    calendar.add(Calendar.DATE, +20);
    for (int i = 1; i <= 30; i++) {
        calendar.add(Calendar.DATE, -1);
        points[i] = new DataPoint(calendar.getTime(), rand.nextInt(9) + 1);
        dataPointsString.add(calendar.getTime().toString());
    }

    GraphView graph = (GraphView) view.findViewById(R.id.graph);

    // you can directly pass Date objects to DataPoint-Constructor
    // this will convert the Date to double via Date#getTime()
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(points);

    // set date label formatter
    graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity()));
    graph.getGridLabelRenderer().setNumHorizontalLabels(6);

    graph.getViewport().setYAxisBoundsManual(true);
    graph.getViewport().setMinY(0);
    graph.getViewport().setMaxY(10);

    graph.getViewport().setXAxisBoundsManual(true);

    // as we use dates as labels, the human rounding to nice readable numbers
    // is not necessary
    graph.getGridLabelRenderer().setHumanRounding(false);

    graph.getViewport().setScalable(true);

    graph.addSeries(series);

知道这是错的吗?

1 个答案:

答案 0 :(得分:0)

     graph.getGridLabelRenderer().setNumHorizontalLabels(11);
you setted 6 change it to 10 or 11 accordingly and try below...

    // activate horizontal zooming and scrolling
graphview.getViewport().setScalable(true);

// activate horizontal scrolling
graphview.getViewport().setScrollable(true);

// activate horizontal and vertical zooming and scrolling
graphview.getViewport().setScalableY(true);

// activate vertical scrolling
graphview.getViewport().setScrollableY(true);