MpChart没有在栏下方显示xaxis标签

时间:2017-11-09 06:30:23

标签: android bar-chart mpandroidchart

enter image description here这是我得到的输出:

这是我显示条形图的代码段: 考虑到所有组合,对我来说不起作用。此外,如果我将 xaxis 标签限制为4,它仍会显示5个值。已经看过很多解决方案,但对我来说并不起作用。

mChart.setMaxVisibleValueCount(60);
            mChart.setDrawGridBackground(false);
            mChart.setDragEnabled(true);
            mChart.getDescription().setEnabled(false);
            mChart.setFitBars(true);
            mChart.setPinchZoom(false);
            mChart.setDoubleTapToZoomEnabled(false);

            XAxis xAxis = mChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setDrawGridLines(false);
            xAxis.setGranularity(0.5f);
            xAxis.setGranularityEnabled(true);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    Float dfloat = value;
                    long dateInMillis = dfloat.longValue();
                    return ChartUtils.getMonthYearFromMillis(dateInMillis);
                }
            });

            YAxis leftAxis = mChart.getAxisLeft();
            leftAxis.setValueFormatter(new ChartUtils.YAxisValueRsFormatter(context));
            leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
            leftAxis.setSpaceTop(15f);
            leftAxis.setDrawGridLines(false);

            YAxis rightAxis = mChart.getAxisRight();
            rightAxis.setEnabled(false);

            Legend l = mChart.getLegend();
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
            l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            l.setDrawInside(false);
            l.setForm(Legend.LegendForm.SQUARE);
            l.setFormSize(9f);
            l.setTextSize(11f);
            l.setXEntrySpace(4f);

            final Map<Long, Double> monthMap = new HashMap<>();
            for (Map.Entry<Long, Double> cMap : chartMap.entrySet()) {
                long month = ChartUtils.getFirstDateMonthYear(cMap.getKey());
                Double amount = monthMap.get(month);
                if (null == amount) {
                    amount = cMap.getValue();
                } else {
                    amount += cMap.getValue();
                }
                monthMap.put(month, amount);
            }
            final Map<Long, Double> treeMap = new TreeMap<>(monthMap);

            final ArrayList<BarEntry> yVals1 = new ArrayList<>();
            for (Map.Entry<Long, Double> cMap : treeMap.entrySet()) {
                yVals1.add(new BarEntry(cMap.getKey(), cMap.getValue().floatValue()));
            }

            final BarDataSet set1;

            if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
                set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
                set1.setValues(yVals1);
                mChart.getData().notifyDataChanged();
                mChart.notifyDataSetChanged();
            } else {
                set1 = new BarDataSet(yVals1, "Amounts");
                set1.setValueTextColor(context.getResources().getColor(R.color.dark_gray_primary));
                set1.setColor(context.getResources().getColor(R.color.colorPrimaryLight));
                set1.setHighLightColor(context.getResources().getColor(R.color.colorPrimaryLight));
                set1.setDrawValues(false);

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

                BarData data = new BarData(dataSets);
                data.setValueTextSize(10f);
                if (monthMap.size() >= 4) {
                    data.setBarWidth(2000000000f);
                } else {
                    data.setBarWidth(2500000000f);
                }

                mChart.setData(data);
            }

            final CustomBarChartMarkerView mv = new CustomBarChartMarkerView(context, R.layout.view_custom_marker);
            mChart.setMarker(mv);
            mChart.setHighlightFullBarEnabled(true);
            if (monthMap.size() > 1) {
                if (monthMap.size() >= 4) {
                    xAxis.setLabelCount(4);
                } else {
                    xAxis.setLabelCount(monthMap.size());
                }
                mChart.setVisibility(View.VISIBLE);
            } else {
                mChart.setVisibility(View.GONE);
            }

尝试了所有选项和组合。仍然没有得到所需的输出。条形x轴(在这种情况下为日期)并不显示在条形图的正下方。

2 个答案:

答案 0 :(得分:0)

xAxis.setCenterAxisLabels(true);

将轴标签居中,而不是将它们绘制在原始位置。这对于分组BarChart特别有用。

来自javadocs

答案 1 :(得分:0)

自定义xAxis如下:

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

我使用的是3.0.1版本