4 * Copyright 2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Standard Alcatel-Lucent disclaimer for contributing to open source
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
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
46 class WikiPlugin_Chart
54 function getDescription()
56 return _("Render SVG charts");
59 function getDefaultArguments()
61 return array('width' => 200,
63 'type' => 'line', // or 'area', 'bar', 'pie'
64 // 'xlabel' => 'x', // TODO
65 // 'ylabel' => 'y', // TODO
67 // 'legend' => false, // TODO
68 'data' => false // required
72 function handle_plugin_args_cruft(&$argstr, &$args)
74 $this->source = $argstr;
77 function run($dbi, $argstr, &$request, $basepage)
81 $args = $this->getArgs($argstr, $request);
83 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'data'));
88 $js = JavaScript('', array('src' => $WikiTheme->_findData('ASCIIsvg.js')));
89 $html->pushContent($js);
91 $values = explode(",", $data);
94 // x_max = number of elements in data
95 // y_min = 0 or smallest element in data if negative
96 // y_max = biggest element in data
98 $x_max = sizeof($values) + 1;
99 $y_min = min($values);
103 $y_max = max($values);
104 // sum is used for the pie only, so we ignore negative values
106 foreach ($values as $value) {
112 $source = 'initPicture(0,' . $x_max . ',' . $y_min . ',' . $y_max . '); axes(); stroke = "' . $color . '"; strokewidth = 5;';
114 if ($type == "bar") {
116 $source .= 'strokewidth = 10; ';
117 foreach ($values as $value) {
118 $source .= 'point1 = [' . $abscisse . ', 0];'
119 . 'point2 = [' . $abscisse . ',' . $value . '];'
120 . 'line(point1, point2);';
123 } elseif ($type == "line") {
125 $source .= 'strokewidth = 3; p = []; ';
126 foreach ($values as $value) {
127 $source .= 'for (t = 1; t < 1.01; t += 1) p[p.length] = ['
134 $source .= 'path(p);';
135 } elseif ($type == "pie") {
136 $source = 'initPicture(-1.1,1.1,-1.1,1.1); stroke = "' . $color . '"; strokewidth = 1;'
137 . 'center = [0, 0]; circle(center, 1);'
138 . 'point = [1, 0]; line(center, point);';
140 foreach ($values as $value) {
142 $angle += $value / $sum;
143 $source .= 'point = [cos(2*pi*' . $angle . '), sin(2*pi*' . $angle . ')]; line(center, point);';
148 $embedargs = array('width' => $args['width'],
149 'height' => $args['height'],
150 'script' => $source);
151 $embed = new SVG_HTML("embed", $embedargs);
152 $html->pushContent($embed);
157 class SVG_HTML extends HtmlElement
161 $start = "<" . $this->_tag;
162 $this->_setClasses();
163 foreach ($this->_attr as $attr => $val) {
169 $qval = str_replace("\"", '"', $this->_quote((string)$val));
170 if ($attr == 'script')
172 $start .= " $attr='$qval'";
174 $start .= " $attr=\"$qval\"";
185 // c-hanging-comment-ender-p: nil
186 // indent-tabs-mode: nil