Index out of range exception when updating donut chart

When updating the data in my donut chart, I frequently get the following exception:

 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
         at System.Collections.Generic.List`1.get_Item(Int32 index)
         at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
         at Radzen.Blazor.RadzenDonutSeries`1.<>c__DisplayClass10_0.<Render>b__0(RenderTreeBuilder __builder2)
         at Radzen.Blazor.RadzenChart.<BuildRenderTree>b__113_2(RenderTreeBuilder __builder2)
         at Microsoft.AspNetCore.Components.CascadingValue`1.Render(RenderTreeBuilder builder)
         at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)

It seems to occur in the following scenario:

  • user is hovering over the chart such that a tooltip for the category is open
  • custom fills are being used
  • the custom fills array increases in length due to the amount of categories changing

This appears to be the same as this issue, but that was reported as fixed in 2021, so perhaps it has come up again?

Here's an example displaying the issue (click on the chart once to load initial data, then bring up a tooltip and click on it again to add more categories)

<div @onclick=MakeData>
    <RadzenChart>
        <RadzenDonutSeries Data=@counts
                  CategoryProperty="Key" ValueProperty="Value" Y="45" X="97" Radius="60"
                  InnerRadius="30" Fills=@colors>
        </RadzenDonutSeries>
        <RadzenLegend Visible="false" />
    </RadzenChart>
</div>

@code
{
    private Dictionary<string, int> counts = new();
    private List<string> colors = new();

    void MakeData()
    {
        var isFirst = colors.Count == 0;
        counts = new();
        colors = new();
        for (int i = 0; i < (isFirst ? 3 : new Random().Next(4, 6)); i ++)
        {
            counts["a" + i.ToString()] = (int)(new Random().Next(0, 10));
            colors.Add("#" + i.ToString() + i.ToString() + i.ToString()); // hacky making colors
        }
    }
}

Tested in 4.29.8

Hi @ThatCoolCoder,

Thanks! We have fixed the issue.

1 Like