200, 'height' => 200, 'type' => 'line', // or 'area', 'bar', 'pie' // 'xlabel' => 'x', // TODO // 'ylabel' => 'y', // TODO 'color' => 'green', // 'legend' => false, // TODO 'data' => false // required ); } function handle_plugin_args_cruft(&$argstr, &$args) { $this->source = $argstr; } function run($dbi, $argstr, &$request, $basepage) { global $WikiTheme; $args = $this->getArgs($argstr, $request); if (!$args['data']) { return $this->error(sprintf(_("A required argument ā€œ%sā€ is missing."), 'data')); } extract($args); $html = HTML(); $js = JavaScript('', array('src' => $WikiTheme->_findData('ASCIIsvg.js'))); $html->pushContent($js); $values = explode(",", $data); // x_min = 0 // x_max = number of elements in data // y_min = 0 or smallest element in data if negative // y_max = biggest element in data $x_max = sizeof($values) + 1; $y_min = min($values); if ($y_min > 0) { $y_min = 0; } $y_max = max($values); // sum is used for the pie only, so we ignore negative values $sum = 0; foreach ($values as $value) { if ($value > 0) { $sum += $value; } } $source = 'initPicture(0,' . $x_max . ',' . $y_min . ',' . $y_max . '); axes(); stroke = "' . $color . '"; strokewidth = 5;'; if ($type == "bar") { $abscisse = 1; $source .= 'strokewidth = 10; '; foreach ($values as $value) { $source .= 'point1 = [' . $abscisse . ', 0];' . 'point2 = [' . $abscisse . ',' . $value . '];' . 'line(point1, point2);'; $abscisse += 1; } } elseif ($type == "line") { $abscisse = 0; $source .= 'strokewidth = 3; p = []; '; foreach ($values as $value) { $source .= 'for (t = 1; t < 1.01; t += 1) p[p.length] = [' . $abscisse . ', t*' . trim($value) . '];'; $abscisse += 1; } $source .= 'path(p);'; } elseif ($type == "pie") { $source = 'initPicture(-1.1,1.1,-1.1,1.1); stroke = "' . $color . '"; strokewidth = 1;' . 'center = [0, 0]; circle(center, 1);' . 'point = [1, 0]; line(center, point);'; $angle = 0; foreach ($values as $value) { if ($value > 0) { $angle += $value / $sum; $source .= 'point = [cos(2*pi*' . $angle . '), sin(2*pi*' . $angle . ')]; line(center, point);'; } } } $embedargs = array('width' => $args['width'], 'height' => $args['height'], 'script' => $source); $embed = new SVG_HTML("embed", $embedargs); $html->pushContent($embed); return $html; } } class SVG_HTML extends HtmlElement { function startTag() { $start = "<" . $this->_tag; $this->_setClasses(); foreach ($this->_attr as $attr => $val) { if (is_bool($val)) { if (!$val) continue; $val = $attr; } $qval = str_replace("\"", '"', $this->_quote((string)$val)); if ($attr == 'script') // note the ' not " $start .= " $attr='$qval'"; else $start .= " $attr=\"$qval\""; } $start .= ">"; return $start; } } // Local Variables: // mode: php // tab-width: 8 // c-basic-offset: 4 // c-hanging-comment-ender-p: nil // indent-tabs-mode: nil // End: