MPAndroidChart BarChart - 栏内的垂直值

时间:2017-12-28 10:59:00

标签: java android mpandroidchart

我正在使用MPAndroidChart库作为条形图,在那里,我使用chart.setDrawValueAboveBar(false)在条形图内设置条形值,现在我想在条形图内垂直显示这些值。 / p>

请帮助..提前谢谢你。

2 个答案:

答案 0 :(得分:2)

首先使这个虚假

mChart.setDrawValueAboveBar(false);

然后启用数据集的绘制值

barDataSet.setDrawValues(true);

由于您还需要旋转文本..您必须为图表实现自定义渲染器。如果您想知道渲染器的工作原理,请查看此answer

我在下面提供了一个示例实现。您可以对其进行修改以控制适合您的文本位置。

自定义渲染器

public class BarChartCustomRenderer extends BarChartRenderer {

    public BarChartCustomRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

    public void drawValue(Canvas c, IValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) {
        mValuePaint.setColor(color);
        c.save();
        c.rotate(90f, x, y);
        Log.d("here", formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler) );
        c.drawText(formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler), x, y, mValuePaint);
        c.restore();
    }
}

图表代码

        BarChart mChart = (BarChart) findViewById(R.id.barChart);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(false);
        mChart.getDescription().setEnabled(false);
        mChart.setDrawGridBackground(false);

        //**add renderer**
        BarChartCustomRenderer barChartCustomRenderer = new BarChartCustomRenderer(mChart, mChart.getAnimator(),   mChart.getViewPortHandler());
        mChart.setRenderer(barChartCustomRenderer);


        XAxis xaxis = mChart.getXAxis();
        xaxis.setDrawGridLines(false);
        xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xaxis.setGranularity(0.5f);
        xaxis.setGranularityEnabled(true);
        xaxis.setDrawLabels(true);
        xaxis.setDrawAxisLine(false);

        YAxis yAxisLeft = mChart.getAxisLeft();
        yAxisLeft.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        yAxisLeft.setDrawGridLines(false);
        yAxisLeft.setDrawAxisLine(false);
        yAxisLeft.setEnabled(false);

        mChart.getAxisRight().setEnabled(false);

        Legend legend = mChart.getLegend();
        legend.setEnabled(false);

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

        ArrayList<String> ylabels = new ArrayList<>();
        for (int i = 0; i < 6; ++i) {
            BarEntry entry = new BarEntry(i, (i + 1) * 2);
            valueSet1.add(entry);
            ylabels.add(" " + i);
        }

        List<IBarDataSet> dataSets = new ArrayList<>();
        BarDataSet barDataSet = new BarDataSet(valueSet1, " ");
        barDataSet.setColor(Color.CYAN);
        barDataSet.setDrawValues(true);
        dataSets.add(barDataSet);


        BarData data = new BarData(dataSets);
        data.setBarWidth(0.4f);
        data.setValueTextSize(10f);
        data.setValueTextColor(Color.BLACK);
        mChart.setData(data);
        mChart.setFitBars(true);
        mChart.invalidate();

<强>结果

enter image description here

答案 1 :(得分:0)

class MyCustomRenderer(
    chart: BarDataProvider, animator: ChartAnimator, viewPortHandler: ViewPortHandler
) : HorizontalBarChartRenderer(chart, animator, viewPortHandler) {

    override fun drawValue(c: Canvas, valueText: String, x: Float, y: Float, color: Int) {
        mValuePaint.color = color
        val xPoint = Utils.convertDpToPixel(26f)
        val yPoint = Utils.convertDpToPixel(16f)
        c.drawText(valueText, xPoint , yPoint , mValuePaint)
    }
}