ch13s02.html 11 KB
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Reading data from a file</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="ch13.html" title="Chapter 13. Getting hold of the data to be displayed"></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">Reading data from a file</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 13. Getting hold of the data to be displayed</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Reading data from a file"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2537882"></a>Reading data from a file</h2></div></div></div>
            
            <p>The second method in order of complexity is to read the data from plain text
                files. An example on how to use this was shown in <a class="xref" href="ch04s02.html#sec.preparing-sunspots-data" title="Preparing the data">Preparing the data</a>. The library contains utility
                methods to ease reading of plain textual data in one of the following
                formats:</p>
            <p>
                </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
                        <p>One column</p>
                    </li><li class="listitem">
                        <p>Two columns</p>
                    </li><li class="listitem">
                        <p>Comma separated values, CSV</p>
                    </li></ol></div><p>
            </p>
            <p>The utility class to handle this is called <code class="code">Class ReadFileData</code> and
                contains three utility methods corresponding to the list above, they are
                    <code class="code">ReadFileData::From1Col()</code>, <code class="code">ReadFileData::From2Col()</code>,
                    <code class="code">ReadFileData::FromCSV()</code> and
                <code class="code">ReadFileData::FromCSV2()</code>.</p>
            <p>These methods are described shortly below</p>
            <p>
                </p><div class="variablelist"><dl><dt><span class="term"><code class="code">ReadFileData::From1Col($aFileName, $aCol1)</code></span></dt><dd>
                            <p>Reads data from a text file with one column of data and stores in
                                the supplied <code class="code">$aCol1</code> vector.</p>
                            <p>Typical data looks like</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
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">123
14.5
19.2</span></pre></td></tr></table></div><p>
                            </p>
                            <p>which would result in </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">$aCol == array(123, 14.5, 19,2)</span></pre></td></tr></table></div><p>
                            </p>
                        </dd><dt><span class="term"><code class="code">ReadFileData::From2Col($aFile, $aCol1, $aCol2, $aSepChar='
                                ')</code></span></dt><dd>
                            <p>Reads data from a text file with two columns separated by the
                                specified character.</p>
                            <p>Typical data looks like</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
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">12,15
13,34
14,27</span></pre></td></tr></table></div><p>
                            </p>
                            <p>which would result in</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">$aCol1 == array(12,13,14);
$aCol2 == array(15,34,27);</span></pre></td></tr></table></div><p>
                            </p>
                        </dd><dt><span class="term">ReadFileData::FromCSV($aFile,&amp;$aData,$aSepChar=',',$aMaxLineLength=1024)</span></dt><dd>
                            <p>This method reads comma separated values from a specified file.
                                The values are all separeted by the specified character. This method
                                can be seen as a generalization of From1Col() method.</p>
                            <p>Typical data looks like</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">12,34,56,18,19.7,55</span></pre></td></tr></table></div><p>
                            </p>
                            <p>which would result in</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">$aData == array(12,34,56,18,19,7,55)</span></pre></td></tr></table></div><p>
                            </p>
                        </dd><dt><span class="term">ReadFileData::FromCSV2($aFile, &amp;$aData, $aOptions =
                            array())</span></dt><dd>
                            <p>This method also reads comma separated values from a file but with
                                more advanced options to control how the data is read. This can be
                                seen as a generalization of From2Col() method.</p>
                            <p>The possible options and there default values for this method
                                are</p>
                            <p>
                                </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                                        <p>'separator' =&gt; ','</p>
                                    </li><li class="listitem">
                                        <p>'enclosure' =&gt; '"'</p>
                                    </li><li class="listitem">
                                        <p>'readlength' =&gt; 1024</p>
                                    </li><li class="listitem">
                                        <p>'ignore_first' =&gt; false</p>
                                    </li><li class="listitem">
                                        <p>'first_as_key' =&gt; false</p>
                                    </li></ul></div><p>
                            </p>
                            <p>Typical data (using the default values)</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
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">10,55
12,78
15,98</span></pre></td></tr></table></div><p>
                            </p>
                            <p>would result in</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-code">$aData = array(
  0 =&gt; array(10,12,15),
  1 =&gt; array(55,78,98)
);</span></pre></td></tr></table></div><p>
                            </p>
                            <p>If 'first_as_key'=&gt;true and the data looks looks like</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-code">&quot;key&quot;,&quot;value&quot;
10,55
12,78
15,98</span></pre></td></tr></table></div><p>
                            </p>
                            <p>the data would instead be read as</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-code">$aData = array(
  &quot;key&quot;   =&gt; array(10,12,15),
  &quot;value&quot; =&gt; array(55,78,98)
);</span></pre></td></tr></table></div><p>
                            </p>
                        </dd><dt><span class="term">ReadFileData::FromMatrix($aFile,$aSepChar=' ')</span></dt><dd>
                            <p>This method is especially suited o read matrix data from a file
                                for use with the Matrix visualization (described in <a class="xref" href="ch22.html" title="Chapter 22. Matrix graphs">Chapter 22. <i>Matrix graphs</i></a>). Each line in the file
                                corresponds to one row in the matrix.</p>
                            <p>Typical data can look like</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
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">13,87,12
15,99,33
19,86,61</span></pre></td></tr></table></div><p>
                            </p>
                            <p>Which wold return a matrix looking like</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
5
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">array( 
  array(13,87,12),
  array(15,99,33),
  array(19,86,61)
);</span></pre></td></tr></table></div><p>
                            </p>
                        </dd></dl></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="ch13.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>