Skip to content

kb(Chart): Add KB for sorting Chart categories #2743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions knowledge-base/chart-sort-categories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: Sort Chart Categories
description: How to sort horizontal axis categories in the Telerik Chart for Blazor, depending on data point values?
type: how-to
page_title: How to Sort Blazor Chart Categories
slug: chart-kb-sort-categories
tags: blazor, chart
ticketid: 1677715
res_type: kb
---

## Environment

<table>
<tbody>
<tr>
<td>Product</td>
<td>Chart for Blazor</td>
</tr>
</tbody>
</table>

## Description

This KB answers the following questions:

* How to sort a stacked Chart by category total values?
* How to order the Telerik Blazor Chart categories by the aggregated sum values of the data items?
* How do you sort a stacked column Chart by category totals?

## Solution

The Chart sorts its categories by their order of appearance in the data. To reorder the categories, you need to reorder the Chart data items, so that the `CategoryField` values occur in the same order as the desired category order.

The example below sorts the categories (`Company`) by the stacked value (sum) of each company's `Revenue` for all its `Product`s.

>caption Sort Chart categories by sorting Chart data items

````RAZOR
<TelerikChart>
<ChartSeriesItems>
@for (int s = 1; s <= ProductCount; s++)
{
<ChartSeries @key="@s"
Type="ChartSeriesType.Column"
Data="@ChartData.Where( x => x.Product == $"Product {s}" ).ToList()"
CategoryField="@nameof(SalesData.Company)"
Field="@nameof(SalesData.Revenue)"
Name="@( $"Product {s}" )">
<ChartSeriesStack Enabled="true" Group="default" />
<ChartSeriesTooltip Visible="true" />
</ChartSeries>
}
</ChartSeriesItems>

<ChartValueAxes>
<ChartValueAxis>
<ChartValueAxisLabels Format="c2" />
</ChartValueAxis>
</ChartValueAxes>

<ChartTitle Text="Revenue per Company and Product"></ChartTitle>

<ChartLegend Position="ChartLegendPosition.Right">
</ChartLegend>
</TelerikChart>

@code {
private List<SalesData> ChartData { get; set; } = new();

private Dictionary<string, decimal> SortedTotalRevenues { get; set; } = new();

private const int CompanyCount = 5;
private const int ProductCount = 3;

protected override void OnInitialized()
{
for (int i = 1; i <= CompanyCount; i++)
{
for (int j = 1; j <= ProductCount; j++)
{
ChartData.Add(new SalesData()
{
Product = $"Product {j}",
Revenue = Random.Shared.Next(1, 1000),
Company = $"Company {i}"
});
}
}

ChartData = ChartData
.GroupBy(dataItem => dataItem.Company)
.Select(group => new { Company = group.Key, TotalRevenue = group.Sum(groupItem => groupItem.Revenue), Data = group.ToList() })
.OrderBy(anonymousObject => anonymousObject.TotalRevenue)
.SelectMany(anonymousObject => anonymousObject.Data)
.ToList();
}

public class SalesData
{
public int Id { get; set; }
public string Product { get; set; } = string.Empty;
public string Company { get; set; } = string.Empty;
public decimal Revenue { get; set; }
}
}
````

## See Also

* [How to display a stacked series sum label in the Telerik Blazor Chart](slug:chart-kb-stacked-series-sum-label)
* [Chart Series Stacking](slug://components/chart/stack)
Loading