MpAndroidChart HorizontalBarChart Customize label

前端 未结 1 1407
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 04:31

I want to add my bar values to the left of the bar and the bar labels to the right of the bar.

Below is the code that initiliz

相关标签:
1条回答
  • 2021-01-29 05:10

    Yes, this is definitely possible using the ValueFormatter and AxisValueFormatter interfaces.

    1. Create a formatter that adds the labels to the right of your bars:

      public class LabelFormatter implements ValueFormatter {
          @Override
          public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
              // assuming that getHeader() returned the label
              return (String) entry.getData();
          }
      }
      
    2. Create an axis formatter that adds the values to the left of your bars:

      public class BarValueFormatter implements AxisValueFormatter {
          private final DataSet mData;
      
          public BarValueFormatter(DataSet data) {
              mData = data;
          }
      
          @Override
          public String getFormattedValue(float value, AxisBase axis) {
              return String.valueOf((int) mData.getEntryForXPos(value, DataSet.Rounding.CLOSEST).getY());
          }
      }
      
    3. Set your formatters to the dataset and axis:

      set1.setValueFormatter(new LabelFormatter());
      mChart.getXAxis().setValueFormatter(new BarValueFormatter(set1));
      

    Note that you need to remove the call to xl.setDrawLabels(false); for the x-axis, so the values are properly rendered.

    0 讨论(0)
提交回复
热议问题