安卓图表制作库MPAndroidChart如何使用双Y轴展示不同刻度。

3
我希望我的图表能像图片一样有两个不同刻度的Y轴。我该如何更改每个轴的刻度?

2
你尝试过绘制两个图形并重叠它们吗? - Pedro Oliveira
这不是一个好的实现。 - rafaelasguerra
2
如果你这么说的话,那就祝你好运用这个库找到一个好的。因为万一你没有阅读文档,它不支持这样的功能。 - Pedro Oliveira
Hellocharts库可以做到这一点。但我真的想使用mpandroidchart。谢谢你的建议。我会等待更好的解决方案。在最后的情况下,我会这样做。 - rafaelasguerra
3个回答

5

上周末发布了2.0.0-beta版本,其中包含一个CombinedChart,可以在一个图表中绘制折线图、柱状图、散点图和蜡烛图。所以该功能终于可用了

关于我的升级发现,你不能简单地为每个数据集添加addDataSet(),你必须生成各个数据集类型(柱状图、折线图、散点图等),然后使用SetData()为适当的类型触发渲染。

这是示例代码


链接已经失效,建议将代码粘贴在文本中而不是使用外部链接 :) - Amir Dora.

4

MPAndroidChart库的作者在这里清楚地指出(MPChart - 散点图和线性图在同一绘图中),该功能尚未可用。因此,我也在寻找更好的方法。

因此,您可以使用此库将两种不同的图表类型显示为一个图表,只需重叠它们即可。

我已经按照以下方式完成了此操作:
1. 编辑活动布局xml,将FrameLayout作为图表容器。

<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>

现在,在Activity类中,构建图表对象并将它们附加到先前的容器中:


    // 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 的优缺点如下:
PROs
  • 您将拥有更多结合 MPAndroidChart 的图表。
    CONs
  • 您需要注意X和Y标签的对齐。
  • 用户事件:只有上层(Z顺序)的图表视图会获得用户输入,因此您需要确保所有其他图表视图行为相同(处理或忽略输入,例如缩放图表或值选择)。
  • 我希望这个伟大库能够尽快发布这个选项。先感谢 Philipp Jahoda!


    4
    哥们,这个重叠的想法是一个天才的解决办法,我从未想过。我目前正在研究组合图表,该功能可能会在接下来的几周内推出。 - Philipp Jahoda

    0
    在我的情况下,我按照mpandroidcharts建议的一切都做了。
    但是,我发现“右y轴”和“左y轴”的刻度仍然相同。
    罪魁祸首就是这个。
        combinedChart.axisLeft?.axisMinimum = //some calculations
        combinedChart.axisLeft?.axisMaximum = //some calculations
        combinedChart.axisRight?.axisMinimum = //some calculations
        combinedChart.axisRight?.axisMaximum = //some calculations
    

    基本上是复制粘贴错误,当你从柱状图或线图中复制你的完美代码时,在设置轴最小值和最大值的位置出现了问题。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接