Welcome!

PowerBuilder Authors: Chris Pollach, Yeshim Deniz, Jayaram Krishnaswamy, Kevin Benedict, Avi Rosenthal

Related Topics: PowerBuilder, Microsoft Cloud

PowerBuilder: Article

Using the Microsoft Chart Controls in PowerBuilder

Adding new charting capabilities to PowerBuilder applications

You may not be aware of this, but Microsoft now provides a rather powerful charting control free of charge as an add-in for .NET 3.5. It's available for download at http://xrl.us/ben3pm. Because it supports 35 different chart types (see Figure 1), it can add significant new charting capability to PowerBuilder applications.

As a .NET visual control we should be able to use it "natively" within PowerBuilder .NET targets once PB 12 is released. However, for those using the current (pre-12.0) versions of PowerBuilder, and those who will still be creating Win32 target applications even after moving to PB 12, it would still be nice to be able to use that control. This article will show you how to do that.

As with other .NET visual controls I've implemented in PowerBuilder in previous PBDJ articles, we'll be using the Microsoft Interop Forms Toolkit (http://xrl.us/beo995) to create a COM wrapper for the control. Once we've done that, we need to create methods, properties, and events on the wrapper that PowerBuilder will use to interact with the underlying control.

For example, there's no direct method for getting the data out of a PowerBuilder DataWindow and into the Microsoft Chart Control.

Even in DataWindow.NET, there's no good way of getting the data out of a DataWindow in the System.Data.Dataset object type that most .NET data-aware controls use. What I've done is created a SetData method on the wrapper that takes the data as an XML string, converts that to a DataSet, and then assigns that to the chart control:

public int SetData ( String xml)
{
System.IO.StringReader sr;
sr = new System.IO.StringReader(xml);
dataset.ReadXml(sr);
chart.DataSource = dataset;
chart.DataBind();
return 1;
}

In addition, the chart type value for the control is set through an enumerated value on a .NET class. Since PowerBuilder can't use that in a Win32 target, I created a method on the wrapper that takes the chart type designed as a string and then determines the applicable enumerated value based on it:

public int SetChartType(String type)
{
switch(type)
{
case "Area":
series.ChartType = SeriesChartType.Area ;
break;
case "Bar":
series.ChartType = SeriesChartType.Bar;
break;
case "BoxPlot":
series.ChartType = SeriesChartType.BoxPlot;
break;
.
.
.
default:
series.ChartType = SeriesChartType.Line ;
break;
}
return 1;
}

In PowerBuilder, I created a new Standard User Object of type olecontrol, and then indicated that I want to base it on the InteropUserControl wrapper I created. I can create additional wrapper functions on this user object that make the control more "PB friendly." Now I have a visual user control I can drop onto any window in any application.

In the example code that you can download from the PBDJ site, I'm using the following code to initially create the chart in the sample window:

string                           ls_xml
u_ds_salaries               lds_salaries

lds_salaries = CREATE u_ds_salaries
ls_xml = lds_salaries.Object.DataWindow.Data.XML
Destroy lds_salaries

ole_chart.of_setdata( ls_xml )
ole_chart.of_addseries( "Salaries" )
ole_chart.of_setXseries( "emp_lname" )
ole_chart.of_setYseries( "salary" )
ole_chart.of_Setcharttype( "Line" )

u_ds_salaries is just a datastore user object that contains a DataWindow that contains employee data from the PowerBuilder example database. I've stored the data in the DataWindow so that you don't need to worry about configuring a database connection to run the sample. There are also a number of minor wrapper functions being called on the interop user control that I haven't mentioned here for brevity. In this particular sample, when the user selects a different graph type from the radio buttons, I just pass the new chart type selected to the SetChartType method.

That's all there is to it. About 300 lines of C# code that I had to add to the interop control, and significantly less than that in the PowerBuilder sample. Obviously, you'll want to extend the control to make use of more of the underlying functionality of the Microsoft Chart Controls, but the sample should give you a significant start.

More Stories By Bruce Armstrong

Bruce Armstrong is a development lead with Integrated Data Services (www.get-integrated.com). A charter member of TeamSybase, he has been using PowerBuilder since version 1.0.B. He was a contributing author to SYS-CON's PowerBuilder 4.0 Secrets of the Masters and the editor of SAMs' PowerBuilder 9: Advanced Client/Server Development.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Dimitri Joosten 09/08/09 03:44:00 PM EDT

Hi Bruce, this technique is very usefull! I am evaluating it now and see how I can use it in the (near) future! Thanks!