{"record":{"id":"901eed22807c63d1","repo":"tui-cs/Terminal.Gui","slug":"number-of-colors-must-match-the-number-of-bars","errorCode":null,"errorMessage":"Number of colors must match the number of bars","messagePattern":"Number of colors must match the number of bars","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/GraphView/MultiBarSeries.cs","lineNumber":27,"sourceCode":"\n    /// <summary>Creates a new series of clustered bars.</summary>\n    /// <param name=\"numberOfBarsPerCategory\">Each category has this many bars</param>\n    /// <param name=\"barsEvery\">How far apart to put each category (in graph space)</param>\n    /// <param name=\"spacing\">\n    ///     How much spacing between bars in a category (should be less than <paramref name=\"barsEvery\"/>/\n    ///     <paramref name=\"numberOfBarsPerCategory\"/>)\n    /// </param>\n    /// <param name=\"colors\">\n    ///     Array of colors that define bar color in each category. Length must match\n    ///     <paramref name=\"numberOfBarsPerCategory\"/>\n    /// </param>\n    public MultiBarSeries (int numberOfBarsPerCategory, float barsEvery, float spacing, Attribute []? colors = null)\n    {\n        _subSeries = new BarSeries [numberOfBarsPerCategory];\n\n        if (colors is { } && colors.Length != numberOfBarsPerCategory)\n        {\n            throw new ArgumentException (@\"Number of colors must match the number of bars\", nameof (numberOfBarsPerCategory));\n        }\n\n        for (var i = 0; i < numberOfBarsPerCategory; i++)\n        {\n            _subSeries [i] = new ()\n            {\n                BarEvery = barsEvery,\n                Offset = i * spacing,\n\n                // Only draw labels for the first bar in each category\n                DrawLabels = i == 0\n            };\n\n            if (colors is { })\n            {\n                _subSeries [i].OverrideBarColor = colors [i];\n            }\n        }","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/GraphView/MultiBarSeries.cs#L9-L45","documentation":"The MultiBarSeries constructor pre-allocates one BarSeries per bar in a category (numberOfBarsPerCategory). If a colors array is supplied, its length must exactly match so each bar gets a color; a mismatch means some bars would be uncolored or the array would overflow.","triggerScenarios":"new MultiBarSeries(3, 1f, 1f, new Attribute[] { a, b }) — 3 bars but only 2 colors; colors array longer than numberOfBarsPerCategory.","commonSituations":"Hardcoding a colors array then changing numberOfBarsPerCategory; sharing a palette array across series of different widths; off-by-one in palette generation.","solutions":["Ensure colors.Length == numberOfBarsPerCategory, or pass null for default colors.","Generate the colors array dynamically from numberOfBarsPerCategory.","If colors are optional, omit the parameter entirely."],"exampleFix":"// before\nnew MultiBarSeries(3, 1f, 0.5f, new Attribute[] { red, green });\n// after\nnew MultiBarSeries(3, 1f, 0.5f, new Attribute[] { red, green, blue });","handlingStrategy":"validation","validationCode":"Attribute[] palette = colors is null || colors.Length == n ? colors : throw new ArgumentException(\"colors mismatch\");\nvar series = new MultiBarSeries (n, every, spacing, palette);","typeGuard":"static bool ColorsMatchBars (int n, Attribute[]? c) => c is null || c.Length == n;","tryCatchPattern":null,"preventionTips":["Generate the colors array from numberOfBarsPerCategory.","Pass null when colors are optional.","Keep palette length in sync if n changes."],"tags":["graphview","multibarseries","constructor","validation"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}