ID-100106943Jim Rohn stated “Don’t wish it were easier, wish you were better.” Yet, I find myself resilient in desiring for things not only to be easier but also for them to be enjoyable. It is not my goal to challenge Jim Rohn wisdom, however, I believe I will adopt a similar principle “Don’t wish it were easier, wish it be enjoyable.”

In my pilgrimage as a software developer I find excitement when stumbling upon an Application Programming Interface (API) that make application development fun and enjoyable. In a previous post, I had the opportunity to show a brief task of creating a Telerik Report table. In this post, I take on a chart creation at run time. This area is one of the most enjoyable of the Teleirk Repoting API.

Like in any charting API available, it is imperative to understand the objects or components that bring about a chart object. In the section below, I attempt to explain a few chart components, although, within the context of the Telerik Report API. Understanding these will save up a substantial amount of time, this principle holds true for any API we may encounter.

Anatomy of Telerik.Reporting.Chart

 

chart_basics003

  • Telerik.Reporting.ChartSeries: It is the element that contains a collection of series item to represent a logical group of data. In our report I will be defined as the product name or category.

 

  • Telerik.Reporting.ChartSeriesItem: It is the isolated or or single element within a ChartSeries object; thus a ChartSeries is a collection of ChartSeriesItem.

 

  • Telerik.Reporting.PlotArea: In lame terms, this is the chart container. This is the area that will be inhabit by the ChartSeries and all the object that involve the graphical representation of data.

 

  • Telerik.Reporting.PlotArea.XAxis: It is the object that has control over the xaxis look and feel and scale features in the chart.

 

  • Telerik.Reporting.PlotArea.YAxis: Similar to the XAxis, It is the object that has control over the yaxis look and feel and scale features.

 

  • Telerik.Reporting.Legend: This contain the legend items that aid in giving interpratation to the ChartSeries or the ChartSeriesItems.

 

  • Telerik.Reporting.LabelItem: It is a single element within a legen. Thus a legend is a collection of LableItems.

 

Telerik Report Chart at Run Time

Telerik Charts poses a group of  Built-in skins for their controls. The same apply for Telerik Reports. A good strategy to customize the chart look and feel is to apply a skin as a starting point. We will apply the skin subsequently to the declaration of the chart object.  In addition, Similar to all the Report objects, We must define the size of the chart and its location.

 

   1: Telerik.Reporting.Chart mychart = new Telerik.Reporting.Chart();
   2: mychart.Skin = "Office2007";
   3: Telerik.Reporting.Drawing.Unit Height = new Telerik.Reporting.Drawing.Unit(8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))); 
   4: Telerik.Reporting.Drawing.Unit Width = new Telerik.Reporting.Drawing.Unit(12, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm)));
   5: mychart.Size = new Telerik.Reporting.Drawing.SizeU(Width, Height);
   6: mychart.Location = new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(0.32, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))), new Telerik.Reporting.Drawing.Unit(0.8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))));

Follow the next link, in order to obtain a complete listing of Telerik Built-in Skins.

Our next step is to define the xaxis and yaxis for our user:

   1: mychart.PlotArea.XAxis.AxisLabel.Visible = true;
   2: mychart.PlotArea.YAxis.AxisLabel.Visible = true;
   3:  
   4: mychart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Month";
   5: mychart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "Sales";
   6: mychart.PlotArea.XAxis.AxisLabel.Appearance.Position.Auto = true;
   7: mychart.PlotArea.YAxis.AxisLabel.Appearance.Position.Auto = true;

 

In the next section we use a data source similar to the one used in the Telerik Reporting Post to defined our ChartSeriesItems. For simplicity, we would assign a sequential value to the xvalue.

   1: ChartSeries series= new ChartSeries();
   2: int seriesItemPosition = 0;
   3: mychart.ChartTitle.TextBlock.Text = "Product Sale Report";
   4: foreach (Product prd in _source.OrderBy(p => p.Date).Where(n => n.ProductName == "Product 1").Select(m => m).ToList<Product>())
   5: {
   6:     ChartSeriesItem seriesItem = new ChartSeriesItem();
   7:     seriesItem.YValue = prd.sale;
   8:     seriesItem.XValue = seriesItemPosition++;
   9:     seriesItem.Appearance.FillStyle.MainColor = seriesColor;
  10:     series.Items.Add(seriesItem);
  11: }
  12: series.Appearance.FillStyle.MainColor = seriesColor;
  13: series.Name = "Product 1";

 

We complete our task by adding the series to the chart object and defining its type:

   1: series.Type = ChartSeriesType.Bar;
   2: mychart.Series.Add(series);

 

Simple enough, we will have the result bar chart:

Chart1

Here is the Complete Source:

public static Telerik.Reporting.Chart GetCharObject(List<Product> _source)
{
    #region Defining Colors
    System.Drawing.Color seriesColor =  System.Drawing.Color.LightBlue;
    #endregion
 
    Telerik.Reporting.Chart mychart = new Telerik.Reporting.Chart();
    mychart.Skin = "Office2007";
   
    #region XAxis and YAxis Labels
    mychart.PlotArea.XAxis.AxisLabel.Visible = true;
    mychart.PlotArea.YAxis.AxisLabel.Visible = true;
    mychart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Month";
    mychart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "Sales";
    mychart.PlotArea.XAxis.AxisLabel.Appearance.Position.Auto = true;
    mychart.PlotArea.YAxis.AxisLabel.Appearance.Position.Auto = true;
    #endregion
    ChartSeries series= new ChartSeries();
    int seriesItemPosition = 0;
    mychart.ChartTitle.TextBlock.Text = "Product Sale Report";
    foreach (Product prd in _source.OrderBy(p => p.Date).Where(n => n.ProductName == "Product 1").Select(m => m).ToList<Product>())
    {
                    ChartSeriesItem seriesItem = new ChartSeriesItem();
                    seriesItem.YValue = prd.sale;
                    seriesItem.XValue = seriesItemPosition++;
                    seriesItem.Appearance.FillStyle.MainColor = seriesColor;
                    series.Items.Add(seriesItem);
    }
    series.Appearance.FillStyle.MainColor = seriesColor;
    series.Name = "Product 1";
    series.Type = ChartSeriesType.Bar;
    mychart.Series.Add(series);
 
    Telerik.Reporting.Drawing.Unit Height = new Telerik.Reporting.Drawing.Unit(8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))); 
    Telerik.Reporting.Drawing.Unit Width = new Telerik.Reporting.Drawing.Unit(12, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm)));
    mychart.Size = new Telerik.Reporting.Drawing.SizeU(Width, Height);
    mychart.Location = new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(0.32, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))), new Telerik.Reporting.Drawing.Unit(0.8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))));
 
    return mychart;
}

 

Eliminate Series Item Value Label and Gridlines

In order to eliminate the gridlines in the chart view, we must hide the visibility of the gridlines in the PlotArea.YAxis Object. Whereas to hide the value labels on the chart we must apply the change to the ChartSeriesItem

...
seriesItem.Label.TextBlock.Visible = false;
...
mychart.PlotArea.YAxis.Appearance.MinorGridLines.Visible = false;
mychart.PlotArea.YAxis.Appearance.MajorGridLines.Visible = false;

Below is the resulted chart:

Chart2

Differentiating within a Chart Series with Custom Legend

There are instances in which we may desire to make a distinction within a chart series. In the scenario to illustrate, we display a different series item color based on the sale value. Here is how the modifications would apply:

//colors
#region Defining Colors
    System.Drawing.Color seriesColor = System.Drawing.Color.LightBlue;
    System.Drawing.Color SeriesColorGreaterThan600 = System.Drawing.Color.Green;
#endregion
 
//Apply Color Based on Value
foreach (Product prd in _source.OrderBy(p => p.Date).Where(n => n.ProductName == "Product 1").Select(m => m).ToList<Product>())
{
    ChartSeriesItem seriesItem = new ChartSeriesItem();
    seriesItem.YValue = prd.sale;
    seriesItem.XValue = seriesItemPosition++;
    seriesItem.Appearance.FillStyle.MainColor = (prd.sale > 600) ? SeriesColorGreaterThan600 : seriesColor;
    // Hide the Graph Values on Series Item
    seriesItem.Label.TextBlock.Visible = false;
    series.Items.Add(seriesItem);
}
 

 

Once we have apply a different color, we moved to create the custom legend. The first step to create a Legend is to disable the Default one. This could be accomplish by changing the Legend Display Mode, then we proceed to add the labels in the following fashion:

series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.Nothing;
// First Label:
LabelItem LegenItem = new LabelItem();
LegenItem.Marker.Visible = true;
LegenItem.Marker.Appearance.Figure = Telerik.Reporting.Charting.Styles.DefaultFigures.Rectangle;
LegenItem.Marker.Appearance.FillStyle.MainColor = seriesColor;
LegenItem.Marker.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
LegenItem.TextBlock.Text = " Sale < 600";
// Second Label
LabelItem LegenItem2 = new LabelItem();
LegenItem2.Marker.Visible = true;
LegenItem2.Marker.Appearance.Figure = Telerik.Reporting.Charting.Styles.DefaultFigures.Rectangle;
LegenItem2.Marker.Appearance.FillStyle.MainColor = SeriesColorGreaterThan600;
LegenItem2.Marker.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
LegenItem2.TextBlock.Text = " Sale > 600";
// Legend Position
mychart.Legend.Appearance.Location = Telerik.Reporting.Charting.Styles.LabelLocation.OutsidePlotArea;
mychart.Legend.Appearance.Overflow = Telerik.Reporting.Charting.Styles.Overflow.Column;
mychart.Legend.Appearance.Position.AlignedPosition = Telerik.Reporting.Charting.Styles.AlignedPositions.Right;

Below is the resulted chart

 

Chart3

Here is the complete Source

   1: #region Defining Colors
   2:   System.Drawing.Color seriesColor = System.Drawing.Color.LightBlue;
   3:   System.Drawing.Color SeriesColorGreaterThan600 = System.Drawing.Color.Green;
   4:   #endregion
   5:  
   6:   Telerik.Reporting.Chart mychart = new Telerik.Reporting.Chart();
   7:   mychart.Skin = "Office2007";
   8:   #region XAxis and YAxis Labels
   9:       mychart.PlotArea.XAxis.AxisLabel.Visible = true;
  10:       mychart.PlotArea.YAxis.AxisLabel.Visible = true;
  11:       mychart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Month";
  12:       mychart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "Sales";
  13:       mychart.PlotArea.XAxis.AxisLabel.Appearance.Position.Auto = true;
  14:       mychart.PlotArea.YAxis.AxisLabel.Appearance.Position.Auto = true;
  15:   #endregion
  16:   ChartSeries series = new ChartSeries();
  17:   int seriesItemPosition = 0;
  18:   mychart.ChartTitle.TextBlock.Text = "Product Sale Report";
  19:   foreach (Product prd in _source.OrderBy(p => p.Date).Where(n => n.ProductName == "Product 1").Select(m => m).ToList<Product>())
  20:   {
  21:       ChartSeriesItem seriesItem = new ChartSeriesItem();
  22:       seriesItem.YValue = prd.sale;
  23:       seriesItem.XValue = seriesItemPosition++;
  24:       seriesItem.Appearance.FillStyle.MainColor = (prd.sale > 600) ? SeriesColorGreaterThan600 : seriesColor;
  25:       // Hide the Graph Values on Series Item
  26:       seriesItem.Label.TextBlock.Visible = false;
  27:       series.Items.Add(seriesItem);
  28:   }
  29:   #region Custom Legends
  30:   LabelItem LegenItem = new LabelItem();
  31:   LegenItem.Marker.Visible = true;
  32:   LegenItem.Marker.Appearance.Figure = Telerik.Reporting.Charting.Styles.DefaultFigures.Rectangle;
  33:   LegenItem.Marker.Appearance.FillStyle.MainColor = seriesColor;
  34:   LegenItem.Marker.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
  35:   LegenItem.TextBlock.Text = " Sale < 600";
  36:  
  37:   LabelItem LegenItem2 = new LabelItem();
  38:   LegenItem2.Marker.Visible = true;
  39:   LegenItem2.Marker.Appearance.Figure = Telerik.Reporting.Charting.Styles.DefaultFigures.Rectangle;
  40:   LegenItem2.Marker.Appearance.FillStyle.MainColor = SeriesColorGreaterThan600;
  41:   LegenItem2.Marker.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
  42:   LegenItem2.TextBlock.Text = " Sale > 600";
  43:  
  44:  
  45:   mychart.Legend.Items.Add(LegenItem);
  46:   mychart.Legend.Items.Add(LegenItem2);
  47:  
  48:       #region Legend Configuration
  49:            mychart.Legend.Appearance.Location = Telerik.Reporting.Charting.Styles.LabelLocation.OutsidePlotArea;
  50:            mychart.Legend.Appearance.Overflow = Telerik.Reporting.Charting.Styles.Overflow.Column;
  51:            mychart.Legend.Appearance.Position.AlignedPosition = Telerik.Reporting.Charting.Styles.AlignedPositions.Right;
  52:       #endregion
  53:   #endregion
  54:  
  55:   series.Appearance.FillStyle.MainColor = seriesColor;
  56:   series.Name = "Product 1";
  57:   series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.Nothing;
  58:   series.Type = ChartSeriesType.Bar;
  59:   mychart.Series.Add(series);
  60:  
  61:   #region Eliminate Gridlines
  62:   mychart.PlotArea.YAxis.Appearance.MajorGridLines.Visible = false;
  63:   mychart.PlotArea.YAxis.Appearance.MinorGridLines.Visible = false;
  64:   #endregion
  65:  
  66:   Telerik.Reporting.Drawing.Unit Height = new Telerik.Reporting.Drawing.Unit(8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm)));
  67:   Telerik.Reporting.Drawing.Unit Width = new Telerik.Reporting.Drawing.Unit(12, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm)));
  68:   mychart.PlotArea.XAxis.AutoShrink = false;
  69:   mychart.Size = new Telerik.Reporting.Drawing.SizeU(Width, Height);
  70:   mychart.Location = new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(0.32, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))), new Telerik.Reporting.Drawing.Unit(0.8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))));