简体   繁体   中英

Android GraphView Bar Graph Heights

I have been trying to create a bar graph displaying food item stock levels decreasing over time.

I used the sample code from here:

I changed the code slightly in GraphViewDemo to accommodate 4 values on the Y axis:

("great", "good", "ok", and "bad") and 4 values on the X axis

("today", "tomorrow", "2 days", "3 days")

and want to show how the stock levels decrease over time. Here's the code that should display the bars increasing from bad to great:

    float[] values = new float[] { 1.0f,1.5f, 2.0f, 2.5f };
    String[] verlabels = new String[] { "great", "good", "ok", "bad"};
    String[] horlabels = new String[] { "today", "tomorrow", "2 days", "3 days" };
    GraphView graphView = new GraphView(this, values, "Stock Levels",horlabels, verlabels, GraphView.BAR);
    setContentView(graphView);

This is fine with the numbers 1.0f, 1.5f, 2.0f, 2.5f. The problem is, if I change the first float to say 2.0 (expecting the first bar to change to the height of "good") all the bars change.

In this case the view would change to the first and third bars height being between "ok" and "good" and the second bar being the height of "bad".

It may be something to do with pixel conversion in the GraphView class near line 94, but the code is not very well commented and I am finding it hard to understand everything.

The issue seemed to be with this line in the GraphView class:

canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);

So I found the values of the 4 heights the bars could be on the chart ("great", "good", "ok", "bad") and created a switch statement that would set a variable. The switch would take the values 1.0, 1.5, 2.0, and 2.5 and relate it to their corresponding bar heights.

I then replaced the height with the variable, instead of the following calculation "(border - h) + graphheight". This way there were only 4 set heights the bar could be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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