Advertisement
alxbuk

charts.plugin.php

Nov 25th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.35 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.      *  Charts plugin
  5.      *
  6.      *  @package Monstra
  7.      *  @subpackage Plugins
  8.      *  @author Romanenko Sergey / Awilum
  9.      *  @copyright 2012 Romanenko Sergey / Awilum
  10.      *  @version 1.0.0
  11.      *
  12.      */
  13.  
  14.  
  15.     // Register plugin
  16.     Plugin::register( __FILE__,                    
  17.                     __('Charts'),
  18.                     __('Charts plugin for Monstra.'),  
  19.                     '1.0.0',
  20.                     'Awilum',                
  21.                     'http://monstra.org');
  22.  
  23.  
  24.  
  25.     // Add hook
  26.     Action::add('theme_header', 'Charts::themesHeaders');
  27.  
  28.     // Add shortcode
  29.     Shortcode::add('chart', 'Charts::_shortcode');
  30.  
  31.  
  32.         /**
  33.          * Charts Shortcode
  34.          *
  35.          *  <code>
  36.          *      {chart data="Mushrooms,3|Zucchini,5|Pepperoni,1"}
  37.          *
  38.          *      {chart width="640" height="480" data="Mushrooms,3|Zucchini,5|Pepperoni,1"}
  39.          *
  40.          *      {chart width="640" height="480" title="How Much Pizza I Ate Last Night" data="Mushrooms,3|Zucchini,5|Pepperoni,1"}
  41.          *
  42.          *      {chart width="640" height="480" title="How Much Pizza I Ate Last Night" type="bar" data="Mushrooms,3|Zucchini,5|Pepperoni,1"}
  43.          *  </code>
  44.          *
  45.          */
  46.     class Charts {
  47.        
  48.  
  49.         /**
  50.          * Charts Headers
  51.          */
  52.         public static function themesHeaders() {
  53.             echo ('');
  54.         }
  55.    
  56.  
  57.         /**
  58.          * Charts Shortcode
  59.          */
  60.         public static function _shortcode($attributes) {
  61.            
  62.             // Extract
  63.             extract($attributes);
  64.  
  65.             // UID
  66.             $uid = Text::random();        
  67.  
  68.             // Data
  69.             if (isset($data)) {
  70.                 $data_string = '';            
  71.                 $_data = explode('|', $data);
  72.                 foreach($_data as $d) {
  73.                     $part = explode(',', $d);                
  74.                     $data_string .= '["'.$part[0].'", '.$part[1].'],';
  75.                 }
  76.                
  77.                 $data = $data_string;
  78.             } else {
  79.                 $data = '["test", 1],';
  80.             }
  81.  
  82.             // Title
  83.             if (isset($title)) $title = $title; else $title = 'Charts';
  84.  
  85.             // Type
  86.             if (isset($type) && ($type == 'pie' || $type == 'bar')) $type = $type; else $type = 'pie';
  87.  
  88.             // Width
  89.             if (isset($width)) $width = $width; else $width = 400;
  90.  
  91.             // Height
  92.             if (isset($height)) $height = $height; else $height = 300;
  93.  
  94.             // Chart
  95.             return ('
  96.             <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  97.             <script type="text/javascript">
  98.                       google.load("visualization", "1.0", {"packages":["corechart"]});
  99.                       google.setOnLoadCallback(drawChart_'.$uid.');
  100.                       function drawChart_'.$uid.'() {
  101.                         var data = new google.visualization.DataTable();
  102.                         data.addColumn("string", "Topping");
  103.                         data.addColumn("number", "Slices");
  104.                         data.addRows([
  105.                           '.$data.'
  106.                         ]);
  107.                         var options = {\'title\':\''.$title.'\',
  108.                                        \'width\':'.(int)$width.',
  109.                                        \'height\':'.(int)$height.'};
  110.                         var chart = new google.visualization.'.ucfirst($type).'Chart(document.getElementById(\'chart_div_'.$uid.'\'));
  111.                         chart.draw(data, options);
  112.                       }
  113.                     </script>
  114.                     <div id="chart_div_'.$uid.'"></div>');      
  115.  
  116.         }
  117.  
  118.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement