MpAndroidChart - 如何填充CombinedChart上两行之间的区域?

时间:2018-04-23 16:42:33

标签: android mpandroidchart

这个类似问题的answer显示了如何填充MpAndroidChart LineChart 上两行之间的区域。

但是,我使用的是 CombinedChart ,而不是使用LineChart(因为我还有条形数据显示)。但是将上述解决方案应用于CombinedChart会导致此异常:

  

java.lang.ClassCastException:org.example.MyLineLegendRenderer不能   被投射到com.github.mikephil.charting.renderer.CombinedChartRenderer           在com.github.mikephil.charting.charts.CombinedChart.setData(combinedChart.java:96)

所以我查看了CombinedChartRenderer但是(与LineChartRenderer不同),它似乎不包含任何填充功能。

那么如何填充CombinedChart上两行之间的空格?

1 个答案:

答案 0 :(得分:1)

您可以覆盖createRenderers()课程中的CombinedChartRenderer方法。这样,您就可以为自己CombinedChart中使用的每种类型的图表使用自己的渲染器。

会是这样的:

public class MyCombinedChartRenderer extends CombinedChartRenderer {

    public MyCombinedChartRenderer(CombinedChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

    @Override
    public void createRenderers() {
        this.mRenderers.clear();
        CombinedChart chart = (CombinedChart)this.mChart.get();
        if (chart != null) {
            CombinedChart.DrawOrder[] orders = chart.getDrawOrder();
            CombinedChart.DrawOrder[] var3 = orders;
            int var4 = orders.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                CombinedChart.DrawOrder order = var3[var5];
                switch(order) {
                    case BAR:
                        if (chart.getBarData() != null) {
                            this.mRenderers.add(new BarChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case BUBBLE:
                        if (chart.getBubbleData() != null) {
                            this.mRenderers.add(new BubbleChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case LINE:
                        if (chart.getLineData() != null) {
                            // replace with you own Line chart renderer
                            //this.mRenderers.add(new LineChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                            this.mRenderers.add(new MyLineChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case CANDLE:
                        if (chart.getCandleData() != null) {
                            this.mRenderers.add(new CandleStickChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                        break;
                    case SCATTER:
                        if (chart.getScatterData() != null) {
                            this.mRenderers.add(new ScatterChartRenderer(chart, this.mAnimator, this.mViewPortHandler));
                        }
                }
            }    
        }
    }
}

在上面的代码中,遇到switchcase LINE:阻止,我们将默认渲染器替换为我们自己的版本:MyLineChartRenderer

然后最后在您的活动代码中,您可以将此渲染器与CombinedChart一起使用:

CombinedChart combinedChart = (CombinedChart) findViewById(R.id.barChart);
combinedChart.setRenderer(new MyCombinedChartRenderer(combinedChart, combinedChart.getAnimator(), combinedChart.getViewPortHandler()));