.Net Links

Monday, July 20, 2009

Simple Webchart Control using Devexpress

Below is the code for used for displaying charts using devexpress webchart controls.


protected void Page_Load(object sender, EventArgs e)
{
// Create a Webchart.
WebChartControl Chartppt = new WebChartControl();

// Generate a data table and bind the chart to it.
Chartppt.DataSource = PPTData();

//Specify the Chart Title

ChartTitle ChrtTtleppt = new ChartTitle();
ChrtTtleppt.Text = "Purchase";
ChrtTtleppt.Alignment = System.Drawing.StringAlignment.Center;
Chartppt.Titles.Add(ChrtTtleppt);

// Specify data members to bind the chart's series template.

Chartppt.SeriesDataMember = "Customer";
Chartppt.SeriesTemplate.ArgumentDataMember = "Month";
Chartppt.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Sales" });
Chartppt.SeriesTemplate.ValueScaleType = ScaleType.Numerical;

//Legend Settings
Chartppt.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside;
Chartppt.Legend.AlignmentVertical = LegendAlignmentVertical.Center;
Chartppt.Legend.BackColor = System.Drawing.Color.White;
Chartppt.Legend.Border.Visible = false;

Chartppt.SeriesTemplate.Label.Visible = false;
Chartppt.SeriesTemplate.View = new LineSeriesView();
Chartppt.DataBind();


//Set the Chart Width and Height
Chartppt.Width = 600;
Chartppt.Height = 300;
Controls.Add(Chartppt);

// Cast the chart's diagram to the XYDiagram type, to access its axes.
XYDiagram diagramppt = Chartppt.Diagram as XYDiagram;

//Property to Add the color to the Chart
diagramppt.DefaultPane.BackColor = System.Drawing.Color.White;


// Create a constant line.
ConstantLine Lineppt = new ConstantLine();
diagramppt.AxisY.ConstantLines.Add(Lineppt);


// Define its axis value.
Lineppt.AxisValue = 15;

// Customize the appearance of the constant line.
Lineppt.Color = System.Drawing.Color.Red;
Lineppt.LineStyle.DashStyle = DashStyle.Solid;
Lineppt.LineStyle.Thickness = 4;
Lineppt.ShowInLegend = false;


//Set the WEbchart properties for good look and feel

((DevExpress.XtraCharts.XYDiagram)Chartppt.Diagram).AxisY.GridSpacing = 10;
((DevExpress.XtraCharts.XYDiagram)Chartppt.Diagram).AxisX.Label.Angle = -90;
((DevExpress.XtraCharts.XYDiagram)Chartppt.Diagram).AxisY.Range.MinValue = 0;
((DevExpress.XtraCharts.XYDiagram)Chartppt.Diagram).AxisY.NumericOptions.Format = NumericFormat.Currency;
}

private DataTable PPTData()
{
// Create an empty table.
DataTable table = new DataTable("Table1");
// Add three columns to the table.s
table.Columns.Add("Customer", typeof(String));
table.Columns.Add("Month", typeof(String));
table.Columns.Add("Sales", typeof(Int32));

// Add data rows to the table.
table.Rows.Add(new object[] { "Total", "25/05/2007", 10 });
table.Rows.Add(new object[] { "Total", "26/05/2007", 10 });
table.Rows.Add(new object[] { "Total", "27/05/2007", 10 });
table.Rows.Add(new object[] { "Total", "29/05/2007", 10 });
table.Rows.Add(new object[] { "Total", "30/05/2007", 20 });


return table;
}

Labels: