如何在Horizo​​ntalBarChart中显示右侧的标签和左侧的值?

时间:2017-04-28 19:26:08

标签: android charts bar-chart mpandroidchart

使用MPAndroidChart 3.0.2绘制水平条形图。值显示在条形图的右侧。我可以使用setValueFormatter并使用IAxisValueFormatter接口在右侧显示标签。但是现在不显示这些值。

{
    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;
   }
}

要求是在右侧显示标签字符串,在左侧显示与每个条形对应的数字。 我确实检查了一些堆栈溢出并谷歌它但是找不到任何有用的东西。

我得到了这个 enter image description here

但这是我的要求enter image description here的一个例子 要感谢一些帮助。 谢谢你的时间

1 个答案:

答案 0 :(得分:8)

几天前我实施了类似的东西。这是代码......

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

enter image description here

希望这有帮助。