TGraph

Source: source/classes/tgraph.prg

Inherits from: TControl

TGraph is FiveWin's charting control that supports bar, line, point, pie, and XY scatter charts. It provides a comprehensive set of graphing capabilities directly within FiveWin windows and dialogs, with support for multiple series, 3D rendering, legends, grid lines, and export to image or printer.

Graph Types

TGraph supports the following chart types, defined as numeric constants:

ConstantValueDescription
GRAPH_TYPE_BAR1Vertical bar chart (default)
GRAPH_TYPE_LINE2Line chart connecting data points
GRAPH_TYPE_POINT3Scatter/point chart
GRAPH_TYPE_PIE4Pie chart
GRAPH_TYPE_XY_LINES6XY scatter chart with connecting lines
GRAPH_TYPE_XY_POINTS7XY scatter chart with points only

Key DATA Members

DATATypeDescription
nTypeNumericGraph type (GRAPH_TYPE_BAR, GRAPH_TYPE_LINE, etc.)
aSeriesArrayArray of series definition objects
aDataArrayChart data values
l3DLogicalEnable 3D rendering
lxGridLogicalShow X-axis grid lines
lyGridLogicalShow Y-axis grid lines
lxValLogicalShow X-axis value labels
lyValLogicalShow Y-axis value labels
cTitleCharacterMain chart title
cSubTitCharacterChart subtitle
cTitXCharacterX-axis title
cTitYCharacterY-axis title
cPictureCharacterPicture mask for value formatting
lViewValLogicalShow data values on chart
lLegendsLogicalShow series legends
nBarSepNumericBar separation gap
nPointNumericPoint marker type (POINT_TYPE_1..3)
nMaxValNumericMaximum Y-axis value
nMinValNumericMinimum Y-axis value
nClrBackNumericBackground color
cBitmapCharacterBackground bitmap file
lAutoRefreshLogicalAuto-repaint on data changes (.T. by default)

Methods

MethodDescription
New( nTop, nLeft, oWnd, nWidth, nHeight, cTitle, lDesign, lPixel, l3D, lxGrid, lyGrid, lxVal, lyVal, lPopUp, lLegends, nType )Create a new TGraph control
ReDefine( nId, oWnd, cTitle, l3D, lxGrid, lyGrid, lxVal, lyVal, lPopUp, lLegends, nType )Redefine from dialog resource
AddSerie( aDat, cLegend, nColor, nType, l3D, lViewVal, lDrawPoint, lDrawLine, cSRLegend )Add a data series to the chart. Returns the series index.
AddXYSerie( aDat, cLegend, nColor, nType, l3D, lViewVal, lDrawPoint, lDrawLine, cSRLegend )Add an XY scatter series (array of {x,y} pairs)
SetYVals( aLabels )Set Y-axis category labels (e.g., Q1, Q2, Q3, Q4)
SelSerie( nSerie )Select a specific series for display
SelPeriod( nPeriod )Select a specific period for display
SaveAsImage( cFile )Save chart as a bitmap image file
Save2XLS( nTypeGraph )Export chart data to Excel
Print( oPrn, nTop, nLeft, nWidth, nHeight )Print the chart on a printer device
PrintGraph( lPrev, oPrn, nY, nX, cFile, nPorY, nPorX, lUser, lHoriz, nTypePage )Advanced printing with preview and zoom options
Copy2ClipBoard( lMsg )Copy chart image to clipboard
ResizeSerie( nNewSize )Resize the number of series dynamically
SetValue( nSerie, nPos, nValue, lRefresh )Change one value and repaint automatically
GetValue( nSerie, nPos )Read one chart value
SetData( aData, lRefresh )Replace all chart data and repaint automatically
SetSerieValues( nSerie, aValues, lRefresh )Replace one series and repaint automatically
SetXYValue( nSerie, nPos, xVal, yVal, lRefresh )Change one XY point and repaint automatically
SetXYData( aXYValues, lRefresh )Replace all XY data and repaint automatically
DelSerie( nSerie, lRefresh )Delete a series and repaint automatically
ClearSeries( lRefresh )Delete all series and repaint automatically
BeginUpdate()Open an update block: repaints are deferred
EndUpdate( lRefresh )Close the block and repaint once
DoRefresh()Repaint now (honors lAutoRefresh and update blocks). Alias: Update()
Default()Apply default chart settings and fonts
Destroy()Destroy the chart and free resources

Auto-Refresh

With lAutoRefresh set (.T. by default), every data change repaints the chart by itself — no need to rebuild the control or call Refresh() manually. Change metrics while navigating and the graph follows:

// One value changes -> the chart repaints itself
oGraph:SetValue( 1, 3, 1250 )

// Replace a whole series at once
oGraph:SetSerieValues( 2, { 10, 20, 30, 25, 40 } )

// Batch many changes with a single repaint at the end
oGraph:BeginUpdate()
oGraph:cTitle := "New title"
oGraph:SetData( { aVals1, aVals2 } )
oGraph:EndUpdate()

// Suspend automatic repaints and refresh manually
oGraph:lAutoRefresh := .F.
...
oGraph:Update()

Sample samples/graphics/graphkpi.prg shows the pattern in a full window: two TXBrowse controls with KPI columns and footer totals, a docked buttonbar, a statusbar with the selected KPIs, and a splitter-resizable TGraph that repaints itself while browsing or when the metrics change. Note: code-created browses need oBrw:CreateFromCode(), and direct assignments to ::aData / ::aSeries cannot be intercepted — use the Set*() methods or call Refresh() afterwards.

Example: Bar Chart with Two Series

#include "FiveWin.ch"

function Main()

   local oWnd, oGraph

   DEFINE WINDOW oWnd TITLE "Quarterly Sales" SIZE 600, 400

   @ 10, 10 GRAPH oGraph SIZE 560, 340 OF oWnd TYPE GRAPH_TYPE_BAR ;
      TITLE "Sales by Quarter"

   oGraph:l3D      := .T.
   oGraph:lxGrid   := .T.
   oGraph:lyGrid   := .T.
   oGraph:lxVal    := .T.
   oGraph:lyVal    := .T.
   oGraph:lLegends := .T.
   oGraph:cPicture := "999,999.99"

   // Set category labels
   oGraph:SetYVals( { "Q1", "Q2", "Q3", "Q4" } )

   // Add two series
   oGraph:AddSerie( { 1200, 1500, 1100, 1800 }, "Product A", CLR_RED )
   oGraph:AddSerie( { 900, 1300, 1600, 1400 }, "Product B", CLR_BLUE )

   ACTIVATE WINDOW oWnd CENTERED

return nil

Notes

See Also