0

I would like to show the values on the left axis in a BarChart and CombinedChart on periods of 100. For example, if I have three values, 10, 120, 250 on the left axis of the table it should have as a reference the values 0, 100, 200 and 300. Instead of this, I am getting values as default with a period of 40. I would want to change this intermediate range.

I know how to set the range value of min-max values but not how to set the range for intermediate values.

Is it possible to modify that intermediate range of values on left axis of the table?

Thanks in advance!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

2 Answers2

1

Not sure whether I correctly understood the intention of your question, but if you want your axis to display values in multiples of 100 you could try something like this:

YAxis leftAxis = mBarChart.getAxisLeft();
leftAxis.setGranularity(100f);
leftAxis.setGranularityEnabled(true);

See the documentation for setGranularity and setGranularityEnabled for more information about granularity and also check out this answer.

Community
  • 1
  • 1
TR4Android
  • 3,200
  • 16
  • 21
0

Yes, it's possible to change intermediate range by using

mBarChart.getAxisLeft().setGranularity(100f);
mBarChart.getAxisLeft().setGranularityEnabled(true);

but If you want to set granularity dynamically, then implement IAxisValueFormatter and compare the return value to get the difference and set Granularity to that difference.

        private float yAxisScaleDifference = -1;
        private boolean granularitySet = false;
        //[10,120,250]
        mBarChart.getAxisLeft().setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float v, AxisBase axisBase) {
            if(!granularitySet) {

                if(yAxisScaleDifference == -1) {
                    yAxisScaleDifference = v;  //10
                }
                else {
                    float diff = v - yAxisScaleDifference;  //120 - 10 = 110
                    if(diff >= 1000) {
                        yAxisLeft.setGranularity(1000f);  
                    }
                    else if(diff >= 100) {
                        yAxisLeft.setGranularity(100f);  //set to 100
                    }
                    else if(diff >= 1f) {
                        yAxisLeft.setGranularity(1f);
                    }
                    granularitySet =true;
                }
            }
            return val;
        }
    });

Another Example:

    say Y-Axis returns [1200,3400,8000,9000....]
      first time: 1200
      second time: 3400 - 1200 = 2200
      set to 1000

If the difference is not uniform you have to use array to store the differences and take the average to get the right granularity.

Extremis II
  • 5,185
  • 2
  • 19
  • 29