Microsoft Chart Controls: Using DataBindCrossTable Method for Dynamic Series
Wednesday, January 25, 2012 at 6:19PM
saveenr

This came up at work today, so I thought I’d share. We were using the Microsoft Chart Controls and didn’t know how handle the case where the number of series in the was unknown – in other words the number of series are defined by the data fed to the chart and which are not known at development time.

The DataBindCrossTable method seemed like the right place to start, but we couldn’t find any easy sample. So, after a few minutes I came up with this simple set of code to illustrate its usage.

// Setup the data
var dt = new System.Data.DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("BugCount", typeof(int));
dt.Columns.Add("Day", typeof(int));
dt.Rows.Add("Kim", 10, 0);
dt.Rows.Add("Kim", 12, 1);
dt.Rows.Add("Kim", 18, 2);
dt.Rows.Add("Kim", 5, 3);
dt.Rows.Add("Philby", 18, 0);
dt.Rows.Add("Philby", 25, 1);
dt.Rows.Add("Philby", 9, 2);
dt.Rows.Add("Philby", 32, 3);
// Build the chart
this.chart1.Series.Clear();
this.chart1.DataBindCrossTable(dt.Rows, "Name", "Day", "BugCount", "");

This code produces the following chart.

 

The process is simple. Create a datatable. One column will identify the series. One column will Identify the values on the X axis. And one column will be the value to graph. Then you are ready to call DataBindCrossTable. One tricky thing is to realize you have to pass in the Rows object from the datatable, and not the datatable itself.

Article originally appeared on viziblr (http://viziblr.com/).
See website for complete article licensing information.