Telerik Reporting Q1 2016

Creating Chart Programmatically - more complex example

Note

This article is obsolete. The Chart item is now superseded by the more advanced Graph item. The Graph item is most often used for building powerful OLAP/Pivot charts.

Programmatically Created Chart

The figure above shows a more complex chart than the minimal example of programmatically created Chart.  This example demonstrates how to:

  • Create the chart, define the chart title and the title's appearance.
  • Define two different chart series, how to hide the series labels and tailor the series appearance.
  • Add multiple chart series items with data.
  • How to display point marks to highlight the data points.
  • Fill the plot area background with a gradient color.
  • Set axis line and label appearance.
Note

The example below is an alternative to using the Report Designer for creating a chart. The approach of hardcoding data in the series is for the sake of the example and would produce a static chart. This code should not be used for programmatic creation of dynamic charts based on data, for such scenario, see How to: Programmatically Data Binding Chart to a Generic List of Objects.

CopyC#
// Define chart and title
Telerik.Reporting.Chart Chart = new Telerik.Reporting.Chart();
Chart.BitmapResolution = 96F;
Chart.ImageFormat = System.Drawing.Imaging.ImageFormat.Emf;
Chart.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(1.8, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))));
Chart.Name = "progchart1";
Chart.PlotArea.EmptySeriesMessage.Appearance.Visible = true;
Chart.PlotArea.EmptySeriesMessage.Visible = true;
Chart.PlotArea.XAxis.AxisLabel.Visible = true;
Chart.PlotArea.YAxis.AxisLabel.Visible = true;
Chart.Size = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(9.6, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))), new Telerik.Reporting.Drawing.Unit(6.1, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))));
Chart.ChartTitle.TextBlock.Text = "My Chart";
Chart.ChartTitle.TextBlock.Appearance.TextProperties.Color =
System.Drawing.Color.Blue;
// Define chart series
ChartSeries chartSeries = new ChartSeries();
chartSeries.Appearance.LabelAppearance.Visible = false;
chartSeries.Name = "GDP";
chartSeries.Type = ChartSeriesType.Line;
chartSeries.Appearance.LineSeriesAppearance.Color =
System.Drawing.Color.BlueViolet;
// Define the items in the series
chartSeries.AddItem(1);
chartSeries.AddItem(1.5);
chartSeries.AddItem(2.0);
chartSeries.AddItem(2.5);
chartSeries.AddItem(3.5);
// visually enhance the data points
chartSeries.Appearance.PointMark.Dimensions.Width = 5;
chartSeries.Appearance.PointMark.Dimensions.Height = 5;
chartSeries.Appearance.PointMark.FillStyle.MainColor =
System.Drawing.Color.Black;
chartSeries.Appearance.PointMark.Visible = true;
// Define chart series
ChartSeries chartSeries2 = new ChartSeries();
chartSeries2.Appearance.LabelAppearance.Visible = false;
chartSeries2.Name = "GNP";
chartSeries2.Type = ChartSeriesType.Line;
chartSeries2.Appearance.LineSeriesAppearance.Color =
System.Drawing.Color.Green;
// Define the items in the series
chartSeries2.AddItem(2);
chartSeries2.AddItem(3);
chartSeries2.AddItem(3.5);
chartSeries2.AddItem(4);
chartSeries2.AddItem(4.5);
// visually enhance the data points
chartSeries2.Appearance.PointMark.Dimensions.Width = 5;
chartSeries2.Appearance.PointMark.Dimensions.Height = 5;
chartSeries2.Appearance.PointMark.FillStyle.MainColor =
System.Drawing.Color.Black;
chartSeries2.Appearance.PointMark.Visible = true;
// set the plot area gradient background fill
Chart.PlotArea.Appearance.FillStyle.FillType =
Telerik.Reporting.Charting.Styles.FillType.Gradient;
Chart.PlotArea.Appearance.FillStyle.MainColor =
System.Drawing.Color.FromArgb(65, 201, 254);
Chart.PlotArea.Appearance.FillStyle.SecondColor =
System.Drawing.Color.FromArgb(0, 107, 186);
// Set text and line for X axis
Chart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Years";
Chart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color =
System.Drawing.Color.Red;
Chart.PlotArea.XAxis.Appearance.Width = 3;
Chart.PlotArea.XAxis.Appearance.Color = System.Drawing.Color.Red;
// Set text and line for Y axis
Chart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "%";
Chart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color =
System.Drawing.Color.Red;
Chart.PlotArea.YAxis.Appearance.Width = 3;
Chart.PlotArea.YAxis.Appearance.Color = System.Drawing.Color.Red;
// Add the series to the chart, chart to page.
Chart.Series.Add(chartSeries);
Chart.Series.Add(chartSeries2);

this.detail.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { Chart });
CopyVB.NET
' Define chart and title
Dim Chart As New Telerik.Reporting.Chart()
Chart.BitmapResolution = 96.0F
Chart.ImageFormat = System.Drawing.Imaging.ImageFormat.Emf
Chart.Location = New Telerik.Reporting.Drawing.PointU(New Telerik.Reporting.Drawing.Unit(0.32, (DirectCast((Telerik.Reporting.Drawing.UnitType.Cm), Telerik.Reporting.Drawing.UnitType))), New Telerik.Reporting.Drawing.Unit(1.8, (DirectCast((Telerik.Reporting.Drawing.UnitType.Cm), Telerik.Reporting.Drawing.UnitType))))
Chart.Name = "progchart1"
Chart.PlotArea.EmptySeriesMessage.Appearance.Visible = True
Chart.PlotArea.EmptySeriesMessage.Visible = True
Chart.PlotArea.XAxis.AxisLabel.Visible = True
Chart.PlotArea.YAxis.AxisLabel.Visible = True
Chart.Size = New Telerik.Reporting.Drawing.SizeU(New Telerik.Reporting.Drawing.Unit(9.6, (DirectCast((Telerik.Reporting.Drawing.UnitType.Cm), Telerik.Reporting.Drawing.UnitType))), New Telerik.Reporting.Drawing.Unit(6.1, (DirectCast((Telerik.Reporting.Drawing.UnitType.Cm), Telerik.Reporting.Drawing.UnitType))))
Chart.ChartTitle.TextBlock.Text = "My Chart"
Chart.ChartTitle.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Blue
' Define chart series
Dim chartSeries As New ChartSeries()
chartSeries.Appearance.LabelAppearance.Visible = False
chartSeries.Name = "GDP"
chartSeries.Type = ChartSeriesType.Line
chartSeries.Appearance.LineSeriesAppearance.Color = System.Drawing.Color.BlueViolet
' Define the items in the series
chartSeries.AddItem(1)
chartSeries.AddItem(1.5)
chartSeries.AddItem(2)
chartSeries.AddItem(2.5)
chartSeries.AddItem(3.5)
' visually enhance the data points
chartSeries.Appearance.PointMark.Dimensions.Width = 5
chartSeries.Appearance.PointMark.Dimensions.Height = 5
chartSeries.Appearance.PointMark.FillStyle.MainColor = System.Drawing.Color.Black
chartSeries.Appearance.PointMark.Visible = True
' Define chart series
Dim chartSeries2 As New ChartSeries()
chartSeries2.Appearance.LabelAppearance.Visible = False
chartSeries2.Name = "GNP"
chartSeries2.Type = ChartSeriesType.Line
chartSeries2.Appearance.LineSeriesAppearance.Color = System.Drawing.Color.Green
' Define the items in the series
chartSeries2.AddItem(2)
chartSeries2.AddItem(3)
chartSeries2.AddItem(3.5)
chartSeries2.AddItem(4)
chartSeries2.AddItem(4.5)
' visually enhance the data points
chartSeries2.Appearance.PointMark.Dimensions.Width = 5
chartSeries2.Appearance.PointMark.Dimensions.Height = 5
chartSeries2.Appearance.PointMark.FillStyle.MainColor = System.Drawing.Color.Black
chartSeries2.Appearance.PointMark.Visible = True
' set the plot area gradient background fill
Chart.PlotArea.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Gradient
Chart.PlotArea.Appearance.FillStyle.MainColor = System.Drawing.Color.FromArgb(65, 201, 254)
Chart.PlotArea.Appearance.FillStyle.SecondColor = System.Drawing.Color.FromArgb(0, 107, 186)
' Set text and line for X axis
Chart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Years"
Chart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red
Chart.PlotArea.XAxis.Appearance.Width = 3
Chart.PlotArea.XAxis.Appearance.Color = System.Drawing.Color.Red
' Set text and line for Y axis
Chart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "%"
Chart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red
Chart.PlotArea.YAxis.Appearance.Width = 3
Chart.PlotArea.YAxis.Appearance.Color = System.Drawing.Color.Red
' Add the series to the chart, chart to page.
Chart.Series.Add(chartSeries)
Chart.Series.Add(chartSeries2)

Me.detail.Items.AddRange(New Telerik.Reporting.ReportItemBase() {Chart})

See Also