Creating RadChart Programmatically
The steps below show how to create a minimal RadChart programmatically.
See the "create a more complex chart programmatically" topic to see how multiple series are created and how appearance can be tailored at run-time.
See the topic "Multiple Chart Types in a Single Chart" to see how multiple series area created programmatically and given different ChartSeriesTypes.
Once the chart is created, the critical steps are creating the ChartSeries and ChartSeriesItem collections. There are two approaches to creating chart series objects. One is to use the default ChartSeries constructor and assign its properties. A second route is to use the RadChart CreateSeries() method to set a number of important properties at once and return the constructed chart series.
1. First add the namespaces that support the objects to be referenced. The Telerik.WebWinControls.UI namespace supports the RadChart declaration and the Telerik.Charting namespace supports the other RadChart objects, e.g. ChartSeries and ChartSeriesItem.
using Telerik.WinControls.UI; using Telerik.Charting;
Imports Telerik.WinControls.UI Imports Telerik.Charting
2. Next construct the RadChart itself. To the RadChart instance, assign the chart title using the ChartTitle.TextBlock.Text property.
RadChart radChart = new RadChart(); radChart.ChartTitle.TextBlock.Text = "My RadChart"; this.Controls.Add(radChart);
Dim RadChart1 As New RadChart() RadChart1.ChartTitle.TextBlock.Text = "My RadChart" Me.Controls.Add(RadChart1)
3. Construct a new ChartSeries object. Assign a name to the ChartSeries. Set the ChartSeries.Type to be Bar. Using the ChartSeries.AddItem() method, add a series of ChartSeriesItem objects to the series Items collection. AddItem takes as parameters a double "Value" and a string "Label".
// Create a ChartSeries and assign its name and chart type Telerik.Charting.ChartSeries chartSeries = new Telerik.Charting.ChartSeries(); chartSeries.Name = "Sales"; chartSeries.Type = ChartSeriesType.Bar; // add new items to the series, // passing a value and a label string chartSeries.AddItem(120, "Internet"); chartSeries.AddItem(140, "Retail"); chartSeries.AddItem(35, "Wholesale");
' Create a ChartSeries and assign its name and chart type Dim chartSeries As New Telerik.Charting.ChartSeries() chartSeries.Name = "Sales" chartSeries.Type = ChartSeriesType.Bar ' add new items to the series, ' passing a value and a label string chartSeries.AddItem(120, "Internet") chartSeries.AddItem(140, "Retail") chartSeries.AddItem(35, "Wholesale")
4. Finally, add the ChartSeries to the RadChart. Series collection and add the RadChart to the page.
' add the series to the RadChart Series collection RadChart1.Series.Add(chartSeries)
5. The finished chart in the running project should look like this example:
The alternative to using the ChartSeries object constructor and assigning properties is to use the RadChart CreateSeries() method that lets you pass several properties in the call, including Name, MainColor, SecondColor and ChartSeriesType.
Telerik.Charting.ChartSeries chartSeries1 = radChart.CreateSeries("Sales", System.Drawing.Color.RoyalBlue, System.Drawing.Color.LightSteelBlue, ChartSeriesType.Bar);
Dim chartSeries1 As Telerik.Charting.ChartSeries = RadChart1.CreateSeries("Sales", System.Drawing.Color.RoyalBlue, System.Drawing.Color.LightSteelBlue, ChartSeriesType.Bar)