Skip to content

Feature Request: Add support for customizable legend content in RadzenChart #2146

@franklupo

Description

@franklupo

Is your feature request related to a problem? Please describe.
Currently, RadzenChart only supports a basic legend with series names. I'm often want to show more detailed statistics (such as latest value, min, avg, max) directly in the legend for better at-a-glance interpretation of monitoring or performance charts.

Describe the solution you'd like
I would like the RadzenChart component to support an extended/customizable legend format where I can include not only the series name, but also dynamic values such as:

  • Latest value
  • Min
  • Average
  • Max

Ideally, the legend entry would support templating or data-binding to computed statistics for each series.

Describe alternatives you've considered

  • Manually creating a separate table below the chart to show statistics (less user-friendly and visually disconnected).
  • Using a third-party chart library that supports extended legends (losing Radzen integration and consistency).

Additional context
The screenshot below shows an example of the desired result:
A combined chart where the legend includes not only the label (e.g., CPU, IO delay) but also key metrics like Latest, Min, Avg, Max for each series.

Image

@typeparam TItem

<RadzenTable AllowAlternatingRows="true" GridLines="DataGridGridLines.None">
    <RadzenTableHeader>
        <RadzenTableHeaderRow>
            <RadzenTableHeaderCell>@L["Counter"]</RadzenTableHeaderCell>
            <RadzenTableHeaderCell>@L["Unit"]</RadzenTableHeaderCell>
            <RadzenTableHeaderCell>@L["Latest"]</RadzenTableHeaderCell>
            <RadzenTableHeaderCell>@L["Min"]</RadzenTableHeaderCell>
            <RadzenTableHeaderCell>@L["Avg"]</RadzenTableHeaderCell>
            <RadzenTableHeaderCell>@L["Max"]</RadzenTableHeaderCell>
        </RadzenTableHeaderRow>
    </RadzenTableHeader>

    <RadzenTableBody>
        @foreach (var item in Counters)
        {
            <RadzenTableRow>
                <RadzenTableCell>@item.Name</RadzenTableCell>
                <RadzenTableCell>@item.Unit</RadzenTableCell>
                <RadzenTableCell>@GetValue(item,TypeValue.Latest)</RadzenTableCell>
                <RadzenTableCell>@GetValue(item, TypeValue.Min)</RadzenTableCell>
                <RadzenTableCell>@GetValue(item, TypeValue.Average)</RadzenTableCell>
                <RadzenTableCell>@GetValue(item, TypeValue.Max)</RadzenTableCell>
            </RadzenTableRow>
        }
    </RadzenTableBody>

</RadzenTable>

@code
{
  [Parameter] public IEnumerable<TItem> Data { get; set; } = [];
  [Parameter] public IEnumerable<LegendCounterItem> Counters { get; set; } = [];

  private enum TypeValue
  {
      Latest,
      Average,
      Min,
      Max
  }

  private string GetValue(LegendCounterItem item, TypeValue type)
  {
      var compiledSelector = item.Selector.Compile();

      var value = type switch
      {
          TypeValue.Latest => Data.Select(compiledSelector).FirstOrDefault(),
          TypeValue.Average => Data.Select(compiledSelector).DefaultIfEmpty(0).Average(),
          TypeValue.Min => Data.Select(compiledSelector).DefaultIfEmpty(0).Min(),
          TypeValue.Max => Data.Select(compiledSelector).DefaultIfEmpty(0).Max(),
          _ => 0,
      };

      return item.Formatter != null
                  ? item.Formatter(value)
                  : !string.IsNullOrEmpty(item.StringFormat)
                      ? value.ToString(item.StringFormat)
                      : value.ToString();
  }

  public record LegendCounterItem(string Name,
                                  string Unit,
                                  string StringFormat,
                                  Expression<Func<TItem, double>> Selector)
  {
      public Func<double, string>? Formatter { get; set; }
  }
}

Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions