RadChart for ASP.NET

Data Binding RadChart to a DataReader Send comments on this topic.
See Also
Building RadCharts > Data Binding RadChart to a DataReader

Glossary Item Box

You can extract the values from data source in a DataReader instance (calling the ExecuteReader() method for your OleDbCommand/SqlCommand command). That DataReader can be used for Telerik RadChart source - all you need to do is set this instance to the DataSource property of the control. The example below shows a chart that displays the total sales by category for 1997 from Northwind database.

VB Copy Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim command As New SqlCommand()
        command.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString)
        command.CommandText = "SELECT CategoryName, SUM(ProductSales) AS TotalSales FROM [Product Sales for 1997] GROUP BY CategoryName"
        command.Connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
        RadChart1.PlotArea.XAxis.DataLabelsColumn = "CategoryName"
        RadChart1.DataSource = reader
        RadChart1.DataBind()
        RadChart1.PlotArea.Appearance.Dimensions.Margins.Left = Telerik.Charting.Styles.Unit.Percentage(16)
        RadChart1.PlotArea.Appearance.Dimensions.Margins.Bottom = Telerik.Charting.Styles.Unit.Percentage(24)
        RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = 45
        RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top
    End Sub
C# Copy Code
protected void Page_Load(object sender, EventArgs e)
   {
       SqlCommand command =
new SqlCommand();
       command.Connection =
new SqlConnection(
       ConfigurationManager.ConnectionStrings[
"NorthwindConnectionString"].ConnectionString);
       command.CommandText =
"SELECT CategoryName, SUM(ProductSales) AS TotalSales FROM [Product Sales for 1997] GROUP BY CategoryName";
       command.Connection.Open();
       SqlDataReader reader = command.ExecuteReader();
       RadChart1.PlotArea.XAxis.DataLabelsColumn =
"CategoryName";
       RadChart1.DataSource = reader;
       RadChart1.DataBind();
       RadChart1.PlotArea.Appearance.Dimensions.Margins.Left = Telerik.Charting.Styles.Unit.Percentage(16);
       RadChart1.PlotArea.Appearance.Dimensions.Margins.Bottom = Telerik.Charting.Styles.Unit.Percentage(24);
       RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = 45;
       RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top;
   }

See Also