ch14s09.html 14.3 KB
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Using a logarithmic scale</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="ch14.html" title="Chapter 14. Common features for all Cartesian (x,y) graph types"></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">Using a logarithmic scale</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 14. Common features for all Cartesian (x,y) graph types</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Using a logarithmic scale"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2548533"></a>Using a logarithmic scale</h2></div></div></div>
            
            <p>In all the previous discussion we have used linear scales. Another option is to
                use a logarithmic scale. This is often used when there is a need to display both
                very small and very large values in the same graph. The library supports the use of
                logarithmic scales on both the x- and y-axis (as well as the optional extra
                y-axis).</p>
            <p>The logarithmic scale support is defined in the module file
                    "j<code class="filename">pgraph_log.php</code>" so this must be included in order to
                access this feature.</p>
            <p>To illustrate the use of a logarithmic scale we will make a graph which uses two
                y-axis and make the second y-axis have a logarithmic scale (we will actually take
                the graph in <a class="xref" href="ch15.html#fig.example6" title="Figure 15.16. Adding and adjusting the position of the legend box (example6.php)">Figure 15.16. Adding and adjusting the position of the legend box <code class="uri"><a class="uri" href="example_src/example6.html" target="_top">(<code class="filename">example6.php</code>)</a></code> </a> and change the second y-axis to be a
                logarithmic scale instead).</p>
            <p>In order to use a logarithmic scale on the second (Y2) axis we only need to
                change</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">$graph-&gt;SetY2Scale('lin');</span></pre></td></tr></table></div><p>
            </p>
            <p>to</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">$graph-&gt;SetY2Scale('log');</span></pre></td></tr></table></div><p>
            </p>
            <p>and the library will take care of the rest. The result of this is shown in <a class="xref" href="ch14s09.html#fig.example7" title="Figure 14.59. Changing the Y2 scale from linear to logarithmic (example7.php)">Figure 14.59. Changing the Y2 scale from linear to logarithmic <code class="uri"><a class="uri" href="example_src/example7.html" target="_top">(<code class="filename">example7.php</code>)</a></code> </a></p>
            <p>
                </p><div class="figure"><a name="fig.example7"></a><p class="title"><b>Figure 14.59. Changing the Y2 scale from linear to logarithmic <code class="uri"><a class="uri" href="example_src/example7.html" target="_top">(<code class="filename">example7.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/example7.png" alt="Changing the Y2 scale from linear to logarithmic (example7.php)"></span> </div></div><p><br class="figure-break">
            </p>
            <p>By default only the major grid lines on the y-axis are shown. By adding the
                lines</p>
            <p>
                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$graph-&gt;ygrid-&gt;Show(true,true);
$graph-&gt;xgrid-&gt;Show(true,false);</span></pre></td></tr></table></div><p>
            </p>
            <p>We can enable both the x-grid lines as well as the minor grid lines on the y-axis
                to get the result shown in <a class="xref" href="ch14s09.html#fig.example8" title="Figure 14.60. Enabling minor grid lines on the y-axis and also grid lines on the x-axis (example8.php)">Figure 14.60. Enabling minor grid lines on the y-axis and also grid lines on the x-axis <code class="uri"><a class="uri" href="example_src/example8.html" target="_top">(<code class="filename">example8.php</code>)</a></code> </a> below</p>
            <p>
                </p><div class="figure"><a name="fig.example8"></a><p class="title"><b>Figure 14.60. Enabling minor grid lines on the y-axis and also grid lines on the x-axis <code class="uri"><a class="uri" href="example_src/example8.html" target="_top">(<code class="filename">example8.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/example8.png" alt="Enabling minor grid lines on the y-axis and also grid lines on the x-axis (example8.php)"></span> </div></div><p><br class="figure-break">
            </p>
            <p>In addition to using a logarithmic scale on the y-axis to generate what is
                commonly known as a "lin-log" scale it is also possible to use a logarithmic scale
                on the x-axis to get a "log-log" plot (as is often used in electrical engineering).
                An example of this is shown in <a class="xref" href="ch14s09.html#fig.loglogex1" title="Figure 14.61. An example of a log-log plot (where both the y- and x-axis use a logarithmic scale) (loglogex1.php)">Figure 14.61. An example of a log-log plot (where both the y- and x-axis use a logarithmic scale) <code class="uri"><a class="uri" href="example_src/loglogex1.html" target="_top">(<code class="filename">loglogex1.php</code>)</a></code> </a></p>
            <p>
                </p><div class="figure"><a name="fig.loglogex1"></a><p class="title"><b>Figure 14.61. An example of a log-log plot (where both the y- and x-axis use a logarithmic scale) <code class="uri"><a class="uri" href="example_src/loglogex1.html" target="_top">(<code class="filename">loglogex1.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/loglogex1.png" alt="An example of a log-log plot (where both the y- and x-axis use a logarithmic scale) (loglogex1.php)"></span> </div></div><p><br class="figure-break">
            </p>
            <p>
                </p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3>
                    <p>The example in <a class="xref" href="ch14s09.html#fig.loglogex1" title="Figure 14.61. An example of a log-log plot (where both the y- and x-axis use a logarithmic scale) (loglogex1.php)">Figure 14.61. An example of a log-log plot (where both the y- and x-axis use a logarithmic scale) <code class="uri"><a class="uri" href="example_src/loglogex1.html" target="_top">(<code class="filename">loglogex1.php</code>)</a></code> </a> also makes use of
                        scatter plots which we have not yet introduced but is a way to draw a plot
                        of a number of data points specified by both there x- and
                        y-coordinates.</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 you think the first value of the Y-axis is to close to the first label
                        of the X-axis you have the option of either increasing the margin (with a
                        call to <code class="code">SetLabelMargin()</code> ) or to hide the first label (with a
                        call to <code class="code">Axis::HideFirstTickLabel()</code> )</p>
                </div><p>
            </p>
            <p>As a final example of using a logarithmic scale we show how to combine a text
                scale for the x-axis and a logarithmic scale for the y-axis. As we already shown in
                    <a class="xref" href="ch04s02.html" title="Graphing the number of sun spots during the 19th Century">Graphing the number of sun spots during the 19th Century</a> it is possible to manually specify
                the labels that should be used on the axis with a call to Axis::SetTickLabels().
                Let's make use of this and the built-in library super global variable
                    "<code class="code">$gDateLocale</code>" which is an instance of a date utility class to get
                a list of the name of all the months in a localized fashion.</p>
            <p>To use the name of the months on the x-axis we haev to add the following two lines
                to the previous example</p>
            <p>
                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
</span><span class="hl-var">$montnames</span><span class="hl-code"> = </span><span class="hl-var">$gDateLocale</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetShortMonth</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$graph</span><span class="hl-code">-&gt;</span><span class="hl-identifier">xaxis</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetTickLabels</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$montnames</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">; 
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
            </p>
            <p>The result of adding these two lines are shown in <a class="xref" href="ch14s09.html#fig.example9" title="Figure 14.62. Using a text-log scale (example9.php)">Figure 14.62. Using a text-log scale <code class="uri"><a class="uri" href="example_src/example9.html" target="_top">(<code class="filename">example9.php</code>)</a></code> </a> below</p>
            <p>
                </p><div class="figure"><a name="fig.example9"></a><p class="title"><b>Figure 14.62. Using a text-log scale <code class="uri"><a class="uri" href="example_src/example9.html" target="_top">(<code class="filename">example9.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/example9.png" alt="Using a text-log scale (example9.php)"></span> </div></div><p><br class="figure-break">
            </p>
            <p>Since the name of the months are quite close we can change what labels should be
                displayed as was discussed in <a class="xref" href="ch14s08.html#sec2.manually-spec-labels" title="Manually specifying scale labels">Manually specifying scale labels</a> to
                only show every second label. Doing this change gives the result shown in <a class="xref" href="ch14s09.html#fig.example9.1" title="Figure 14.63. Adjusting the text scale so that only every second labels are displayed. (example9.1.php)">Figure 14.63. Adjusting the text scale so that only every second labels are displayed. <code class="uri"><a class="uri" href="example_src/example9.1.html" target="_top">(<code class="filename">example9.1.php</code>)</a></code> </a></p>
            <p>
                </p><div class="figure"><a name="fig.example9.1"></a><p class="title"><b>Figure 14.63. Adjusting the text scale so that only every second labels are displayed. <code class="uri"><a class="uri" href="example_src/example9.1.html" target="_top">(<code class="filename">example9.1.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/example9.1.png" alt="Adjusting the text scale so that only every second labels are displayed. (example9.1.php)"></span> </div></div><p><br class="figure-break">
            </p>
            <p>Another option if we think that the labels are too close is to rotate them. In
                    <a class="xref" href="ch14s09.html#fig.example9.2" title="Figure 14.64. Rotating the x-axis labels 90 degree (example9.2.php)">Figure 14.64. Rotating the x-axis labels 90 degree <code class="uri"><a class="uri" href="example_src/example9.2.html" target="_top">(<code class="filename">example9.2.php</code>)</a></code> </a> we have kept all labels but rotated them 90
                degree.</p>
            <p>
                </p><div class="figure"><a name="fig.example9.2"></a><p class="title"><b>Figure 14.64. Rotating the x-axis labels 90 degree <code class="uri"><a class="uri" href="example_src/example9.2.html" target="_top">(<code class="filename">example9.2.php</code>)</a></code> </b></p><div class="figure-contents">  <span class="inlinemediaobject"><img src="images/example9.2.png" alt="Rotating the x-axis labels 90 degree (example9.2.php)"></span> </div></div><p><br class="figure-break">
            </p>
            <p>
                </p><div class="caution" title="Caution" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Caution</h3>
                    <p>Remember that the built-in bitmap fonts only supports 0 and 90 degree
                        text. TTF fonts support texts at an arbitrary angle.</p>
                </div><p>
            </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="ch14.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>