Skip to content

Commit a88c40e

Browse files
docs(chart): dynamic series example
1 parent a046aa1 commit a88c40e

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: How to add dynamic series in a chart
3+
description: How to use dynamic series collection in a chart from the code-behind
4+
type: how-to
5+
page_title: How to use dynamic series in a chart
6+
slug: chart-dynamic-series
7+
position:
8+
tags:
9+
ticketid: 1424840
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Charts for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
26+
I want to add dynamic series at run time, from the C# code-behind, based on the user selected options.
27+
28+
29+
## Solution
30+
31+
The standard approach in Blazor is to loop over a view model with the needed data, and generate the needed series tags in the markup based on the code behind data.
32+
33+
This also applies to other collections in other components, or for generating a given number of component instances.
34+
35+
You may also find useful the [Data Binding article]({%slug components/chart/databind%}) on different ways to provide the data to the series and the chart.
36+
37+
38+
Here is a simple example:
39+
40+
````CSHTML
41+
@using Telerik.Blazor
42+
@using Telerik.Blazor.Components.Chart
43+
@using Telerik.Blazor.Components.Button
44+
45+
<TelerikButton OnClick="@AddSeries">Add series</TelerikButton>
46+
47+
<TelerikChart>
48+
<TelerikChartSeriesItems>
49+
@foreach (MyChartSeriesViewModel item in series)
50+
{
51+
<TelerikChartSeries Type="ChartSeriesType.Column" Name="@item.name" Data="@item.data">
52+
</TelerikChartSeries>
53+
}
54+
</TelerikChartSeriesItems>
55+
56+
<TelerikChartCategoryAxes>
57+
<TelerikChartCategoryAxis Categories="@xAxisItems"></TelerikChartCategoryAxis>
58+
</TelerikChartCategoryAxes>
59+
60+
<TelerikChartTitle Text="Quarterly revenue per product"></TelerikChartTitle>
61+
62+
<TelerikChartLegend Position="ChartLegendPosition.Right">
63+
</TelerikChartLegend>
64+
</TelerikChart>
65+
66+
@code {
67+
List<MyChartSeriesViewModel> series { get; set; } = new List<MyChartSeriesViewModel>();
68+
69+
public class MyChartSeriesViewModel
70+
{
71+
public List<object> data { get; set; } = new List<object>();
72+
public string name { get; set; }
73+
}
74+
75+
public string[] xAxisItems = new string[] { "Q1", "Q2", "Q3", "Q4" };
76+
77+
protected override void OnInitialized()
78+
{
79+
base.OnInitialized();
80+
81+
List<object> series1Data = new List<object>() { 10, 2, 5, 6 };
82+
List<object> series2Data = new List<object>() { 5, 8, 2, 7 };
83+
84+
series.Add(new MyChartSeriesViewModel { name = "first", data = series1Data });
85+
series.Add(new MyChartSeriesViewModel { name = "second", data = series2Data });
86+
}
87+
88+
void AddSeries()
89+
{
90+
List<object> series3Data = new List<object>() { 4, 9, 6, 2 };
91+
92+
series.Add(new MyChartSeriesViewModel { name = "third", data = series3Data });
93+
}
94+
}
95+
````
96+

0 commit comments

Comments
 (0)