MPAndroidChart: Combined Chart

那年仲夏 提交于 2019-12-11 02:59:46

问题


I am using MPAndroidChart library.

I wanted to use the CombinedChart to create a Chart like that:

Is that possible? I tried it out but it doesnt seem to work because the entries arent working as i expected. You cant say an entry has value 2 on the x-axis and value 300 on the y-axis. Also i cant create two different y-axis, one for the bars and one for the lines.

Some curious thing is that MPAndroidChart first adds all x-values and after that all y-values and you have no possibility to controll which y-value belongs to which x-value because its just inserting the y-values in order of their appearing and relates it to the next x-value.

Is there some way how i can create such a diagram with MPAndroidChart. I actually dont want to be forced to use Google Charts because of the required internet connection (but creating that kind of diagram would work perfectly with Google Charts).


回答1:


  • you can have 2 different axes
  • you have control which y-value belongs to which x-value (of course!)
  • check out the combined-chart-example
  • it looks like this:

UPDATE: v3.0.0+

The example for the CombinedChart has been extended, now allowing stacked bars and grouped bars together with other chart types.

The essence of setting data for a CombinedChart is the CombinedData class. It can be populated with various other data, such as LineData, BarData etc:

    CombinedData data = new CombinedData();

    data.setData(generateLineData()); // set LineData...
    data.setData(generateBarData()); // set BarData...
    data.setData(generateBubbleData());
    data.setData(generateScatterData());
    data.setData(generateCandleData());

    chart.setData(data);
    chart.invalidate();

How to create e.g. LineData can be found in the setting data documentation.




回答2:


You can create a custom class that extends View and in onDraw method create Rectangles of the required height, width and position for bars. As for the lines well that's obvious, set the stroke to make it more visible.

I have used a custom View to create bars in my app WhiteMarker and lines in Chron.




回答3:


As the accepted aswer has broken links, you should use:

<com.github.mikephil.charting.charts.CombinedChart
        android:id="@+id/chart"/>

In your XML

and then here is some code:

        LineData linearData = new LineData(set1);
        // Set you LinearData

        BarData barData = new BarData(set2);
        // Set you LinearData

        CombinedData data=new CombinedData();
        data.setData(linearData);
        data.setData(barData);

EDIT: Original answer has been modified to reflect what I just wrote. Links have been updated and some code has been added.



来源:https://stackoverflow.com/questions/31056095/mpandroidchart-combined-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!