Company logo

FOXSTUFF

Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Versión en español de este artículo

SimpleChart revisited

Your comments, questions and suggestions on our VFP charting control

Our thanks to all of you who have given us feedback on our SimpleChart control. Since we published the control in March 2002, it has become the most downloaded item on the Foxstuff site, and we are delighted that so many Foxpro programmers have found it useful.

In this aricle, we will answer some of your more frequent questions about the control, and also pass on some useful tips from its users.

For those who haven't seen SimpleChart, the control is essentially a wrapper for Microsoft's MSChart ActiveX control. Its aim is to simplify the job of producing line graphs, histograms, pie charts and other two- and three-dimensional charts. To find out more about it, and to obtain your free copy, see the Foxstuff article, Add graphs and charts to your VFP applications.

Does my chart have to be on a form by itself, or can I place it alongside other controls?

It's your choice. The control works exactly the same way in both situations.

I'm trying to produce a 2D horizontal bar chart, but I get an OLE error, 'Invalid property value'. Why?

SimpleChart supports chart types 0 - 9, and 14 and 16. You can only set the ChartType property to one of those values. The other 16 values that you see in the Properties Window do not work in VFP (the 2D horizontal bar chart would be type 11). This is a limitation of MS Chart, not of SimpleChart.

I have carefully followed the instructions for producing a chart, but nothing happens. There is no error message. The chart simply fails to appear.

The most likely explanation is that the chart is unable to find the input table or cursor. Make sure that the table or cursor is open in the current data session, and the correct alias has been stored in the cAlias property. Check also that the cData property contains a valid list of names of numeric fields. The CreateChart method will return .F. if it detects a problem with cAlias or cData.

What additional files do I need to distribute with my application?

You need to distribute the MS Chart ActiveX control, which is MSCHRT20.OCX. This should be installed in the user's System folder and registered as an ActiveX control. If you are not sure how to do that, check the Help for the Setup Wizard (if you are using VFP 6.0) or InstallShield Express (VFP 7.0 or later).

How can I print my chart?

Neither MS Chart nor SimpleChart directly supports printing. Nor can you place the chart in a report. However, there is an EditCopy method, which copies the chart to the clipboard. You could therefore paste the chart into another application and then print it from there (see also the next two questions).

I tried using the EditCopy method to copy my chart to MS Word, but all I could see was a block of figures.

What you saw was the underlying data on which the chart was based. To see the chart, choose Paste Special from Word's Edit menu, then select Picture. The same behaviour occurs in Excel and some other applications.

Given that I can now paste the chart into a Word document and then print it, is there a way to do that programmatically, without user intervention?

Yes. The following technique uses ActiveX Automation to do just that (our thanks to Ben Hambidge for suggesting this idea, and to Jon Barker for his help with testing it).

First, create a custom form property. We'll call it oWord. Initialise it to NULL, for example by executing this code in the form's Init event:

THISFORM.oWord = NULL

Next, execute the following code at the point at which you want to print the chart (such as in the Click event of a Print button):

IF ISNULL(THISFORM.oWord)
  THISFORM.oWord=CREATEOBJECT("Word.Application")
ENDIF
oDoc=THISFORM.oWord.Documents.Add()
THISFORM.MyChart.EditCopy()
THISFORM.oWord.Selection.PasteSpecial(.F., .F., 0, .F., 3)
oDoc.PrintOut
oDoc.Close(0)

Essentially, this code will instantiate Microsoft Word as an Automation server (unless it has already been instantiated), copy the chart to the clipboard, paste the chart image (not the values) into a new Word document, print the document, and close the document without prompting to save it. (In the fifth line of the above code, MyChart is the name of the SimpleChart object.)

Finally, add this code to the form's Destroy event:

IF NOT ISNULL(THISFORM.oWord)
  THISFORM.oWord.Quit
ENDIF

How can I display a rotation cursor so that users can rotate my 3D chart?

A rotation cursor appears as a four-headed arrow that enables the user to rotate a 3D chart interactively. To enable it, set the AllowDynamicRotation property to .T. Also, make sure that the AllowSelections property is also set to .T. The user will then be able to invoke the rotation cursor by holding down the Ctrl key.

Is it possible to rotate a 3D chart programmatically?

Yes. There are properties available to adjust both the rotation and the elevation. To see this in action, add two spinners to your form. Place this code in the InteractiveChange event of the first spinner:

THISFORM.MyChart.Plot.View3d.Rotation=this.Value

and this in the InteractiveChange of the second spinner:

THISFORM.MyChart.Plot.View3d.Elevation = this.Value

When you run the form and adjust the spinner values, you will see some interesting effects. (Our thanks to Kirk Kelly for providing this information.)

Is there a way of changing the font used for the labels?

This code will change the labels on the X-axis to 14 pt Arial Narrow:

WITH THISFORM.MyChart.Plot.Axis(0)
  FOR EACH olabel IN .Labels
    olabel.VtFont.Name = "Arial Narrow"
    oLabel.vtFont.Size = 14
  ENDFOR
ENDWITH

Altrenatively, reference Axis(1) or Axis(2) rather than Axis(0) in the first line. This will let you change the font on the left and right vertical axes respectively.

Is it possible to display titles for the three axes?

Yes. SimpleChart can display a title for the X-axis and for each of the two Y-axes. Here's how your code might look:

WITH THISFORM.MyChart.Plot.Axis(0)
  .AxisTitle.Text = "This is the X-axis"
  .AxisTitle.vtFont.Name = "Arial"
  AxisTitle.VtFont.Size = 12
ENDWITH

This will display a horizontal title for the x-axis. As with the labels, you can reference Axis(1) or Axis(2) for the left and right y-axes respectively.

The AxisTitle object has several other properties that you can use to customise the title -- TextLayout.Orientation, for instance, lets you choose the orientation (1 = horizontal, 2 = vertical). In addition, the VtFont object has properties for Style (1 = bold, 2 = italic, 3 = bold italic) and Effect (256 = strike-through, 512 = underline).

Mike Lewis Consultants Ltd. July 2002. Revised September 2002, January 2006 and January 2008.

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.