PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.56k stars 9.01k forks source link

[BUG v3.1.0] Stacked Bars are misplaced in wrong positions #3154

Open ghizoulo opened 7 years ago

ghizoulo commented 7 years ago

I am using v3.1.0 of this library. I am fine with it until I have a chart like this one below: mwexn

So I want to get this result, here is my code:

final ArrayList<String> labels = new ArrayList<String>();
        labels.addAll(Utils.getXLabels()); // a list of months

        //-------Estimé-------
        Map<Integer, List<BarEntry>> entriesEstime = new TreeMap<>();

        //for create Grouped Bar chart
        for (Map.Entry<Integer, List<Graphe1>> elt : data.entrySet()) {
            List<BarEntry> entryEstime = new ArrayList<>();
            entriesEstime.put(elt.getKey(), new ArrayList<BarEntry>());
            if (!elt.getValue().isEmpty() && elt.getValue() != null) {
                for (Graphe1 graphe2 : elt.getValue()) {
                    entryEstime.add(new BarEntry(Float.parseFloat(graphe2.getMois()), new float[]{graphe2.getEstime(), graphe2.getRealise()}));
                }
                entriesEstime.get(elt.getKey()).addAll(entryEstime);
            }
        }

        List<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();

        int i = 0;
        //create dataset estime
        for (Map.Entry<Integer, List<BarEntry>> entry : entriesEstime.entrySet()) {
            if (!entry.getValue().isEmpty()) {
                BarDataSet dataSet = new BarDataSet(entry.getValue(), entry.getKey()+"");
                dataSet.setColors(new int[] {Color.rgb(67,67,72), Color.rgb(124,181,236)});
                dataSet.setStackLabels(new String[]{
                        "Estimé", "Réalisé"
                });
                dataSets.add(dataSet);
            }
            i++;
        }

        BarData data = new BarData(dataSets);

        data.setBarWidth(0.45f);
        data.setDrawValues(true);
        //hide legend
        mChart.getLegend().setEnabled(false);

        //delete the right yAxis labels
        mChart.getAxisRight().setDrawLabels(false);
        mChart.getAxisRight().setDrawAxisLine(false);
        mChart.getAxisRight().setEnabled(false);

        //show vertical lines
        mChart.getAxisLeft().setDrawGridLines(true);

        XAxis xAxis = mChart.getXAxis();

        //make the xAxis displayed on the bottom
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        xAxis.setCenterAxisLabels(true);
        xAxis.setDrawGridLines(true);

        //make the xAxis displayed on the bottom
        xAxis.setGranularity(1f);
        xAxis.setAxisMaximum(labels.size());
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularityEnabled(true);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
        float groupSpace = 0.06f;
        float barSpace = 0.02f;
        mChart.setData(data);
        mChart.groupBars(0f, groupSpace, barSpace);
        mChart.animateY(5000);

With this code, I have this chart: cddeed

The bars are not displayed in their right position, for example the second bar in January must be in March.

Please if anyone can find a solution to this, let me know @PhilJay @danielgindi

tjiang11 commented 7 years ago

+1 on this issue, how are the bars supposed to be grouped by X value when groupBars() modifies all the X values? There should be decoupling between the displayed X value and inherent X value.

tjiang11 commented 7 years ago

I have found the solution: You must choose your group space and bar space and bar width such that: (number data sets) * (bar space + bar width) + group space = 1.0. See example.

MohammadRezaei92 commented 6 years ago

@tjiang11 Your little explanation solved my problem that I stuck in it for 2 days.

valentingavran commented 4 years ago

@tjiang11 Your little explanation solved my problem that I stuck in it for 2 days.

The link does not work anymore 🤕