<?php

/*************************************************************************
 * piechart.class.php                                                    *
 *************************************************************************
 *                                                                       *
 * (c) 2008-2011 Wolf Software Limited <support@wolf-software.com>       *
 * All Rights Reserved.                                                  *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                       *
 *************************************************************************/

class Piechart
{
  private $class_name    = "Piechart";
  private $class_version = "1.0.0";
  private $class_author  = "Wolf Software";
  private $class_source  = "http://www.wolf-software.com/downloads/php-classes/networking-classes/geomap-class/";

  private $username      = null;
  private $password      = null;
  private $database      = null;
  private $hostname      = null;

  private $tables        = null;
  private $totals        = null;

  public function class_name()
    {
      return $this->class_name;
    }

  public function class_version()
    {
      return $this->class_version;
    }

  public function class_author()
    {
      return $this->class_author;
    }

  public function class_source()
    {
      return $this->class_source;
    }

  public function __construct()
    {
    }

  public function draw_chart($details = array(), $title = 'I need a title', $legend = 'bottom', $width = 800, $height = 380, $container = 'chart_div')
    {
      $count = count($details);

      if ($count == 0)
        return;
?>
      <script type="text/javascript" src="https://www.google.com/jsapi"></script>
      <script type="text/javascript">
        google.load("visualization", "1", {packages:["corechart"]});
        google.setOnLoadCallback(drawChart);

        function drawChart()
          {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Label');
            data.addColumn('number', 'count');
            data.addRows(<?php echo $count; ?>);
<?php
            $count = 0;
            foreach($details as $d)
              {
?>
            data.setValue(<?php echo $count;?>, 0, '<?php echo $d['label']; ?>');
            data.setValue(<?php echo $count;?>, 1, <?php echo $d['count']; ?>);
<?php
            $count++;
              }
?>
            var chart = new google.visualization.PieChart(document.getElementById('<?php echo $container; ?>'));
            chart.draw(data, {width: <?php echo $width; ?>, height: <?php echo $height; ?>, title: '<?php echo $title; ?>', legend: '<?php echo $legend; ?>'});
          };
        </script>

<?php

    }
}

?>
