Issue
I am trying to use bar chart with all negative values. But it currently draws the bar from 0 to the negative value.
so in a bar graph with value -100 it draws bar from top of graph down, even if i change the y axis min/max.
I cant seem to find anything in documentation to do what i want.
I want the bars to draw from the min y axis/ x axis crossing up to the value like a regular looking bar graph when all values are positive.
Here is what i get that i don't want.
Solution
The requirement is to display bars in the style of positive data, but display labels in left axis in style of negetive data.
So, we can just pass Positive data to chart and surely now the bars are displayed in style of positive data. Also the label in left axis now is displayed in position data, we can use valueformater to force the labels in left axis display as negative.
For example the data is [(1, -10),(2, -20), (3,-30)], just pass [(1, 10),(2, 20), (3,30)] to the chart, and the bar will display as you like. Then set valueformater to leftaxis like this:
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return String.format("%2.2f", -1 * value);
}
});
use " -1 * value" change the value in left axis to nagetive.
Here is a sample from the demo:
Answered By - Cook Chen
Answer Checked By - Timothy Miller (JavaFixing Admin)