Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart: Combined Chart

I am using MPAndroidChart library.

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

enter image description here

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

like image 341
Mulgard Avatar asked Jun 25 '15 16:06

Mulgard


2 Answers

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

enter image description here

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.

like image 158
Philipp Jahoda Avatar answered Oct 14 '22 03:10

Philipp Jahoda


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.

like image 35
Mayank Avatar answered Oct 14 '22 04:10

Mayank