This is a migrated thread and some comments may be shown as answers.

Series names for Legend Gantt Chart

1 Answer 98 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Craig Campbell
Top achievements
Rank 1
Craig Campbell asked on 18 Sep 2009, 06:52 PM
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.

1 Answer, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 23 Sep 2009, 08:17 AM
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.
Tags
Chart (Obsolete)
Asked by
Craig Campbell
Top achievements
Rank 1
Answers by
Ves
Telerik team
Share this question
or