119 lines
16 KiB
HTML
119 lines
16 KiB
HTML
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Creating and formatting a basic matrix graph</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch22.html" title="Chapter 22. Matrix graphs"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Creating and formatting a basic matrix graph</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 22. Matrix graphs</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Creating and formatting a basic matrix graph"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2591521"></a>Creating and formatting a basic matrix graph</h2></div></div></div>
|
|||
|
|
|||
|
<p>In order to create a matrix plot the extension module
|
|||
|
"<code class="filename">jpgraph_matrix.php</code>" must be included in the script along with
|
|||
|
the core module "<code class="filename">jpgraph.php</code>".</p>
|
|||
|
<p>The creation of Matrix graphs otherwise follows the traditional steps in the library
|
|||
|
of first creating a matrix graph which acts as a canvas for one or several matrix plots.
|
|||
|
The principle steps are:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
|
|||
|
<p>Create a basic matrix graph as an instance of <code class="code">class
|
|||
|
MatrixGraph</code> (in all our example we use the variable
|
|||
|
<code class="code">$graph</code> to hold this instance)</p>
|
|||
|
</li><li class="listitem">
|
|||
|
<p>Create an instance of one or several matrix plots as instances of
|
|||
|
<code class="code">class MatrixPlot</code>, make any necessary modifications to the
|
|||
|
color map and appearance and then add the plots to the matrix graph
|
|||
|
canvas.</p>
|
|||
|
</li><li class="listitem">
|
|||
|
<p>Send back the graph to the client with a call to
|
|||
|
<code class="code">MatrixGraph::Stroke()</code>. As usual this can be used to either
|
|||
|
send back the graph to the client (e.g.browser) or write the graph to a file
|
|||
|
by specifying a filename as the first argument to
|
|||
|
<code class="code">MatrixGraph::Stroke()</code>.</p>
|
|||
|
</li></ol></div>
|
|||
|
<p>The example in <a class="xref" href="ch22s02.html#fig.matrix_ex0" title="Figure 22.2. A basic matrix graph with all default values (matrix_ex0.php)">Figure 22.2. A basic matrix graph with all default values <code class="uri"><a class="uri" href="example_src/matrix_ex0.html" target="_top">(<code class="filename">matrix_ex0.php</code>)</a></code> </a> shows a matrix plot using just the default values for
|
|||
|
all parameters. </p>
|
|||
|
<p>
|
|||
|
</p><div class="example"><a name="example.matrix_ex0"></a><p class="title"><b>Example 22.1. A basic matrix graph with all default values (<code class="filename">matrix_ex0.php</code>) </b></p><div class="example-contents"> <div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
|||
|
2
|
|||
|
3
|
|||
|
4
|
|||
|
5
|
|||
|
6
|
|||
|
7
|
|||
|
8
|
|||
|
9
|
|||
|
10
|
|||
|
11
|
|||
|
12
|
|||
|
13
|
|||
|
14
|
|||
|
15
|
|||
|
16
|
|||
|
17
|
|||
|
18
|
|||
|
19
|
|||
|
20
|
|||
|
21
|
|||
|
22
|
|||
|
23
|
|||
|
24
|
|||
|
25
|
|||
|
26
|
|||
|
27
|
|||
|
28
|
|||
|
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
|||
|
</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph_matrix.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-comment">//</span><span class="hl-comment"> Some (random) matrix</span><span class="hl-comment"></span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-code">
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">6</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">6</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">6</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">17</span><span class="hl-code">,</span><span class="hl-number">6</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">6</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">12</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-number">9</span><span class="hl-code">,</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">7</span><span class="hl-code">,</span><span class="hl-number">6</span><span class="hl-code">,</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">,
|
|||
|
</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup a bsic matrix graph and title</span><span class="hl-comment"></span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$graph</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">MatrixGraph</span><span class="hl-brackets">(</span><span class="hl-number">400</span><span class="hl-code">,</span><span class="hl-number">300</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">title</span><span class="hl-code">-></span><span class="hl-identifier">Set</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Basic matrix example</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">title</span><span class="hl-code">-></span><span class="hl-identifier">SetFont</span><span class="hl-brackets">(</span><span class="hl-identifier">FF_ARIAL</span><span class="hl-code">,</span><span class="hl-identifier">FS_BOLD</span><span class="hl-code">,</span><span class="hl-number">14</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-comment">//</span><span class="hl-comment"> Create a ,atrix plot using all default values</span><span class="hl-comment"></span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$mp</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">MatrixPlot</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-var">$mp</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div></div></div><p><br class="example-break"> </p><div class="figure"><a name="fig.matrix_ex0"></a><p class="title"><b>Figure 22.2. A basic matrix graph with all default values <code class="uri"><a class="uri" href="example_src/matrix_ex0.html" target="_top">(<code class="filename">matrix_ex0.php</code>)</a></code> </b></p><div class="figure-contents"> <span class="inlinemediaobject"><img src="images/matrix_ex0.png" alt="A basic matrix graph with all default values (matrix_ex0.php)"></span> </div></div><p><br class="figure-break">
|
|||
|
</p>
|
|||
|
<p>In the same way as for other graph types one or several Matrix plots can be added and
|
|||
|
positioned freely in the Matrix graph by specifying the position as either absolute
|
|||
|
coordinates or as fractions of the width/height of the overall graph.</p>
|
|||
|
<p>
|
|||
|
</p><div class="tip" title="Tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Tip</h3>
|
|||
|
<p>An easier way to position Matrix plots is to use layout classes as described
|
|||
|
in <a class="xref" href="ch22s07.html" title="Using layout classes to position matrix plots">Using layout classes to position matrix plots</a></p>
|
|||
|
</div><p>
|
|||
|
</p>
|
|||
|
<p>
|
|||
|
</p><div class="tip" title="Tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Tip</h3>
|
|||
|
<p>If the data for the matrix is available in a file a convenient way to get hold
|
|||
|
of the dat in the file is to use the utility <code class="code">class ReadFileData</code> to
|
|||
|
get hold of the data using the method</p>
|
|||
|
<p>
|
|||
|
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
|
|||
|
<p><code class="code">ReadFileData::FromMatrix($aFile,$aSeparator=' ')</code>
|
|||
|
</p>
|
|||
|
</li></ul></div><p>
|
|||
|
</p>
|
|||
|
<p>which read the matrix from a file. Each row of the matrix must be a separate
|
|||
|
line and each cell is separated with the character specified as the second
|
|||
|
argument. By default a space is used as separator. All values read back are
|
|||
|
converted to floating point numbers (double precision). The following short
|
|||
|
example shows how easy this is to use</p>
|
|||
|
<p>
|
|||
|
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
|||
|
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$data = ReadFileData::FromMatrix('matrixdata.txt');</span></pre></td></tr></table></div><p>
|
|||
|
</p>
|
|||
|
</div><p>
|
|||
|
</p>
|
|||
|
<p>We will not further discuss all the standard graph formatting options such as the
|
|||
|
ability to add title(s), footers etc. since this is covered in numerous other places in
|
|||
|
this manual.</p>
|
|||
|
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch22.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>
|