Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPandroidchart Double YAxis with different scales

I want that my chart has two YAxis with different scales like the image . How I can change the scale for each axis?

enter image description here

like image 218
rafaelasguerra Avatar asked Jul 18 '26 13:07

rafaelasguerra


2 Answers

Version 2.0.0-beta was released over the weekend with a CombinedChart, which allows plotting Line-, Bar-, Scatter- and CandleData in one chart. So the feature is finally available.

One note about what I found during my upgrade, you can't simply addDataSet() for each one, you have to generate the individual DataSet types (Bar, Line, Scatter, etc) and then use SetData() for the appropriate type, to trigger rendering.

Here is the example code.

like image 103
gtcompscientist Avatar answered Jul 20 '26 04:07

gtcompscientist


The author of MPAndroidChart library clearly specifies here, (MPChart - Scatter Chart and LineChart in same plot), that this function is not yet available. So, I am also looking for a nicer approach.

As such, you can display two different chart types as one, still using this library, only by overlapping them.

I have done this on my side as it follows:
1. Edit the activity layout xml, having a FrameLayout as charts container.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    .......
    <FrameLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/root_view">
    </FrameLayout>
    .......
</LinearLayout>
  1. Now, in the Activity class, build the chart objects and attach them to the previous container:


    // construct the charts, as different objects
    // make sure to have valid Data Sets    
    LineChartItem lineChart = new LineChartItem(lineChartDataset, getApplicationContext());
    BarChartItem barChart = new BarChartItem(barChartDataset, getApplicationContext());

    // get the charts' container
    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.root_view);

    // populate the container with the charts' views
    frameLayout.addView(lineChart.getView(0, null, getApplicationContext()));
    frameLayout.addView(barChart.getView(1, null, getApplicationContext()));

PROs/CONs for such a practice:
PROs

  • You have more combined charts with MPAndroidChart.
    CONs
  • You need to take care of X and Y Labels alignment.
  • User Events: only the upper (in Z order) Chart View will get the user input, so you need to make sure that all the other chart views behave in the same manner (either processing OR ignoring the input, like zooming the chart or value selection).
  • I hope for a quick release of this great lib having this option. Thank you in advance Philipp Jahoda!

    like image 29
    Adrian Boatca Avatar answered Jul 20 '26 03:07

    Adrian Boatca