« 4 Fun WebGL Demos | Main | My Alternative to Gantt charts for Project Schedules »
Wednesday
Jan252012

Microsoft Chart Controls: Using DataBindCrossTable Method for Dynamic Series

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.

image

 

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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>