Thursday, July 14, 2011

VFP Tricks: Adding Graphs and Charts into FoxPro Application

VFP Tricks: Add Graphs or Charts into FoxPro Application
Somehow any charts/graphs in an application would make a big difference for a user-friendly visual application. Also, sometimes it's very important to includes charts or graph in our applications.

Here an easy way for creating charts and graphs in Visual FoxPro application. We need take to take a look at SimpleChart control. You can download SimpleChart control (.zip) from link provided at the bottom of this post.

Just add the control to vfp forms, adjust some properties. Before you really know this control, you'll be able to makes good looking line graphs, pie charts, histograms and a any others two or three dimensional charts.

SimpleChart control is essentially acts as a wrapper for Microsoft's MSChart ActiveX control, which is come free within Visual FoxPro (from versions 6.0 and later). If you well know about MSChart, you also know that it can produce a wide varieties of useful charts. Unfortunately it is still difficult and is hardly to be said as programmer-friendly. Putting the data into charts is such a very difficult thing. There are also you have to cope with a confusing array of properties and also methods to customize the chart appearances.

The SimpleChart control class was aimed to gives ease of use on MSChart. The minimum requirement is only to provides a table or cursor to stores data. Just set two properties of the SimpleChart control class and call a single method provided.

SimpleChart is absolutely free. To download it, follow a link at the bottom of this post.

Start with a cursor

The very first step on using SimpleChart class is to put data into a cursor or table. Essentially, a table/cursor should contains one field for each of the series to be plotted and, optionally, a further fields to hold the row labels. Each record in the table/cursor should be correspond to one data point.

Properties and methods

The second step, you must drag-drop SimpleChart control class onto form. Just adjust two properties within. This can be done either from the Form Designer or with a proper variable point in your code. The class's Init method is a good place to write the code.

The properties to be fills up are;
- cAlias; set this to the cursor alias that contains the data to
  be plotted into.
- cData; set this to a comma delimited list of the cursor fields
  which contain the data to be plotted. These fields data used
  must be in numeric types.

For example, if the alias of the cursor is cSales, you can write the following code in the Init event of SimpleChart control:

SimpleChart.cAlias = "cSales"
SimpleChart.cData = "SALES_VAL,SALES_QTY" // fields name

Call the SimpleChart.CreateChart method to render the chart on the form, just like this;

SimpleChart.CreateChart()

It's not needed to pass any parameters to the CreateChart method. Pay attention to always open the cursor or table before calls CreateChart method.

Download SimpleChart control class.


Reference.

No comments: