Create a report with chart dynamically

Create a report with chart dynamically

In this tutorial we will learn how to create a report with a chart from code.

A line of SharpShooter Reports products allows adding charts to reports through code. Below you can find a sample code demonstrating how to accomplish this task:

...

Chart chart = new Chart();

chart.Name = "chart1";

//Creating ChartArea

ChartArea chartArea = new ChartArea();

chartArea.Name = "chartArea1";

chartArea.Dock = ChartAreaDockStyle.Client;

chart.Elements.Add(chartArea);

//Creating Series

Series series = new Series();

series.Name = "series1";

series.AutoArgument = false;

series.View = new PieSeriesView

{

      Fill = new SolidFill(Color.FromArgb(190, Color.BlueViolet))

};

series.Stroke = new SimpleStroke

{

      Color = Color.Black,             

      Width = 2

};

series.DataSource = "Products";

series.Bindings.Add(new SeriesBinding("Argument", "ProductName"));

series.Bindings.Add(new SeriesBinding("Value", "UnitPrice"));

chartArea.Elements.Add(series);

//Creating ValueLabels

ValueLabels valueLabels = new ValueLabels();

valueLabels.Name = "valueLabels1";

valueLabels.Fill = new SolidFill(Color.White);

valueLabels.Stroke = new SimpleStroke

{

    Color = Color.Black,

    Width = 2

};

valueLabels.Font = new FontDescriptor(new Font("Microsoft Sans Serif", 10));

valueLabels.TextFill = new SolidFill(Color.Black);

series.Elements.Add(valueLabels);

//Creating Axis X

Axis axisX = new Axis();

axisX.Name = "AxisX";

axisX.IsDiscrete = true;

chartArea.Elements.Add(axisX);

//Adding labels to X axis

ScaleLabels scaleLabelsX = new ScaleLabels();

scaleLabelsX.Name = "ScaleLabelsX";

scaleLabelsX.TextRotationMode = TextRotationMode.Screen;

scaleLabelsX.Padding = 20;

scaleLabelsX.Dock = ScaleDocking.Outside;

scaleLabelsX.Font = new FontDescriptor(new Font("Microsoft Sans Serif", 10));

axisX.Elements.Add(scaleLabelsX);

//Creating Axis Y

Axis axisY = new Axis();

axisY.Name = "AxisY";

axisY.Direction = AxisDirection.Y;

axisX.IsDiscrete = true;

chartArea.Elements.Add(axisY);

// Creating ScaleLables for AxisY

ScaleLabels scaleLabelsY = new ScaleLabels();

scaleLabelsY.Name = "ScaleLabelsY";

scaleLabelsY.Dock = ScaleDocking.Outside;

scaleLabelsY.Distance = -27;

scaleLabelsY.Font = new FontDescriptor(new Font("Microsoft Sans Serif", 10));

axisY.Elements.Add(scaleLabelsY);

ChartControl chartControl = new ChartControl();

chartControl.Name = "chartControl1";          

chartControl.Chart = chart;

chartControl.DataSource = "Products";

chartControl.Size = new Vector(PerpetuumSoft.Framework.Drawing.Unit.ToInternalUnit(620, PerpetuumSoft.Framework.Drawing.Unit.Pixel), PerpetuumSoft.Framework.Drawing.Unit.ToInternalUnit(396, PerpetuumSoft.Framework.Drawing.Unit.Pixel));

page.Controls.Add(chartControl);


Here you can download a full source code of the sample:

http://perpetuumsoft.com/Support/RSS/DynamicPieChart.zip

Add Feedback