MPandroidchart Double YAxis with different scales
2 answers
The author of the MPAndroidChart library clearly states here ( MPChart - Scatter Chart and LineChart in the same plot ) that this feature is not yet available. So, I'm also looking for a more convenient approach.
Thus, you can display two different chart types as one while still using this library, only by overlapping them.
I did it on my side as follows:
1. Edit the layout of the xml activity having a FrameLayout container in the form of diagrams.
<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>
- Now, in the Activity class, create 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 this practice:
PROs
CONS You need to take care of the alignment of the X and Y labels.
Custom Events: Only the top (in Z order) Chart View will receive user input, so you need to make sure that all other chart views behave the same (either handling OR ignoring the input, such as scaling chart or value selection).
I look forward to a quick release of this large library that has this option. Thanks in advance to Philip Jahoda!
+4
source to share