简体   繁体   English

Android GraphView y轴未正确递增

[英]Android GraphView y-axis not incrementing correctly

I'm currently developing a simple Android app that uses the GraphView library to create a BarGraphSeries and I've noticed a strange occurrence when I'm plotting my data. 我目前正在开发一个简单的Android应用程序,该应用程序使用GraphView库创建BarGraphSeries并且在绘制数据时发现了奇怪的情况。

I'm returning a count for data that's stored in my local SQLite database and whenever I pass these values to the graphs y-axis the y-axis is incrementing by 0.5 but if I were to hard code the values (for the sake of testing) the y-axis is incrementing by 1 (desired functionality). 我正在返回存储在本地SQLite数据库中的数据的计数,每当我将这些值传递到图形y轴时,y轴就会增加0.5,但是如果我要对这些值进行硬编码(出于测试目的) ),y轴以1(所需功能)递增。

Code that's giving me issues: 给我一些问题的代码:

    // 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)
    });

Graph generated from passing in a variable to the DataPoint: 通过将变量传递到DataPoint生成的图:

不需要的图

Hard coded values (chart works fine): 硬编码值(图表工作正常):

    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)
    });

Graph generated from hard coded values: 从硬编码值生成的图形:

所需图

I'm new to Android development and I've been doing this to help out a friend for an assignment. 我是Android开发的新手,我一直在这样做是为了帮助一位朋友做作业。 Am I doing anything wrong in the declaration of my int variables and this is why it's causing it to increase the y-axis by 0.5 whereas the hard coded values appear to increment as I would have expected in whole numbers. 我在声明int变量时做错什么了int ,这就是为什么它导致y轴增加0.5的原因,而硬编码值似乎按我的预期增加了。

The y-values of the grid lines are generated automatically depending on minimal and maximal values of the Y-axis and it doesn't matter that you graph values are int . 网格线的y值是根据Y轴的最小值和最大值自动生成的,而绘制图形的值int无关紧要。 You Y-axis starts with 2 and ends with 4 and there are four "sections". 您的Y轴以2开始,以4结束,并且有四个“部分”。 So the lines are set on 2.5, 3.0 and 3.5. 因此,将行设置为2.5、3.0和3.5。

  • You can use setMinY(0) to set zero as starting point for the Y-axis, then you will have grid lines on 1, 2 and 3. 您可以使用setMinY(0)将零设置为Y轴的起点,然后在1、2和3上有网格线。
  • You can use setNumVerticalLabels(1) to have only one grid line, it would be on 3 in your case. 您可以使用setNumVerticalLabels(1)仅具有一条网格线,在您的情况下为3。 But note that is your max Y values is 5 , then the grid line would be on 3.5 但请注意,最大Y值为5,则网格线将位于3.5上

Maybe there is a method which allows to set grid lines only on integer values but I didn't find it. 也许有一种方法可以只在整数值上设置网格线,但我没有找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM