]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Chart.php
rcs_id no longer makes sense with Subversion global version number
[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 getDefaultArguments() {
58         return array('width'  => 200,
59                      'height' => 200,
60                      'type' => 'line', // or 'area', 'bar', 'pie'
61                      // 'xlabel' => 'x', // TODO
62                      // 'ylabel' => 'y', // TODO
63                      'color' => 'green',
64                      // 'legend' => false, // TODO
65                      'data' => false // mandatory
66                      );
67     }
68     function handle_plugin_args_cruft(&$argstr, &$args) {
69         $this->source = $argstr;
70     }
71
72     function run($dbi, $argstr, &$request, $basepage) {
73
74         global $WikiTheme;
75         $args = $this->getArgs($argstr, $request);
76         extract($args);
77         $html = HTML();
78         $js = JavaScript('', array ('src' => $WikiTheme->_findData('ASCIIsvg.js')));
79         $html->pushContent($js);
80
81         $values = explode(",", $data);
82
83         // x_min = 0
84         // x_max = number of elements in data
85         // y_min = 0 or smallest element in data if negative
86         // y_max = biggest element in data
87
88         $x_max = sizeof($values) + 1;
89         $y_min = min($values);
90         if ($y_min > 0) {
91             $y_min = 0;
92         }
93         $y_max = max($values);
94         // sum is used for the pie only, so we ignore negative values
95         $sum = 0;
96         foreach ($values as $value) {
97             if ($value > 0) {
98                 $sum += $value;
99             }
100         }
101
102         $source = 'initPicture(0,'.$x_max.','.$y_min.','.$y_max.'); axes(); stroke = "'.$color.'"; strokewidth = 5;';
103
104         if ($type == "bar") {
105             $abscisse = 1;
106             $source .= 'strokewidth = 10; ';
107             foreach ($values as $value) {
108                 $source .= 'point1 = ['.$abscisse.', 0];'
109                         .  'point2 = ['.$abscisse.','.$value.'];'
110                         .  'line(point1, point2);';
111                 $abscisse += 1;
112             }
113         } else if ($type == "line") {
114             $abscisse = 0;
115             $source .= 'strokewidth = 3; p = []; ';
116             foreach ($values as $value) {
117                 $source .= 'for (t = 1; t < 1.01; t += 1) p[p.length] = ['
118                         .  $abscisse
119                         .  ', t*'
120                         .  trim($value)
121                         .  '];';
122                 $abscisse += 1;
123             }
124             $source .= 'path(p);';
125         } else if ($type == "pie") {
126             $source = 'initPicture(-1.1,1.1,-1.1,1.1); stroke = "'.$color.'"; strokewidth = 1;'
127                     . 'center = [0, 0]; circle(center, 1);'
128                     . 'point = [1, 0]; line(center, point);';
129             $angle = 0;
130             foreach ($values as $value) {
131                 if ($value > 0) {
132                     $angle += $value/$sum;
133                     $source .= 'point = [cos(2*pi*'.$angle.'), sin(2*pi*'.$angle.')]; line(center, point);';
134                 }
135             }
136         }
137
138         $embedargs = array('width'  => $args['width'],
139                            'height' => $args['height'],
140                            'script' => $source);
141         $embed = new SVG_HTML("embed", $embedargs);
142         $html->pushContent($embed);
143         return $html;
144     }
145 };
146
147 class SVG_HTML extends HtmlElement {
148     function startTag() {
149         $start = "<" . $this->_tag;
150         $this->_setClasses();
151         foreach ($this->_attr as $attr => $val) {
152             if (is_bool($val)) {
153                 if (!$val)
154                     continue;
155                 $val = $attr;
156             }
157             $qval = str_replace("\"", '&quot;', $this->_quote((string)$val));
158             if ($attr == 'script')
159                 // note the ' not "
160                 $start .= " $attr='$qval'";
161             else
162                 $start .= " $attr=\"$qval\"";
163         }
164         $start .= ">";
165         return $start;
166     }
167 }
168
169 // Local Variables:
170 // mode: php
171 // tab-width: 8
172 // c-basic-offset: 4
173 // c-hanging-comment-ender-p: nil
174 // indent-tabs-mode: nil
175 // End:
176 ?>