带标题的垂直条形图

时间:2017-12-23 10:36:50

标签: android graph

我正在使用MPChart图库,我正在显示条形图,以下代码我正在显示图表,但我想要的标题如下:" Jan"," Feb"与他们等。如何制作带有标题值的图表?

 List<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(0f, 30f));
        entries.add(new BarEntry(1f, 80f));
        entries.add(new BarEntry(2f, 60f));
        entries.add(new BarEntry(3f, 50f));

        BarDataSet set = new BarDataSet(entries, "BarDataSet");
        BarData data = new BarData(set);
        data.setBarWidth(0.9f); // set custom bar width
        chart.setData(data);
        chart.setFitBars(true); // make the x-axis fit exactly all bars
        chart.invalidate(); // refresh

我现在正在使用这个库:

implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

2 个答案:

答案 0 :(得分:1)

首先这样做:

 ArrayList<String> xVals = new ArrayList<String>();
 xVals.add("Jan");
 xVals.add("Feb");
 xVals.add("Mar");
 xVals.add("Apr");
 xVals.add("May");
 xVals.add("Jun");

然后按如下方式自定义您的Xaxis:

 XAxis xAxis = chart.getXAxis();
 xAxis.setGranularity(1f);
 xAxis.setGranularityEnabled(true);
 xAxis.setCenterAxisLabels(true);
 xAxis.setDrawGridLines(false);
 xAxis.setAxisMaximum(6);
 xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

 // Below line will add labels on your xAxis:

 xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));

答案 1 :(得分:-1)

使用此:

        BarDataSet set = new BarDataSet(entries, "BarDataSet");
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set);
        BarData data = new BarData(dataSets);
        data.setBarWidth(0.9f); // set custom bar width
        chart.setData(data);
        chart.setFitBars(true); // make the x-axis fit exactly all bars
        chart.invalidate(); // refres