1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * Image_Graph - PEAR PHP OO Graph Rendering Utility.
7 *
8 * PHP version 5
9 *
10 * LICENSE: This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version. This library is distributed in the hope that it
14 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
16 * General Public License for more details. You should have received a copy of
17 * the GNU Lesser General Public License along with this library; if not, write
18 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 *
21 * @category   Images
22 * @package    Image_Graph
23 * @subpackage Plot
24 * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
25 * @author     Stefan Neufeind <pear.neufeind@speedpartner.de>
26 * @copyright  2003-2009 The PHP Group
27 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
28 * @version    SVN: $Id: Line.php 291406 2009-11-29 00:54:22Z neufeind $
29 * @link       http://pear.php.net/package/Image_Graph
30 */
31
32/**
33 * Include file Image/Graph/Plot.php
34 */
35require_once 'Image/Graph/Plot.php';
36
37/**
38 * Include file Image/Graph/Tool.php
39 */
40require_once 'Image/Graph/Tool.php';
41
42/**
43 * Fit the graph (to a line using linear regression).
44 *
45 * @category   Images
46 * @package    Image_Graph
47 * @subpackage Plot
48 * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
49 * @author     Stefan Neufeind <pear.neufeind@speedpartner.de>
50 * @copyright  2003-2009 The PHP Group
51 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
52 * @version    Release: 0.8.0
53 * @link       http://pear.php.net/package/Image_Graph
54 */
55class Image_Graph_Plot_Fit_Line extends Image_Graph_Plot
56{
57
58    /**
59     * Perform the actual drawing on the legend.
60     *
61     * @param int $x0 The top-left x-coordinate
62     * @param int $y0 The top-left y-coordinate
63     * @param int $x1 The bottom-right x-coordinate
64     * @param int $y1 The bottom-right y-coordinate
65     *
66     * @return void
67     * @access private
68     */
69    function _drawLegendSample($x0, $y0, $x1, $y1)
70    {
71        $y = ($y0 + $y1) / 2;
72        $dy = abs($y1 - $y0) / 6;
73        $this->_canvas->addVertex(array('x' => $x0, 'y' => $y + $dy));
74        $this->_canvas->addVertex(array('x' => $x1, 'y' => $y - $dy));
75        $this->_canvas->polygon(array('connect' => false));
76    }
77
78    /**
79     * Output the plot
80     *
81     * @return bool Was the output 'good' (true) or 'bad' (false).
82     * @access private
83     */
84    function _done()
85    {
86        if (Image_Graph_Plot::_done() === false) {
87            return false;
88        }
89
90        $this->_canvas->startGroup(get_class($this) . '_' . $this->_title);
91        $this->_clip(true);
92        $keys = array_keys($this->_dataset);
93        foreach ($keys as $key) {
94            $dataset =& $this->_dataset[$key];
95            $dataset->_reset();
96            $data = array();
97            while ($point = $dataset->_next()) {
98                $data[] = array(
99                    'X' => $this->_pointX($point),
100                    'Y' => $this->_pointY($point)
101                );
102            }
103
104            $regression = Image_Graph_Tool::calculateLinearRegression($data);
105            $this->_getLineStyle($key);
106            $this->_canvas->line(
107                array(
108                    'x0' => $this->_left,
109                    'y0' => $this->_left * $regression['slope'] + $regression['intersection'],
110                    'x1' => $this->_right,
111                    'y1' => $this->_right * $regression['slope'] + $regression['intersection']
112                )
113            );
114        }
115        $this->_clip(false);
116        $this->_canvas->endGroup();
117
118        return true;
119    }
120}
121
122?>
123