Hot questions for Combined Chart in MPAndroidChart

MpAndroidChart - How to fill area between two lines on a CombinedChart?

Question: I am using CombinedChart (as I also have bar data to show). But applying the above solution to a CombinedChart results in this exception:

java.lang.ClassCastException: org.example.MyLineLegendRenderer cannot be cast to com.github.mikephil.charting.renderer.CombinedChartRenderer at com.github.mikephil.charting.charts.CombinedChart.setData(CombinedChart.java:96)

So I have looked at CombinedChartRenderer but (unlike LineChartRenderer), it does not seem to contain any functionality for filling.

So how can I fill the space between two lines on a CombinedChart?

Answer: You can override the createRenderers() method in the CombinedChartRenderer class. This way you can use your own renderer for each and every type of graph that you use in the your CombinedChart.

It will be something like this :

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));
                        }
                }
            }    
        }
    }
}

In the above code, in the switch block when case LINE: is encountered, we replace the default renderer with our own version : MyLineChartRenderer.

Then finally in your Activity code, you can use this renderer with your CombinedChart :

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

MPAndroidchart-CombinedChart don't draw start zero?

Question: I use mpandroidchart-combinedchart for Stock chart.

the bottom chart did not start from 0,who can help me? I try chart.getAxisLeft().setAxisMinimum(0); but it didn't work.

Answer: Try with chart.getAxisLeft().setSpaceBottom(0f)


The granularity between LineChart and CandleStickChart is different

Question: I have a demand,the CandleStickChart and the LineChart is showing on the same table,but i find a question

the YAxis granularity between LineChart and CandleStickChart is different

Layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.my.myapplication1.MainActivity">

<RelativeLayout
    android:layout_width="300dp"
    android:layout_height="400dp">

    <com.github.mikephil.charting.charts.CandleStickChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/linechart"
        android:layout_width="match_parent"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_height="match_parent" />
</RelativeLayout>

the code setting YAxis of LineChart and CandleStickChart :

YAxis axisLeft = linechart.getAxisLeft();
axisLeft.setAxisMinimum(0);
axisLeft.setGranularity(1);
axisLeft.setLabelCount(7, false);
axisLeft.setDrawGridLines(false);
axisLeft.setDrawAxisLine(false);

YAxis leftAxis = candleChart.getAxisLeft();
leftAxis.setAxisMinimum(0);
leftAxis.setGranularity(1);
leftAxis.setLabelCount(7, false);
leftAxis.setDrawGridLines(false);
leftAxis.setDrawAxisLine(false);

although i set the granularity ,it's not work. How can i do to let the granularity of them be the same?

Answer: Instead of your Layout.xml to:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.my.myapplication1.MainActivity">

<RelativeLayout
    android:layout_width="300dp"
    android:layout_height="400dp">

    <com.github.mikephil.charting.charts.CombinedChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

And then look combined chart example here in link below: https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java