Programming - Tech - Life

DevExpress XtraChart - Add data to chart with code

DevExpress XtraChart - Add data to chart with code

Published Jun 4 2009

I wanted to share how to add data to a DevExpress XtraChart control with code and not the chart wizard. The majority of the examples I found at DevExpress.com only used the wizard.  So I would like to show everyone how I did it and maybe help a programmer out there who has to do the same thing

First of all, the thing I needed to do was add series to a DevExpress XtraChart control with data that was calculated when my Form loaded. The wizard examples always used static data that just need to displayed. So I passed my Datatable to the form and used the Datatable.Select method to get the needed DataRows . The next step was to check if length of the DataRows was greater than zero if it is then loop through each one and calculate the values. Add those values to a new Series point. That point will then be added to a new Series which will then be added to the DevExpress Chart control.

The code pictured below is a simple example of the functionality that I used.

private void frmDevExpressXtraChart_Load(object sender, EventArgs e) 
{
//Create and add a unique series to the xtrachart control on each loop
for (int i = 0; i < 10; i++) {
string nameOfSeries = "Series " + i.ToString();
Series stackedBarSeries = new Series(nameOfSeries, ViewType.StackedBar);

//Note: Keep the point name the same to add data to that series of points.
//If the name is not the same then a new point will be added to the x-axis
stackedBarSeries.Points.Add(new SeriesPoint("point", new double[] { Convert.ToDouble(i) }));
chartControl1.Series.Add(stackedBarSeries);
}
}

 

I hope that my example will be a help to any developers out there who are using DevExpress XtraCharts!