Android GraphView y轴未正确递增

时间:2018-04-22 21:56:49

标签: java android android-studio android-graphview

我目前正在开发一个使用GraphView库创建BarGraphSeries的简单Android应用,并且在我绘制数据时发现了一个奇怪的事情

我返回了存储在我的本地SQLite数据库中的数据的计数,每当我将这些值传递到图表y轴时,y轴递增0.5但是如果我要对值进行硬编码(为了测试),y轴增加1(所需功能)。

给我提出问题的代码:

    // This is how I've declared my variables... I will then go onto call my db
    // and populate each variable with a column count.

    private static int GREAT_COUNT = 0;
    private static int GOOD_COUNT = 0;
    private static int MEH_COUNT = 0;
    private static int FUGLY_COUNT = 0;
    private static int AWFUL_COUNT = 0;

    GraphView graph = findViewById(R.id.graph);
    BarGraphSeries<DataPoint> series = new BarGraphSeries<>(new DataPoint[] {
            new DataPoint(0, GREAT_COUNT),
            new DataPoint(1, GOOD_COUNT),
            new DataPoint(2, MEH_COUNT),
            new DataPoint(3, FUGLY_COUNT),
            new DataPoint(4, AWFUL_COUNT)
    });

通过将变量传递给DataPoint生成的图表:

Undesired graph

硬编码值(图表工作正常):

    GraphView graph = findViewById(R.id.graph);
    BarGraphSeries<DataPoint> series = new BarGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 2),
            new DataPoint(1, 3),
            new DataPoint(2, 7),
            new DataPoint(3, 10),
            new DataPoint(4, 11)
    });

根据硬编码值生成的图表:

Desired graph

我是Android开发的新手,我一直这样做是为了帮助朋友完成任务。我在int变量的声明中做了什么错误,这就是为什么它导致它将y轴增加0.5,而硬编码值看起来像我预期的那样增加号。

1 个答案:

答案 0 :(得分:0)

网格线的y值是根据Y轴的最小值和最大值自动生成的,图形值为int并不重要。 Y轴以2开头,以4结尾,有四个“部分”。因此线条设置为2.5,3.0和3.5。

  • 您可以使用setMinY(0)将零设置为Y轴的起点,然后您将在1,2和3上设置网格线。
  • 您可以使用setNumVerticalLabels(1)只有一个网格线,在您的情况下它将是3。但请注意,您的最大Y值为5,然后网格线将为3.5

也许有一种方法允许仅在整数值上设置网格线但我没有找到它。