Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Generating a Graph dynamically
Message
From
11/11/2003 03:43:19
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
ActiveX controls in VFP
Miscellaneous
Thread ID:
00847725
Message ID:
00848615
Views:
23
>I've been asked to generate some report showing Graphs depending on some data dates ranges. I've heard of MS Graph ocx but don't know how to use it. I also took a look at the Graph wizard of VFP but I just couldn't find how to tell the graph what info or table to use.
>
>any ideas, or link to help me up to find the path?

Selim,
Basically you pass the tab delimited version of data to MSGraph.
*** set the LOCALEID to English
nlLocaleId=Sys(3004)		&& Save local id
=Sys(3006,1033)			&& We will be sending instructions in English

#include xlconstants.h
#Define TABULATE Chr(9)
Wait Window Nowait "Filling cell values..."

Select a.first_name-(' ' +a.last_name ) As Employee,;
  b.country, Sum(c.Order_amt) As TotOrder;
  FROM  customer b ;
  INNER Join orders c On  b.cust_id = c.cust_id ;
  INNER Join Employee a On a.emp_id = c.emp_id ;
  GROUP By 2, 1 ;
  into Cursor myCursor
Do (_Genxtab)
lcTempFile = Sys(2015)+'.tmp'
Copy To (lcTempFile) Type Csv
MCGDATA = Chrtran(Filetostr(lcTempFile),',',TABULATE)
Erase (lcTempFile)

Create Cursor testgen (graphtest g)
Append Blank
Wait Window Nowait "Plotting..."
Append General graphtest Class MSGraph.Chart Data MCGDATA

oForm = Createobject("myForm")
With oForm
  .Show
  .AddObject("myGraph","OleBoundControl")
  .AddObject("Exporter","Exporter")
  .Exporter.Visible = .T.
  With .myGraph
    .Height = oForm.Height - 10 - oForm.Exporter.Height
    .Width = oForm.Width - 10
    .Left = 5
    .Top = 5 + oForm.Exporter.Height
    .ControlSource = "testgen.graphtest"
    .Object.Application.plotby = xlRows
    .ChartGroups(1).Gapwidth = 0
    With .Axes(xlCategory).TickLabels
      With .Font
        .Name = "Arial"
        .FontStyle = "Normal"
        .Size = 8
      Endwith
      .Offset = 50
      .Orientation = 90
    Endwith
  Endwith
  .FormatGraph(.myGraph,xlColumnClustered,;
    'Customer Sales by Employee',;
    'Customer Country',0,;
    'Number of Customers',90,;
    'Employee Name',0)
  .myGraph.Visible=.T.
Endwith
Wait Clear
**** Set the LocaleId to the previous value
=Sys(3006,Val(nlLocaleId))

Read Events

Define Class myForm As Form
  Width=800
  Height=600
  Name='MyForm'
  BackColor = Rgb(255,255,196)
  Procedure QueryUnload
  Clear Events
Endproc

  Procedure FormatGraph
  Lparameters oGraph,tnChartType, tcChartTitle, ;
    tcCategoryTitle, tnCategoryRotation, ;
    tcValuesTitle, tnValuesRotation, ;
    tcSeriesTitle, tnSeriesRotation
  With oGraph
    .ChartType = tnChartType
    .hastitle = .T.
    .haslegend = .T.
    With .Legend.Font
      .Bold = .F.
      .Size = 8
    Endwith
    .Legend.Position = xlRight
    With .ChartTitle
      .Caption = tcChartTitle
      .Shadow = .T.
      With .Border
        .Weight = xlHairline
        .LineStyle = xlAutomatic
      Endwith
      With .Font
        .Size = 12
        .Background = xlBackgroundTransparent
      Endwith
    Endwith
    With .Axes(xlCategory).TickLabels
      .Font.Size = 8
      .Orientation = xlTickLabelOrientationAutomatic
    Endwith
    *!*			With .SeriesCollection(1)
    *!*			    .HasDataLabels = .T.
    *!*			    .DataLabels.Font.Size = 8
    *!*			EndWith
    * Set axis titles
    This.FormatAxis(oGraph,xlCategory, tcCategoryTitle,tnCategoryRotation) && Set axis titles
    This.FormatAxis(oGraph,xlValue, tcValuesTitle, tnValuesRotation)
    With .Axes(xlValue).TickLabels
      .Font.Size = 8
      .Orientation = xlTickLabelOrientationAutomatic
    Endwith
    If .Axes.Count > 2	&& Otherwise illegal operation (if chart is not 3D)
      This.FormatAxis(oGraph,xlSeriesAxis, tcSeriesTitle,tnSeriesRotation)
      With .Axes(xlSeriesAxis).TickLabels
        .Font.Size = 8
        .Orientation = xlTickLabelOrientationAutomatic
      Endwith
    Endif
  Endwith
Endproc


  Procedure FormatAxis
  Lparameters toGraph, tnAxis, tcCaption, tnOrientation, tcFontName, ;
    tnFontSize, tlBold, tlItalic, tlUnderline
  If Type("tcCaption") = "C" And !Empty(tcCaption) ;
      and Type("toGraph.Axes(tnAxis)") = "O" && Not empty title and axis is a supported type
    If Type("tnOrientation") # "N"
      tnOrientation = 0
    Endif
    If Type("tcFontName") # "C" Or Empty(tcFontName)
      tcFontName = "Arial"
    Endif
    If Type("tnFontSize") # "N"
      tnFontSize = 10
    Endif

    With toGraph.Axes(tnAxis)
      .hastitle = .T.
      With .AxisTitle
        .Caption = tcCaption
        .Orientation = tnOrientation
        With .Font
          .Name = tcFontName
          .Size = tnFontSize
          .Bold = tlBold
          .Italic = tlItalic
          .Underline = tlUnderline
          .Background = xlBackgroundTransparent
        Endwith
      Endwith
    Endwith
  Endif
Endproc

Enddefine

Define Class Exporter As CommandButton
  Top =0
  Left = 70
  Caption = 'Export'
  Procedure Click && Export to 4 different file formats
  lcFormats = 'TIF,GIF,PNG,JPG'
  lcFileName = 'c:\Temp\myExport'
  lnFormats = Alines(arrFormats,lcFormats,.T.,',')
  For ix=1 To lnFormats
    Thisform.myGraph.Object.Export(;
        Forceext(lcFileName,arrFormats[ix]),arrFormats[ix])
  Endfor
Endproc
Enddefine
MSGraph object model is almost the same as Excel Chart object model. IOW you could use Excel help or MSgraph help for PEM (not 100% but in general).

Also think of MSChart 2.0 and OWC. For MSChart sample browse old messages (search I'd say but ...:). For OWC sample visit west-wind com.

MSChart is fast but not printable as easily as does MSgraph but OTOH doesn't have some problems of MSGraph (like slowness, if user has Office and uninstalls office bombs etc).

Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform