PhilJay/MPAndroidChart · error · RuntimeException

BarData needs to hold at least 2 BarDataSets to allow groupi

Error message

BarData needs to hold at least 2 BarDataSets to allow grouping.

What it means

Thrown by BarData.groupBars() when fewer than two BarDataSets are present. Grouping arranges multiple datasets side by side per x-value and inserts group/bar spacing, so a single dataset has nothing to group. The check is setCount <= 1, which rejects both zero and one dataset. Remember to call notifyDataSetChanged() on the chart after a successful grouping.

Source

Thrown at MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.java:60

    public float getBarWidth() {
        return mBarWidth;
    }

    /**
     * Groups all BarDataSet objects this data object holds together by modifying the x-value of their entries.
     * Previously set x-values of entries will be overwritten. Leaves space between bars and groups as specified
     * by the parameters.
     * Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.
     *
     * @param fromX      the starting point on the x-axis where the grouping should begin
     * @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
     * @param barSpace   the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
     */
    public void groupBars(float fromX, float groupSpace, float barSpace) {

        int setCount = mDataSets.size();
        if (setCount <= 1) {
            throw new RuntimeException("BarData needs to hold at least 2 BarDataSets to allow grouping.");
        }

        IBarDataSet max = getMaxEntryCountSet();
        int maxEntryCount = max.getEntryCount();

        float groupSpaceWidthHalf = groupSpace / 2f;
        float barSpaceHalf = barSpace / 2f;
        float barWidthHalf = mBarWidth / 2f;

        float interval = getGroupWidth(groupSpace, barSpace);

        for (int i = 0; i < maxEntryCount; i++) {

            float start = fromX;
            fromX += groupSpaceWidthHalf;

            for (IBarDataSet set : mDataSets) {

View on GitHub (pinned to 9c7275a059)

Solutions

  1. Ensure BarData holds >= 2 BarDataSets before calling groupBars(): new BarData(set1, set2) or data.addDataSet(set2).
  2. Guard: if (barData.getDataSetCount() >= 2) barData.groupBars(...); else fall back to plain bar rendering.
  3. Only call groupBars() when the user has selected/loaded at least two series.
  4. After grouping, call chart.notifyDataSetChanged() and chart.invalidate().

Example fix

// before
BarData data = new BarData(set1); // only one set
data.groupBars(0f, 0.8f, 0.1f); // throws

// after
BarData data = new BarData(set1, set2);
data.setBarWidth(0.3f);
data.groupBars(0f, 0.8f, 0.1f);
chart.notifyDataSetChanged();
Defensive patterns

Strategy: validation

Validate before calling

if (barData.getDataSetCount() >= 2) {
    barData.groupBars(fromX, groupSpace, barSpace);
    chart.notifyDataSetChanged();
}

Type guard

boolean canGroup(BarData data) {
    return data != null && data.getDataSetCount() >= 2;
}

Prevention

When it happens

Trigger: Calling barData.groupBars(...) after adding only one BarDataSet. Calling groupBars() before all datasets are added. Removing datasets (e.g. user toggling series off) and leaving the groupBars() call in place.

Common situations: Building a grouped bar chart but only one series is loaded from the backend; dynamically filtering datasets down to one and re-running layout code; copy-pasted grouping call from a multi-series example into a single-series chart.

Related errors


AI-assisted analysis of PhilJay/MPAndroidChart@9c7275a059 (2026-08-14). Data as JSON: /api/errors/33de2d517dbc991d. Report an issue: GitHub.