FWH 2102: Printing TGraph, without creating as control

FWH 2102: Printing TGraph, without creating as control

Postby nageswaragunupudi » Wed Mar 17, 2021 2:35 pm

To print a graph using TGraph, we need to first create TGraph control in a Dialog/Window and then call the Print() method. But if we need to print many graphs or the same graph with different datas on many pages, this is not convenient.

Now we can define one or more TGraph objects without creating as controls of dialog/window and use these objects with variable data to directly print graphs to multiple pages. The user will not see any dialog/window with flickering graphs.

command:
Code: Select all  Expand view

#xcommand DEFINE GRAPH <oGraph> [ SIZE <nWidth>, <nHeight> ] ;
               [ TITLE <cTitle> ] ;
               [ <pixel: PIXEL > ] ;
               [ <l3d: 3D> ] ;
               [ <lxGrid: XGRID> ] ;
               [ <lyGrid: YGRID> ] ;
               [ <lxVal: XVALUES> ] ;
               [ <lyVal: YVALUES> ] ;
               [ <lLegends: LEGENDS> ] ;
               [ TYPE <nType> ] ;
               [ FONT <oFont> ] ;
 

We can then configure the graph object the way we want, assign series, labels and data and print using the command:
Code: Select all  Expand view

#xcommand @ <nRow>, <nCol> PRINT TO <prn> GRAPH <ograph> ;
      SIZE <nWidth>,<nHeight> [<unit: PIXEL,MM,CM,INCHES,SCREEN>] ;
 


Again we can reconfigure the TGraph object as we want and print another graph on the same page or another page.

Here is a sample
fwh\samples\graphprn.prg
Code: Select all  Expand view

/*
  Printing graphs using TGraph class without creating as
  control in a window/dialog
*/


#include "fivewin.ch"
#include "tgraph.ch"

REQUEST DBFCDX

//----------------------------------------------------------------------------//

function Main()

   local oGraph, oPrn, aTable, cRegion

   CreateDBF()

   USE GRAFDATA NEW VIA "DBFCDX"
   SET ORDER TO TAG MAIN
   GO TOP

   DEFINE GRAPH oGraph SIZE 250, 200 TYPE GRAPH_TYPE_BAR ;
      YVALUES XGRID YGRID XVALUES LEGENDS

   WITH OBJECT oGraph
      :aSeries = { { "DeskTop", METRO_AMBER  }, ;
                   { "Lap-Top", METRO_OLIVE  }, ;
                   { "Printer", CLR_HMAGENTA } }
      :aYVals = { "Jan", "Feb", "Mar", "Apr", "May" }
      :lViewSRLegend = .T.
      :cTitle := "ABC COMPANY"
   END

   PRINT oPrn PREVIEW

   for each cRegion in { "NORTH", "SOUTH" }

      oGraph:aData   := FW_DbfToArray( "JAN,FEB,MAR,APR,MAY", { || FIELD->REGION = cRegion } )
      oGraph:cTitle  := "ABC COMPANY, " + cRegion + " REGION"

      PAGE

      @ 1, 1   PRINT TO oPrn GRAPH oGraph SIZE 5,4 INCHES
      @ 5, 1   PRINT TO oPrn TABLE oGraph:aTable SIZE 5, 2 INCHES
      @ 7, 1.5 PRINT TO oPrn CHART oGraph:aTable SIZE 4, 3 INCHES

      ENDPAGE

   next

   ENDPRINT

   oGraph:End()
   CLOSE DATA

return nil

//----------------------------------------------------------------------------//

static function CreateDBF()

   field REGION, PRODUCT

   local aData := ;
   {  { "NORTH", "DeskTop", 14280, 20420, 12870, 25347,  7640 } ;
   ,  { "NORTH", "Lap-Top",  8350, 10315, 15870,  5347, 12340 } ;
   ,  { "NORTH", "Printer", 12345,  8945, 10560, 15600, 17610 } ;
   ,  { "SOUTH", "DeskTop", 12345,  8945, 10560, 15600, 17610 } ;
   ,  { "SOUTH", "Lap-Top", 14280, 20420, 12870, 25347,  7640 } ;
   ,  { "SOUTH", "Printer",  8350, 10315, 15870,  5347, 12340 } ;
   }

   DBCREATE( "GRAFDATA", { { "REGION", "C", 5, 0 }, { "PRODUCT", "C", 7, 0 }, ;
      { "JAN", "N", 5, 0 }, { "FEB", "N", 5, 0 }, { "MAR", "N", 5, 0 }, ;
      { "APR", "N", 5, 0 }, { "MAY", "N", 5, 0 } }, "DBFCDX", .T., "DATA" )

  FW_ArrayToDBF( aData )

  GO TOP
  INDEX ON REGION+PRODUCT TAG MAIN
  CLOSE DATA

return nil


Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10465
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 57 guests