Blogs

The One With

Linking a Chart Control to your Pivot Grid. (Helpful Tip)

     

I was working on something recently that required a lot of data analyses. I used the PivodGrid control as it perfectly represented my data source. And one of the reports I was building had the data grouped by Month and by Day, so I wanted to see it as a line chart as well. Quickly poking around I discovered that the PivotGridControl itself can be used as a DataSource as it implements both the IBindingList and the ITypedList interfaces. There are three members (PropertyDescriptors) that the PivotGridControl::ITypedList exposes to help with chart integration. It is easier to visualize them in terms of the coordinate system. (Line chart for example.)

1: "Series": This would be the the series legend.

2: "Arguments": This would be the X-axis.

3: "Values": This would be the Y-axis.

and this is how the list would look like if bound to a regular grid:

 

Binding the chart itself is easy (this will use the Auto-Created Series but you can also bind individual series the same way):

chartControl1.DataSource = pivotGridControl;
chartControl1.SeriesDataMember = "Series";
chartControl1.SeriesTemplate.ArgumentDataMember = "Arguments";
chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Values" });

And that's it. Here is a couple of lines to quickly create your pivot table from the database in case you want to try it out.

const string SQL_GroupByMonth = 
"select count(*) as Count, month(MyTable.DateColumm) as Month, " +
"day(MyTable.DateColumm) as Day from MyTable " +
"group by month(MyTable.DateColumm), day(MyTable.DateColumm) " +
"order by Month, Day";
var db = new SqlConnection("<CONNECTION STRING>");
db.Open();
var da = new SqlDataAdapter(SQL_GroupByMonth, db);
var ds = new DataSet();
da.Fill(ds);
pivotGridControl.DataSource = ds.Tables[0];

 

 

Cheers,

Azret

Published Aug 04 2008, 10:23 PM by Azret Botash (DevExpress)
Technorati tags: XtraPivotGrid, XtraCharts
Bookmark and Share

Comments

 

CESAR F. QüEB said:

Nice tip.. very useful... thank you!...

Really appreciated for me and the customers...

August 5, 2008 12:54 AM
 

komathi said:

It is very helpful to all...

Thanks

Regards

Komathi.S

November 24, 2008 4:31 AM
 

The One With said:

Just like our WinForms Pivot Grid is able to act as an input data source for a Chart Control (r ead here

May 5, 2010 5:40 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.