如何在 Horizo​​ntalBarChart 中显示右侧的标签和左侧的值? [英] How to show labels on right and values to left side in HorizontalBarChart?

查看:19
本文介绍了如何在 Horizo​​ntalBarChart 中显示右侧的标签和左侧的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

但这是我的要求的一个例子请欣赏一些帮助.感谢您的时间

解决方案

几天前我实施了类似的方案.这是代码...

公共类 MainActivity 扩展 AppCompatActivity {受保护的 Horizo​​ntalBarChart mChart;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ArrayList标签 = 新的 ArrayList<>();label.add("一月");label.add("二月");label.add("三月");label.add("四月");label.add("五月");label.add("六月");mChart = (Horizo​​ntalBarChart) 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);ArrayListyVals1 = new ArrayList();for (int i = 0; i <6; i++) {yVals1.add(new BarEntry(i, (i+1)*10));}BarDataSet set1;set1 = new BarDataSet(yVals1, "DataSet 1");ArrayListdataSets = new ArrayList();dataSets.add(set1);BarData 数据 = new BarData(dataSets);data.setValueTextSize(10f);data.setBarWidth(.9f);mChart.setData(数据);mChart.getLegend().setEnabled(false);}公共类 CategoryBarChartXaxisFormatter 实现 IAxisValueFormatter {ArrayList值;public CategoryBarChartXaxisFormatter(ArrayList values) {this.mValues = 值;}@覆盖公共字符串 getFormattedValue(浮点值,AxisBase 轴){int val = (int) 值;字符串标签 = "";如果 (val >= 0 && val < mValues.size()) {标签 = mValues.get(val);} 别的 {标签 = "";}退货标签;}}}

结果

希望这会有所帮助.

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 to display the labels on the right. But the values are not displayed now.

{
    HorizontalBarChart barChart = (HorizontalBarChart) itemView.findViewById(R.id.barChart);

            BarData data = new BarData();

        ArrayList<BarEntry> valueSet1 = new ArrayList<>();

        ArrayList<String> labels = new ArrayList<>();
        labels.add("January");
        labels.add("February");
        labels.add("March");
        labels.add("April");
        labels.add("May");
        labels.add("June");
        ArrayList<String> ylabels = new ArrayList<>();
        int dataCount=0;
        for (int i=0;i<6;++i) {
            BarEntry entry = new BarEntry(dataCount,(i+1)*2);
            valueSet1.add(entry);
            ylabels.add(" "+i);
            dataCount++;
        }
        List<IBarDataSet> dataSets = new ArrayList<>();
        BarDataSet bds = new BarDataSet(valueSet1, " ");
        bds.setColors(ColorTemplate.MATERIAL_COLORS);
        String[] xAxisLabels = labels.toArray(new String[0]);
        String[] yAxisLabels = ylabels.toArray(new String[0]);
        bds.setStackLabels(xAxisLabels);
        dataSets.add(bds);
        data.addDataSet(bds);
        data.setDrawValues(true);
        data.setBarWidth(0.4f);

        XAxis xaxis = barChart.getXAxis();
        xaxis.setDrawGridLines(false);
        xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xaxis.setGranularityEnabled(true);
        xaxis.setGranularity(1);
        xaxis.setDrawLabels(true);
        xaxis.setLabelCount(dataCount+1);
            xaxis.setXOffset(10);
            xaxis.setDrawAxisLine(false);
            CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(xAxisLabels);
            xaxis.setValueFormatter(xaxisFormatter);

            YAxis yAxisLeft = barChart.getAxisLeft();
            yAxisLeft.setEnabled(false);

            YAxis yAxisRight = barChart.getAxisRight();
           yAxisRight.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
            yAxisRight.setDrawGridLines(false);
            yAxisRight.setDrawAxisLine(false);

            Legend legend = barChart.getLegend();
            legend.setEnabled(false);

            barChart.setFitBars(true);
            barChart.setData(data);
            barChart.setDescription(null);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {

   private String[] mValues;

   public CategoryBarChartXaxisFormatter(String[] values) {
       this.mValues = values;
   }

   @Override
   public String getFormattedValue(float value, AxisBase axis) {

       int val = (int)value;
       String label="";
       if(val>=0 && val<mValues.length) {
           label= mValues[val];
       }
       else {
           label= "";
       }
       return label;
   }
}

requirement is to display label strings on the right and a number on the left corresponding to each bar. I did check some stack overflow and google it but didn't find anything that works so far.

i get this

but this is an example of my requirement Do appreciate some help. Thanks for your time

解决方案

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.

这篇关于如何在 Horizo​​ntalBarChart 中显示右侧的标签和左侧的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆