KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Graphing in SVG and Adding Text to the Graph Output
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: January 20, 2011

The GRAPH command is used for both graphing in SVG or 4D Chart.

The type of the graphArea parameter determines which graphics engine is used for rendering: If you pass a 4D Chart area reference or a graph area variable, the 4D Chart plug-in will be used. If you pass a picture varaible, the SVG engine will be used.

GRAPH(graphArea;graphNumber;xLabels {;yElements} {;yElements2; ... ;yElementsN})


So, the first step is to be sure to utilize a Picture variable on your form instead of a 4D Chart plugin area. Otherwise, if a 4D Chart area is used, this sample code will not work

The following sample code creates a graph in a picture variable using the SVG engine, then converts the picture variable over to an SVG reference to use SVG commands on it, then converts the svg_ref_id back to a Picture variable for use in 4D again:

C_PICTURE(vGraph) `Pass if you want to use the SVG engine

` Create an array for the x-axis
ARRAY STRING(4;X;2)
X{1}:="1995" ` X Label #1
X{2}:="1996" ` X Label #2

` Create an array for the y-axis
ARRAY REAL(A;2)
A{1}:=30 ` Insert some data
A{2}:=40

` Create an array for the y-axis
ARRAY REAL(B;2)
B{1}:=50 ` Insert some data
B{2}:=80

` graph type
C_LONGINT(vType)
vType:=4 ` 4 for line graph

` Draw the graph
GRAPH (vGraph;vType;X;A;B)

` Set the legends for the graph
GRAPH SETTINGS(vGraph;0;0;0;0;False;False;True;"France";"USA")

` convert the C_PICTURE to an SVG_Ref_ID
$svgRef:=SVG_Open_picture (vGraph)

` add text to it
$textID:=SVG_New_text ($svgRef;"Hello world";0;0)

` convert from SVG_Ref_ID back to C_Picture
SVG EXPORT TO PICTURE($svgRef;vGraph)

` clear SVG memory
SVG_CLEAR ($svgRef)

` write out the picture file
WRITE PICTURE FILE("";vGraph)


The sample code above should get you started with being able to use the SVG commands on a graph created with the GRAPH command.