在最新版本的带有StackedBar图表的MPAndroid图表中,无法在X轴上设置/显示标签

时间:2019-07-02 12:41:53

标签: android mpandroidchart stacked-chart

我正在实现MPAndroid Chart库的堆积条形图,但是它不允许我添加x轴标签,并且它为少量数据显示不同的颜色。我的代码是:

    private void setStackChartData() {
    stackedChart.setOnChartValueSelectedListener(this);
    stackedChart.getDescription().setEnabled(false);

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    stackedChart.setMaxVisibleValueCount(40);

    // scaling can now only be done on x- and y-axis separately
    stackedChart.setPinchZoom(false);

    stackedChart.setDrawGridBackground(false);
    stackedChart.setDrawBarShadow(false);

    stackedChart.setDrawValueAboveBar(false);
    stackedChart.setHighlightFullBarEnabled(false);

    // change the position of the y-labels
    YAxis leftAxis = stackedChart.getAxisLeft();
 //   leftAxis.setValueFormatter(new MyValueFormatter("K"));
    leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
    stackedChart.getAxisRight().setEnabled(false);

    XAxis xLabels = stackedChart.getXAxis();
    xLabels.setPosition(XAxis.XAxisPosition.TOP);


    Legend l = stackedChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setFormSize(8f);
    l.setFormToTextSpace(4f);
    l.setXEntrySpace(6f);

    setStackedChart();

}

private void  setStackedChart(){

    final ArrayList<String> xAxisLabel = new ArrayList<>();
    xAxisLabel.add("Mon");
    xAxisLabel.add("Tue");
    xAxisLabel.add("Wed");
    xAxisLabel.add("Thu");
    xAxisLabel.add("Fri");
    xAxisLabel.add("Sat");
    xAxisLabel.add("Sun");

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

    for (int i = 0; i < 12; i++) {
        float mul = (30 + 1);
        float val1 = (float) (Math.random() * mul) + mul / 3;
        float val2 = (float) (Math.random() * mul) + mul / 3;
       // float val3 = (float) (Math.random() * mul) + mul / 3;

        values.add(new BarEntry(
                i,
                new float[]{val1, val2},
                getResources().getDrawable(R.drawable.ic_expand_less)));
    }

    BarDataSet set1;

    if (stackedChart.getData() != null &&
            stackedChart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) stackedChart.getData().getDataSetByIndex(0);
        set1.setValues(values);
        stackedChart.getData().notifyDataChanged();
        stackedChart.notifyDataSetChanged();
    } else {
        set1 = new BarDataSet(values, "");
        set1.setDrawIcons(false);
        set1.setColors(getColors());
        set1.setStackLabels(new String[]{"Births", "Divorces"});

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);

        BarData data = new BarData(dataSets);
        data.setValueFormatter(new StackedValueFormatter(false, "", 1));
        data.setValueTextColor(Color.WHITE);

        stackedChart.setData(data);
    }

    stackedChart.setFitBars(true);
    stackedChart.invalidate();

}

我尝试了各种方法,但是不建议使用其他解决方案,因此无法应用它们。希望你能得到我的问题。提前致谢。

0 个答案:

没有答案