Line Chart with Pivot Data?

Thread is closed for posting
7 posts, 2 answers
  1. Vinny
    Vinny avatar
    23 posts
    Member since:
    Apr 2007

    Posted 13 May 2008 Link to this post

    Hello,
    I'm trying to create a line chart using pivot data returned from SQL Server. For example. Column 1 is a Product List and columns 2 - 13 represent each month of the year with a sales figure. Can anyone point me to a demo or sample that demonstrates how to get a line chart from this data.  I haven't gotten anywhere on my own and looking over the live demos application hasn't gotten me anywhere.

    I'd like the y axis to be a 1 - 100,000 range and the x axis to represent the months of the year. Then a line representing each of the products. 

    I appreciate the help.

    Thank you,
    Vinny
  2. Ves
    Admin
    Ves avatar
    2926 posts

    Posted 15 May 2008 Link to this post

    Hello Vinny,

    I am afraid RadChart does not support databinding to pivot data. Still, you can populate the chart manually by looping through the data like this:
    for each row (product)
      Create a ChartSeries
      for each cell (field) in this row
        Create a ChartSeriesItem and add it to the above series
      end_inner_for
    end_outer_for
     
    I hope this helps.

    Best wishes,
    Ves
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Vinny
    Vinny avatar
    23 posts
    Member since:
    Apr 2007

    Posted 15 May 2008 Link to this post

    Thank you Ves. would you by chance have a sample app to help me get started or perhaps point me to an appropriate example in the Live demos app?

    Thanks,
    Vinny
  4. Answer
    Ves
    Admin
    Ves avatar
    2926 posts

    Posted 16 May 2008 Link to this post

    Hi Vinny,

    Please, find attached a sample page with RadChart displaying pivot data, using the approach I mentioned. I hope it helps.

    Best wishes,
    Ves
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Vinny
    Vinny avatar
    23 posts
    Member since:
    Apr 2007

    Posted 16 May 2008 Link to this post

    Thank you again Ves. This is very helpful in getting me started.
  6. kollam2003
    kollam2003 avatar
    86 posts
    Member since:
    Nov 2006

    Posted 26 Jun 2008 Link to this post

    will dis work with the2007 radchart??
  7. Answer
    Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 26 Jun 2008 Link to this post

    Hello kollam2003,

    We are unsure which specific version of RadChart you are referring to but generally older versions of the control do not expose the BeforeLayout server-side event, so you should modify the code to handle the PrePaint event instead and you need to manually set the size for the axis item labels as well:

    public partial class _Default : System.Web.UI.Page 
        DataTable tbl; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
     
            tbl = new DataTable(); 
            DataColumn col = new DataColumn("ProductName"); 
            col.DataType = typeof(string); 
            tbl.Columns.Add(col); 
            col = new DataColumn("Jan"); 
            col.DataType = typeof(double); 
            tbl.Columns.Add(col); 
            col = new DataColumn("Feb"); 
            col.DataType = typeof(double); 
            tbl.Columns.Add(col); 
            col = new DataColumn("March"); 
            col.DataType = typeof(double); 
            tbl.Columns.Add(col); 
            tbl.Rows.Add(new object[] { "P1", 2, 19, 11 }); 
            tbl.Rows.Add(new object[] { "P2", 12, 13, 14 }); 
            tbl.Rows.Add(new object[] { "P3", 22, 5, 7 }); 
            tbl.Rows.Add(new object[] { "P4", 9, 6, 2 }); 
     
            foreach (DataRow row in tbl.Rows) 
            { 
                ChartSeries ser = new ChartSeries(row["ProductName"].ToString(), ChartSeriesType.Line); 
     
                for (int i = 1; i < tbl.Columns.Count; i++) 
                { 
                    ChartSeriesItem item = new ChartSeriesItem((double)row[i]); 
                    ser.Items.Add(item); 
                } 
                RadChart1.Series.Add(ser); 
            } 
     
            RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.Dimensions.AutoSize = false
            RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.Dimensions.Height = 20
            RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.Dimensions.Width = 50
        } 
     
        protected void RadChart1_PrePaint(object sender, EventArgs e) 
        { 
            for (int i = 0; i < RadChart1.PlotArea.XAxis.Items.Count; i++) 
            { 
                RadChart1.PlotArea.XAxis.Items[i].TextBlock.Text = tbl.Columns[i + 1].ColumnName; 
            } 
        } 
     



    Regards,
    Manuel
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top