mpandroidchart - How can I avoid the repeated values in Y-Axis?

前端 未结 6 549
情话喂你
情话喂你 2020-12-10 01:31

I want to avoid the repeated values and the -0 values in Y-Axis, avoiding the image situation.

I have these ideas to solve this, but any solution:

6条回答
  •  甜味超标
    2020-12-10 01:51

    You can set granularity to your required value to prevent the repetition when zoomed.

    mBarChart.getAxisLeft().setGranularity(1f);  // y-axis scale will be 0,1,2,3,4....
    mBarChart.getAxisLeft().setGranularityEnabled(true);
    

    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;
        //say the values returned by IAxisFormatter is in hundreds: 100, 200, 300...
        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;  //200 - 100 = 100
                    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
      granularity 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.

提交回复
热议问题