Force BarChart Y axis labels to be integers?

后端 未结 8 756
滥情空心
滥情空心 2021-02-05 11:47

I\'ve created a BarChart using MPAndroidChart and I\'m entering the data dynamically. This means that I need my Y axis to also be determined dynamically. All of my data is repre

相关标签:
8条回答
  • 2021-02-05 12:28

    Pretty simple. All you need is two things:

    1. Axis value formatter

      mChart.getAxisLeft().setValueFormatter(new ValueFormatter() {
          @Override
          public String getFormattedValue(float value) {
              return String.valueOf((int) Math.floor(value));
          }
      });
      
    2. Axis label count

      int max = findMaxYValue(yourdata); // figure out the max value in your dataset
      mChart.getAxisLeft().setLabelCount(max);
      
    0 讨论(0)
  • 2021-02-05 12:29

    My Solution:

    mChart.getAxisLeft().setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return String.valueOf((int) Math.floor(value));
        }
    });
    
    int max = (int) mChart.getData().getYMax(); 
    mChart.getAxisLeft().setLabelCount(max);
    
    0 讨论(0)
提交回复
热议问题