Search code examples
androidmpandroidchartstackedbarseries

Android - MPAndroidChart - Overlay 3 bars


This question has been made here, but still has got no answer...

Using StackedBarActivity example from MPAndroidChart library to draw a 3 merge bars, and setting the values as [1,2,3]... the chart is showed with a total of all numbers, 1+2+3=6.. but what I want is to merge all values in order to value 2 overlay value 3 and value 1 overlay value 2, as showed on the image bellow:

enter image description here

Basically, I want to use the StackedBarActivity activity, but not stack all the bars, instead, I want to put one behind another. Visually I know I can subtract the bigger value with the smaller, making a sum until reaching 3 (1+1+1), but then the value 3 will retain value 1 and not value 3.

Is there a way I can do this using this class? Is there a better class to do this?


Solution

  • I know I am like 6 months late with answer, but I think that is working solution:

    List<String> xVals = getXVals(); //some function to get values
    List<BarEntry> yVals = new ArrayList();
    int[] barColors = new int[bars.size() * numberOfValues];
    int index=0;
    for (Bar bar : bars) {
        List<BarEntry> yValsForBar = new ArrayList();
        for (int i=0; i<numberOfValues; i++) {
            yValsForBar.add(new BarEntry(bar.value(i), i));
            barColors[i+index*numberOfValues] = bar.color();
        }
        yVals.addAll(yValsForBar);
        index++;
    }
    BarDataSet dataSet = new BarDataSet(yVals, "data set");
    BarData data = new BarData(xVals);
    data.addDataSet(dataSet);
    

    And here, you should have overlying bars in different colors (note: of course you might not see some bars if they have bigger values than next ones, if you wan't to sort them you will have to do some modifications to this). At least works with me.