]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Chart.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / Chart.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 2007 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with PhpWiki; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /*
25  * Standard Alcatel-Lucent disclaimer for contributing to open source
26  *
27  * "The ChartPlugin ("Contribution") has not been tested and/or
28  * validated for release as or in products, combinations with products or
29  * other commercial use. Any use of the Contribution is entirely made at
30  * the user's own responsibility and the user can not rely on any features,
31  * functionalities or performances Alcatel-Lucent has attributed to the
32  * Contribution.
33  *
34  * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
35  * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
36  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
37  * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
38  * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
39  * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
40  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41  * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
42  * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
43  * ALONE BASIS."
44  */
45
46 class WikiPlugin_Chart
47 extends WikiPlugin
48 {
49     function getName() {
50         return _("Chart");
51     }
52
53     function getDescription() {
54         return _("Render SVG charts");
55     }
56
57     function getVersion() {
58         return preg_replace("/[Revision: $]/", '',
59                             "\$Revision$");
60     }
61
62     function getDefaultArguments() {
63         return array('width'  => 200,
64                      'height' => 200,
65                      'type' => 'line', // or 'area', 'bar', 'pie'
66                      // 'xlabel' => 'x', // TODO
67                      // 'ylabel' => 'y', // TODO
68                      'color' => 'green',
69                      // 'legend' => false, // TODO
70                      'data' => false // mandatory
71                      );
72     }
73     function handle_plugin_args_cruft(&$argstr, &$args) {
74         $this->source = $argstr;
75     }
76
77     function run($dbi, $argstr, &$request, $basepage) {
78
79         global $WikiTheme;
80         $args = $this->getArgs($argstr, $request);
81         extract($args);
82         $html = HTML();
83         $js = JavaScript('', array ('src' => $WikiTheme->_findData('ASCIIsvg.js')));
84         $html->pushContent($js);
85
86         $values = explode(",", $data);
87
88         // x_min = 0
89         // x_max = number of elements in data
90         // y_min = 0 or smallest element in data if negative
91         // y_max = biggest element in data
92
93         $x_max = sizeof($values) + 1;
94         $y_min = min($values);
95         if ($y_min > 0) {
96             $y_min = 0;
97         }
98         $y_max = max($values);
99         // sum is used for the pie only, so we ignore negative values
100         $sum = 0;
101         foreach ($values as $value) {
102             if ($value > 0) {
103                 $sum += $value;
104             }
105         }
106
107         $source = 'initPicture(0,'.$x_max.','.$y_min.','.$y_max.'); axes(); stroke = "'.$color.'"; strokewidth = 5;';
108
109         if ($type == "bar") {
110             $abscisse = 1;
111             $source .= 'strokewidth = 10; ';
112             foreach ($values as $value) {
113                 $source .= 'point1 = ['.$abscisse.', 0];'
114                         .  'point2 = ['.$abscisse.','.$value.'];'
115                         .  'line(point1, point2);';
116                 $abscisse += 1;
117             }
118         } else if ($type == "line") {
119             $abscisse = 0;
120             $source .= 'strokewidth = 3; p = []; ';
121             foreach ($values as $value) {
122                 $source .= 'for (t = 1; t < 1.01; t += 1) p[p.length] = ['
123                         .  $abscisse
124                         .  ', t*'
125                         .  trim($value)
126                         .  '];';
127                 $abscisse += 1;
128             }
129             $source .= 'path(p);';
130         } else if ($type == "pie") {
131             $source = 'initPicture(-1.1,1.1,-1.1,1.1); stroke = "'.$color.'"; strokewidth = 1;'
132                     . 'center = [0, 0]; circle(center, 1);'
133                     . 'point = [1, 0]; line(center, point);';
134             $angle = 0;
135             foreach ($values as $value) {
136                 if ($value > 0) {
137                     $angle += $value/$sum;
138                     $source .= 'point = [cos(2*pi*'.$angle.'), sin(2*pi*'.$angle.')]; line(center, point);';
139                 }
140             }
141         }
142
143         $embedargs = array('width'  => $args['width'],
144                            'height' => $args['height'],
145                            'script' => $source);
146         $embed = new SVG_HTML("embed", $embedargs);
147         $html->pushContent($embed);
148         return $html;
149     }
150 };
151
152 class SVG_HTML extends HtmlElement {
153     function startTag() {
154         $start = "<" . $this->_tag;
155         $this->_setClasses();
156         foreach ($this->_attr as $attr => $val) {
157             if (is_bool($val)) {
158                 if (!$val)
159                     continue;
160                 $val = $attr;
161             }
162             $qval = str_replace("\"", '&quot;', $this->_quote((string)$val));
163             if ($attr == 'script')
164                 // note the ' not "
165                 $start .= " $attr='$qval'";
166             else
167                 $start .= " $attr=\"$qval\"";
168         }
169         $start .= ">";
170         return $start;
171     }
172 }
173
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End:
181 ?>