如何在mpandroidchart中绘制Xaxis? (折线图,条形图)

时间:2020-10-25 07:10:17

标签: android charts mpandroidchart

我想在条形图,折线图中设置Xaxis
首先,我的图表图片。
我要在条形图中对4个xAxis进行性别,在折线图中对6个xAxis进行性别。

enter image description here

enter image description here

我的代码在这里

Barchart

public void ageRateChart(View view){
    BarChart ageRateChart = view.findViewById(R.id.ageRateChart);
    ageRateChart.setExtraOffsets(5.0f, 10.0f, 5.0f, 5.0f);

    ArrayList ageValues = new ArrayList(); //label★★
    ageValues.add(new BarEntry(0.0f, 0.0f, "10대"));
    ageValues.add(new BarEntry(1.0F, 4.0F, "20대"));
    ageValues.add(new BarEntry(2.0F, 13.0F, "30대"));
    ageValues.add(new BarEntry(3.0F, 38.0F, "40대"));
    ageValues.add(new BarEntry(4.0F, 33.0F, "50대"));
    ageValues.add(new BarEntry(5.0F, 12.0F, "60대 이상"));

    XAxis xAxis = ageRateChart.getXAxis();
    xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return ageValues.get((int)value);
            }
        });

    ageRateChart.animateY(4000, Easing.EaseInOutCubic);

    final int[] ageColors = {
        Color.parseColor("#d4d4d4"),
        Color.parseColor("#d4d4d4"),
        Color.parseColor("#d4d4d4"),
        Color.parseColor("#cf16f2"),
        Color.parseColor("#d4d4d4"),
        Color.parseColor("#d4d4d4"),
    };
    ArrayList<Integer> colors = new ArrayList<Integer>();
    for(int c : ageColors)
        colors.add(c);

    BarDataSet ageDataSet = new BarDataSet(ageValues, " 연령대");
    ageDataSet.setColors(colors);

    BarData ageData = new BarData(ageDataSet);
    ageData.setBarWidth(1f);
    ageRateChart.setData(ageData);
    ageRateChart.setFitBars(true);
    ageRateChart.invalidate();
}

折线图

public void timeLineChart(View view){
    ArrayList timeValues = new ArrayList();
    Entry timeLine1 = new Entry(0.0F, 140.0F);
    timeValues.add(timeLine1);
    Entry timeLine2 = new Entry(1.0F, 2000.0F);
    timeValues.add(timeLine2);
    Entry timeLine3 = new Entry(2.0F, 800.0F);
    timeValues.add(timeLine3);
    Entry timeLine4 = new Entry(3.0F, 500.0F);
    timeValues.add(timeLine4);

    LineChart timeLineChart = view.findViewById(R.id.timeLineChart);

    LineDataSet setTime = new LineDataSet(timeValues, "시간대별 댓글");
    setTime.setAxisDependency(YAxis.AxisDependency.LEFT);
    setTime.setColor(Color.parseColor("#c39797"));

    ArrayList timeDataset = new ArrayList();
    timeDataset.add(setTime);

    final ArrayList<String> xLabel = new ArrayList<>(); //label★★
    xLabel.add("0~6시");
    xLabel.add("6~12시");
    xLabel.add("12~18시");
    xLabel.add("18~0시");

    XAxis xAxis = timeLineChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setValueFormatter(new IndexAxisValueFormatter(){
            @Override
            public String getFormattedValue(float value, AxisBase axis){
                return xLabel.get((int)value);
            }
        });
    xAxis.setGranularity(1f);

    LineData timeData = new LineData((List)timeDataset);
    timeLineChart.setData(timeData);
    timeLineChart.invalidate();
}

但是,不建议使用Valueformatter。
如何设置Xaxis? mpandroidchart版本为3.1.0,有关此函数的任何信息均已过时。
非常感谢。

0 个答案:

没有答案