Telerik blogs

Imagine you have a databound RadChart and you need to customize the item labels, say you need to combine the values of two fields into a single item label. All you need to do is wire the ItemDataBound event, retrieve the ChartSeriesItem and customize it as per your requirements. You may use the actual data for this, here is an example:

 

protected void RadChart1_ItemDataBound(object sender, ChartItemDataBoundEventArgs e)
   
{
       
ChartSeriesItem item = e.SeriesItem;
       
DataRowView dataItem = (DataRowView)e.DataItem;
       
item.Label.TextBlock.Text = dataItem["FirstName"].ToString() + " " + dataItem["LastName"].ToString();
   
}

 

That was simple, but what about auto-generated axis items? OK, let’s start with some background – RadChart allows you to modify it at any stage of the page lifecycle, including events fired as late as Page_PreRenderComplete. And still its axes will be auto-scaled showing the range needed for the data being shown. So, how to take advantage of RadChart’s smart axes, but customize their items? Another event comes in handy – BeforeLayout. It is fired after the axis items are created, but before the start of the layout calculations, so the following code will produce Y axis labels with alternating size:

 

protected void RadChart1_BeforeLayout(object sender, EventArgs e)
   
{
       
for (int i = 0; i < RadChart1.PlotArea.YAxis.Items.Count; i++)
       
{
           
RadChart1.PlotArea.YAxis.Items[i].TextBlock.Appearance.TextProperties.Font = new System.Drawing.Font("verdana", i%2==0?8:12);
       
}
   
}

Finally – yes, you can use the same approach for the auto-generated legend items :-)

Hope you have fun customizing RadChart.


Comments

Comments are disabled in preview mode.