<?php if (!defined('PmWiki')) exit();
/*
 * LibChart - charting recipe using real OSS for PmWiki 2.x
 * Copyright 2006 by Breyten ernsting (bje@dds.nl)
 * libchart written by and (C) 2005-2006 Jean-Marc Trémeaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * See http://www.pmwiki.org/wiki/Cookbook/LibChart for info.
 */
define(LIBCHART_VERSION, '0.1');

Markup('chartdir','directives',
        "/\\(:(chart|chartdir)\\s*(.*?):\\)/e",
        "libchartMakeChart('$2')");

//Markup('chart','<inline',
//        "/\\(:chart\\s*(.*?):\\)/e",
//        "makeChart('$1')");

/*
 * makeImgId - make an image id
 * /param $data Data points
 * /param $labels Data labels
 */
function libchartMakeImgId($data, $labels, $chart_type) {
  $datastr = is_array($data) ? implode(",", $data) : $data;
  $labelsstr = is_array($labels) ? implode(",", $labels) : $labels;
  return md5("data:$datastr:labels:$labelsstr") ."-$chart_type";
}

function libchartMakeChart($opts) {
  global $FarmD, $PubDirUrl;
  $args = ParseArgs("$opts");
  // $args = key value pairs
  $corr = (array_key_exists('data', $args) && array_key_exists('labels', $args));
  if ($corr) {
    $chart_type = array_key_exists('type', $args) ? $args['type'] : 'VerticalChart';
    $imgid = libchartMakeImgId($args['data'], $args['labels'], $chart_type);
    $imgloc = "libchart/$imgid.png";
    $imgfile = "$FarmD/pub/$imgloc";
    if (!file_exists($imgfile)) {
      include_once(dirname(__FILE__) . "/libchart-1.1/libchart/libchart.php");
      $title = array_key_exists('title', $args) ? $args['title'] : '';
      $data = preg_split('/\s*,\s*/', $args['data']);
      $labels = preg_split('/\s*,\s*/', $args['labels']);
      $chart_width = array_key_exists('width', $args) ? $args['width'] : 400;
      $chart_height = array_key_exists('height', $args) ? $args['height'] : 300;
      $chart = new $chart_type($chart_width, $chart_height);
      $maxelems = (count($data) >= count($labels)) ? count($data) : count($labels);
      for($i=0;$i<$maxelems;$i++) {
        $chart->addPoint(new Point($labels[$i], $data[$i]));
      }
      if ($title != '') {
        $chart->setTitle($title);
      }
      $chart->render($imgfile);
    }
    return "$PubDirUrl/$imgloc";
  } else {
    return "ERROR: invalid chart specification.";
  }
}