Below is an example of an SVG file, which draws a blue rectangle.
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect width="300" height="100" fill="blue"/>
</svg>
2) Add a green circle and a yellow star to the example.
3) Add some text.
<embed src="example.svg" type="image/svg+xml" width="400" height="300" />Or:
<object data="example.svg" type="image/svg+xml" width="400" height="300" />Browsers need to create scrollbars for large SVG images. Some browsers have problems with that. Creating a large object area will force the browser to produce adequate scrollbars:
<object data="example.svg" type="image/svg+xml" width="200%" height="200%" />In general, there can be incompatibilities between different browsers and different operating systems. Not all advanced features may work on all browsers.
<?php header('Content-Type: image/svg+xml'); echo '<?xml version="1.0" standalone="no"?>'; ?>This should be followed by the DOCTYPE and the svg tags as in the previous examples.
If a PHP or Perl/CGI script produces an SVG file, it is important
that the header information is accurately produced.
For Perl, don't use "print header();" but instead use this:
print "Content-type: image/svg+xml\n\n";
print '<?xml version="1.0" standalone="no"?>';
This should be followed by printing the DOCTYPE and svg tags.
7) Print selected values of the metadata. Try these:
Graphviz has a number of command-line utilities for creating graph layouts:
dot (for directed graphs), neato and fdp (for undirected graphs), and
twopi and circo for different radial, circular layouts.
The graph code is saved in files with a ".dot" extension.
digraph names {
node0 [label="John"]
node1 [label="Mary"]
node2 [label="Paul"]
node0 -> node1
node0 -> node2
node2 -> node1
}
An undirected graph uses the word "graph" instead of "digraph"
and "--" instead of "->".
10) Save the file as an undirected graph and use the "neato" and "fdp" layouts.
11) Have a look at the Graphviz gallery. Each of the examples shows both the picture and the Graphviz file that was used. (In some cases, you need to click on the picture to see the code.)
It should be noted that sending user data to system calls is a potential security risk. The user data needs to be carefully checked. This will be discussed in the Security lecture.
The following example shows a form that contains textfields for
names of people and their friends. The script in the next section then
draws a graph for the social network of these people.
<form action=" ... " method="post">
Enter the names of a people and their friends. <p>
Person: <input type="text" size =10 name="person1"> <br>
Friends: <input type="text" size=10 name="f11"><br>
<input type="text" size=10 name="f12"> <br>
<input type="text" size=10 name="f13"> <p>
Person: <input type="text" size=10 name="person2"> <br>
Friends: <input type="text" size=10 name="f21"><br>
<input type="text" size=10 name="f22"> <br>
<input type="text" size=10 name="f23"> <p>
Person: <input type="text" size=10 name="person3"> <br>
Friends: <input type="text" size=10 name="f31"><br>
<input type="text" size=10 name="f32"> <br>
<input type="text" size=10 name="f33"> <p>
<input type="submit" value="Draw it">
<?php error_reporting(E_ALL ^ E_NOTICE); header("Content-type: image/svg+xml"); $graphcontent = "digraph G {". "\"".$_REQUEST['person1']."\" -> \"".$_REQUEST['f11']."\"\n". "\"".$_REQUEST['person1']."\" -> \"".$_REQUEST['f12']."\"\n". "\"".$_REQUEST['person1']."\" -> \"".$_REQUEST['f13']."\"\n". "\"".$_REQUEST['person2']."\" -> \"".$_REQUEST['f21']."\"\n". "\"".$_REQUEST['person2']."\" -> \"".$_REQUEST['f22']."\"\n". "\"".$_REQUEST['person2']."\" -> \"".$_REQUEST['f23']."\"\n". "\"".$_REQUEST['person3']."\" -> \"".$_REQUEST['f31']."\"\n". "\"".$_REQUEST['person3']."\" -> \"".$_REQUEST['f32']."\"\n". "\"".$_REQUEST['person3']."\" -> \"".$_REQUEST['f33']."\"\n". "}"; $descriptorspec = array(0 => array("pipe", "r"),1 => array("pipe", "w") ); $process = proc_open('/usr/bin/dot -Tsvg',$descriptorspec,$pipes); if (is_resource($process)) { fwrite($pipes[0], "$graphcontent"); fclose($pipes[0]); while (! feof($pipes[1])) {$retval .= fgets($pipes[1]);} echo $retval; fclose($pipes[1]); $return_value = proc_close($process); } ?>
#!/usr/bin/perl use CGI; my $cgi = new CGI; $filetype = "svg"; $graphcontent = "digraph G {". "\"".$cgi->param('person1')."\" -> \"".$cgi->param('f11')."\"\n". "\"".$cgi->param('person1')."\" -> \"".$cgi->param('f12')."\"\n". "\"".$cgi->param('person1')."\" -> \"".$cgi->param('f13')."\"\n". "\"".$cgi->param('person2')."\" -> \"".$cgi->param('f21')."\"\n". "\"".$cgi->param('person2')."\" -> \"".$cgi->param('f22')."\"\n". "\"".$cgi->param('person2')."\" -> \"".$cgi->param('f23')."\"\n". "\"".$cgi->param('person3')."\" -> \"".$cgi->param('f31')."\"\n". "\"".$cgi->param('person3')."\" -> \"".$cgi->param('f32')."\"\n". "\"".$cgi->param('person3')."\" -> \"".$cgi->param('f33')."\"\n". "}"; if ($filetype eq "svg") { print "Content-type: image/svg+xml\n\n"; open(PIPE, "| dot -Tsvg 2>Errors.log"); print PIPE $graphcontent; close PIPE; } else { print "Content-type: text/html\n\n"; print $graphcontent; }