Series names for Legend Gantt Chart

Thread is closed for posting
2 posts, 0 answers
  1. Craig Campbell
    Craig Campbell avatar
    1 posts
    Member since:
    Sep 2009

    Posted 18 Sep 2009 Link to this post

    void RadChart1_ItemDataBound(object sender, ChartItemDataBoundEventArgs e)  
        {  
            DataRowView drv = e.DataItem as DataRowView;  
            DateTime startDate = (DateTime)drv["EventDate"];  
            DateTime endDate = (DateTime)drv["EndDate"];  
            StringBuilder sb = new StringBuilder();  
            sb.Append(drv["Title"].ToString());  
            sb.AppendFormat("\n{0} - {1}", startDate.ToShortDateString(), endDate.ToShortDateString());  
            e.SeriesItem.Name = drv["Source"].ToString();  
            e.SeriesItem.ActiveRegion.Tooltip = sb.ToString();  
              
        } 
    I'm struggling getting the series names to show up properly in the legend. All I get is 'Series xx' and I'm sure I'm overlooking something very simple, or at least I hope so.

    Basic theory: I'm grabbing list items from various SharePoint lists via Lists Web service, converting the XmlNodes into DataSets, doing a little manipulation to the data, then create a DataTable with all List items.
    DataTable schema looks like:
    dt.Columns.Add("Source"typeof(String));  
    dt.Columns.Add("Title"typeof(string));  
    dt.Columns.Add("From"typeof(double));  
    dt.Columns.Add("To"typeof(double));  
    dt.Columns.Add("EventDate"typeof(DateTime));  
    dt.Columns.Add("EndDate"typeof(DateTime)); 

    For each Source, I want it to show up in different colors for the Gantt chart and display the name so the user knows which list it came from.

    Snippet for chart:
    ChartSeries series = new ChartSeries();  
    RadChart1.PlotArea.YAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortDate;  
                      
            RadChart1.PlotArea.YAxis.Appearance.LabelAppearance.RotationAngle = 45;  
            RadChart1.PlotArea.YAxis.LabelStep = 1;  
            RadChart1.PlotArea.YAxis.Appearance.LabelAppearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top;  
            RadChart1.Series.Clear();  
            RadChart1.PlotArea.YAxis.AutoScale = false;  
            RadChart1.PlotArea.YAxis.AxisMode = ChartYAxisMode.Extended;  
     
            DataTable dt = getListItems();  
     
            RadChart1.PlotArea.YAxis.AddRange(from.ToOADate(), to.ToOADate(), step);  
            RadChart1.DataSource = dt;  
     
            series.Type = ChartSeriesType.Gantt;  
            series.Appearance.LabelAppearance.Visible = false;  
            series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.SeriesName;  
            series.Appearance.BarWidthPercent = 30M;  
            series.DataYColumn = "From";  
            series.DataYColumn2 = "To";  
            RadChart1.Series.Add(series);  
            RadChart1.SeriesOrientation = ChartSeriesOrientation.Horizontal;  
            RadChart1.ChartTitle.TextBlock.Text = "Aggregated Calendar";  
            if(_realWidth > 0)  
                RadChart1.Width = new Unit(_realWidth * .97);  
    RadChart1.DataBind(); 

    Here is the ItemDataBound event:
    void RadChart1_ItemDataBound(object sender, ChartItemDataBoundEventArgs e)     
        {     
            DataRowView drv = e.DataItem as DataRowView;     
            DateTime startDate = (DateTime)drv["EventDate"];     
            DateTime endDate = (DateTime)drv["EndDate"];     
            StringBuilder sb = new StringBuilder();     
            sb.Append(drv["Title"].ToString());     
            sb.AppendFormat("\n{0} - {1}", startDate.ToShortDateString(), endDate.ToShortDateString());     
            e.SeriesItem.Name = drv["Source"].ToString();     
            e.SeriesItem.ActiveRegion.Tooltip = sb.ToString();     
                 
        }    
     

    I would expect to be able to set the column that is being used for the Series like
    series.DataSeriesColumn = "Source"; //DataTable column name 
    similar to how you specify the DataYColumn, DataYColumn2, etc.

    Surely, I don't have to create new ChartSeries objects for every list I aggregate? Any help would be appreciated.
  2. Ves
    Admin
    Ves avatar
    2927 posts

    Posted 23 Sep 2009 Link to this post

    Hi Craig,

    This line:

    series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.SeriesName; 


    forces RadChart to display a single entry in the legend for every series. Your code shows a single chart series and its Name property is never set, so you get the default "Series xx". As you will need a legend item for every ChartSeriesItem (i.e. every row in the table), I would suggest changing the above line to:

    series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels;

    As each ChartSeriesItem's Name property is set in the ItemDataBound event handler, you will get the desired text. Still, the default behavior is to show all the items in the series in the same color. You can add some additional code in the ItemDataBound event handler to set individual colors for bars as shown here.

    Best regards,
    Ves
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top