How to show labels on right and values to left side in HorizontalBarChart?

后端 未结 1 1216
情深已故
情深已故 2021-01-07 07:03

Drawing a horizontal bar chart using MPAndroidChart 3.0.2. the values are shown on the right of the bars. I could use setValueFormatter and use IAxisValueFormatter interface

相关标签:
1条回答
  • I implemented something similar just a few days back. Here's the code...

    public class MainActivity extends AppCompatActivity {
    
        protected HorizontalBarChart mChart;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayList<String> labels = new ArrayList<>();
            labels.add("January");
            labels.add("February");
            labels.add("March");
            labels.add("April");
            labels.add("May");
            labels.add("June");
    
            mChart = (HorizontalBarChart) findViewById(R.id.barChart);
            mChart.setDrawBarShadow(false);
            mChart.setDrawValueAboveBar(true);
            mChart.getDescription().setEnabled(false);
            mChart.setPinchZoom(false);
            mChart.setDrawGridBackground(false);
    
    
            XAxis xl = mChart.getXAxis();
            xl.setPosition(XAxis.XAxisPosition.BOTTOM);
            xl.setDrawAxisLine(true);
            xl.setDrawGridLines(false);
            CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(labels);
            xl.setValueFormatter(xaxisFormatter);
            xl.setGranularity(1);
    
            YAxis yl = mChart.getAxisLeft();
            yl.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
            yl.setDrawGridLines(false);
            yl.setEnabled(false);
            yl.setAxisMinimum(0f);
    
            YAxis yr = mChart.getAxisRight();
            yr.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
            yr.setDrawGridLines(false);
            yr.setAxisMinimum(0f);
    
            ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
            for (int i = 0; i < 6; i++) {
                yVals1.add(new BarEntry(i, (i+1)*10));
            }
    
            BarDataSet set1;
            set1 = new BarDataSet(yVals1, "DataSet 1");
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(.9f);
            mChart.setData(data);
            mChart.getLegend().setEnabled(false);
        }
    
        public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
    
            ArrayList<String> mValues;
    
            public CategoryBarChartXaxisFormatter(ArrayList<String> values) {
                this.mValues = values;
            }
    
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
    
                int val = (int) value;
                String label = "";
                if (val >= 0 && val < mValues.size()) {
                    label = mValues.get(val);
                } else {
                    label = "";
                }
                return label;
            }
        }
    
    }
    

    RESULT

    Hope this helps.

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