Author: sebsecond2 Date: 2010-09-10 17:16:54 +0200 (Fri, 10 Sep 2010) New Revision: 30878
Added: plugins/sdInteractiveChartPlugin/trunk/ plugins/sdInteractiveChartPlugin/trunk/LICENSE plugins/sdInteractiveChartPlugin/trunk/README plugins/sdInteractiveChartPlugin/trunk/config/ plugins/sdInteractiveChartPlugin/trunk/config/app.yml plugins/sdInteractiveChartPlugin/trunk/config/config.php plugins/sdInteractiveChartPlugin/trunk/lib/ plugins/sdInteractiveChartPlugin/trunk/lib/helper/ plugins/sdInteractiveChartPlugin/trunk/lib/helper/sdInteractiveChartHelper.php plugins/sdInteractiveChartPlugin/trunk/lib/sdAreaGraph.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdAxisBaseChart.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdBarGraph.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdBaseChart.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdColumnGraph.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdGaugeGraph.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdInteractiveChartConfig.php plugins/sdInteractiveChartPlugin/trunk/lib/sdLineGraph.class.php plugins/sdInteractiveChartPlugin/trunk/lib/sdPieGraph.class.php plugins/sdInteractiveChartPlugin/trunk/package.xml plugins/sdInteractiveChartPlugin/trunk/sdInteractiveChartPlugin-0.2.0.tgz plugins/sdInteractiveChartPlugin/trunk/web/ plugins/sdInteractiveChartPlugin/trunk/web/js/ plugins/sdInteractiveChartPlugin/trunk/web/js/.htaccess plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0-min.js plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0.js Log: Initial Version added to the Symfony Repo. - Effectively the same as the version 0.2.0 Added: plugins/sdInteractiveChartPlugin/trunk/LICENSE =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/LICENSE (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/LICENSE 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,19 @@ +Copyright (c) 2010 Seb Dangerfield - Second2 Ltd + + 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. Added: plugins/sdInteractiveChartPlugin/trunk/README =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/README (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/README 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,180 @@ +# sdInteractiveChartPlugin +**Version 0.2** + +##Overview + +This plugin allows you to use Google's Visualization API without writing any javascript, +it simplifies the creation of charts and makes the code a lot neater which is especially useful +if your planning to include more than one chart in a page. + +It allows you to create charts by only writing PHP, all the +Javascript code is dealt with by the plugin. As many charts as you want can be +included in each page, it will handle the creation for each one as well as loading +in any required libraries. + +It can create a number of different charts: + +* Area Charts +* Bar Charts +* Column Charts +* Gauges +* Line Graphs +* Pie Charts + +Timeline graphs will be added in a future version. + +###Requirements: + +This plugin requires jQuery, only tested with 1.4.2 although should work with +earlier versions as none of the jquery functions used are new ones. + +##How to Include + +###Installation + +To install simply use: + + $ symfony plugin:install -s"beta" sdInteractiveChartPlugin + +If this doesn't work (which for some reason happens with some versions of symfony) +just download the package to your local machine and then use: + + $ symfony plugin:install /path/to/plugin/sdInteractiveChartPlugin-0.2.0.tgz + +Clear the cache: + + $ symfony cache:clear + +Enable the plugin (in the application's *settings.yml*): + + enabled_modules: [default, sdInteractiveChart ... + +###Assets + +Publish the plugin's assets: + + plugin:publish-assets + + + +###Including the Javascript + +The plugin will automatically add the required javascript file to the bottom of +the head of the page once everything else has loaded. + +If you would prefer to include it yourself then you need to edit the *app.yml* file +under the config directory: + + all: + sdInteractiveChart: + web_dir: /sdInteractiveChartPlugin + chart_js_url: 'http://www.google.com/' + ajax_api: 'jsapi' + auto_load_js: true + debug_mode: false + +Change the line *auto_load_js* to false. + +Then when you want to load the javascript you will need to use: + + <?php use_helper('sdInteractiveChart'); ?> + <?php addInteractiveChartJavascript(); ?> + + +##Creating Charts + +The helper class **sdInteractiveChart** is a static class and exposes a list of functions for creating new +charts, each one returns a chart object of the type requested: + +* newBarChart -- (returns: sdBarGraph object) +* newLineChart -- (returns: sdLineGraph object) +* newAreaChart -- (returns: sdAreaGraph object) +* newColumnChart -- (returns: sdColumnGraph object) +* newGuageChart -- (returns: sdGaugeGraph object) +* newPieChart -- (returns: sdPieGraph object) + +It also contains the special function: + +* generateJsonData -- (returns: formated JSON object) + +Which allows you to return chart data requested via ajax to the plugin. + +Using the chart object returned by the helper class you can set the chart, and +specify colors and labels and so fourth. + +###Chart Data + +You can either directly specify the data for the chart. Or you can tell it to +retrieve the data via ajax. + +####Inline Data + + $chart->inlineGraph($data, $labels, $divName) + $chart->inlineGraph(array('hits' => array(1,9,5)), array('Monday', 'Tuesday', 'Wednesday'), 'chart_div'); + +####Ajax Request + + $chartFun = $chart->ajaxGraph($ajax_url, $extra_ajax_params, $divName); + $chartFun = $chart->ajaxGraph( url_for('graphs/data'), array('some_extra_param' => 'hello'), 'chart_div'); + +The *$divName* is the ID of the div you would like the chart to be created in. +Any content in there will be overwritten. + +###Rendering the Chart + +Once your ready for the chart code to be pasted into the page simply call the +*render()* method of the chart object. + + +##Example A (Inline Data) + +This example creates a simple area chart. See http://code.google.com/apis/visualization/documentation/gallery/areachart.html + + use_helper('sdInteractiveChart'); + $chart = InteractiveChart::newAreaChart(); + $chart->setWidthAndHeight('400', '240'); + $chart->setDataColors(array('#aa0000')); + $chart->setBaselineColor('#ccc'); + $chart->inlineGraph(array('hits' => array(1,9,5)), array('Monday', 'Tuesday', 'Wednesday'), 'chart_div'); + $chart->render(); + +##Example B (Ajax Data) + +This code would go in the page template/action: + + use_helper('sdInteractiveChart'); + $chart = InteractiveChart::newBarChart(); + $chart->setWidthAndHeight('500', '320'); + $chart->setDataColors(array('#990000', '#cd0000')); + $chart->setBaselineColor('#aaa'); + $chart->setLegendPosition(LineGraph::$LEGEND_NONE); + $chart->ajaxGraph( url_for('graphs/load_data'), array(), 'chart_div'); + $chart->render(); + +Then this would go in the *graphs/load_data* action/template: + + // The labels for each Bar. + $labels = array("Apples", "Oranges", "Pears", "Grapes"); + + // The Data + $data = array('Last Week' => array(1, 5, 3, 7), 'This Week' => array(8, 4, 5, 2)); + use_helper('sdInteractiveChart'); + + // This line then outputs the JSON array to the browser. + echo InteractiveChart::generateJsonData($data, $labels, array()); + +##Callback + +If you want to know when the chart has been rendered on screen you can pass the +name of a function to the *setCallback* function of your chart object. + + $chart->setCallback('graphLoaded'); + +The chart object will be passed to the function which will contain all the info +on the chart. + +##Feedback + +If you find any bugs or have any ideas for any new features which could be added just drop me an email. +Or alternatively update your local copy of the code, then goto the *contribute* tab for this plugin and +request to join the project as a developer! Added: plugins/sdInteractiveChartPlugin/trunk/config/app.yml =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/config/app.yml (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/config/app.yml 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,10 @@ +all: + sdInteractiveChart: + web_dir: /sdInteractiveChartPlugin + chart_js_url: 'http://www.google.com/' + ajax_api: 'jsapi' + auto_load_js: true + debug_mode: false + + + Added: plugins/sdInteractiveChartPlugin/trunk/config/config.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/config/config.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/config/config.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,12 @@ +<?php + + +if (in_array('sdInteractiveChart', sfConfig::get('sf_enabled_modules', array()))) +{ + // If auto_load_js is enabled then once the context has been initiated + // we can add in the javascipt files we require! + // NOTE: Chart specific ones get auto loaded once the page has loaded! + if (sfConfig::get('app_sdInteractiveChart_auto_load_js')) + $this->dispatcher->connect('context.load_factories', array('sdInteractiveChartConfig', 'listenToContextLoadFactoriesEvent')); +} +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/helper/sdInteractiveChartHelper.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/helper/sdInteractiveChartHelper.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/helper/sdInteractiveChartHelper.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,100 @@ +<?php +/** + * InteractiveChart Helper Class. Used to create new objects of any of the + * different chart objects + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield - Second2 + * @version 0.2 + */ + +define('InteractiveChartRoute', dirname(__FILE__).'/../'); +require_once(InteractiveChartRoute . 'sdBaseChart.class.php'); + +class InteractiveChart { + + + + static function newBarChart() { + require_once(InteractiveChartRoute . 'sdAxisBaseChart.class.php'); + require_once(InteractiveChartRoute . 'sdBarGraph.class.php'); + return new BarGraph(); + } + + static function newLineChart() { + require_once(InteractiveChartRoute . 'sdAxisBaseChart.class.php'); + require_once(InteractiveChartRoute . 'sdLineGraph.class.php'); + return new LineGraph(); + } + + static function newAreaChart() { + require_once(InteractiveChartRoute . 'sdAxisBaseChart.class.php'); + require_once(InteractiveChartRoute . 'sdLineGraph.class.php'); + require_once(InteractiveChartRoute . 'sdAreaGraph.class.php'); + return new AreaGraph(); + } + + static function newColumnChart() { + require_once(InteractiveChartRoute . 'sdAxisBaseChart.class.php'); + require_once(InteractiveChartRoute . 'sdBarGraph.class.php'); + require_once(InteractiveChartRoute . 'sdColumnGraph.class.php'); + return new ColumnGraph(); + } + + static function newGuageChart() { + require_once(InteractiveChartRoute . 'sdGaugeGraph.class.php'); + return new GaugeGraph(); + } + + static function newPieChart() { + require_once(InteractiveChartRoute . 'sdPieGraph.class.php'); + return new PieGraph(); + } + + + + static function generateJsonData(&$data, $chartLabels, $extraData = array()) { + $result = $extraData; + $result['dataNames'] = array(); + $result['data'] = array(); + + $result['labels'] = $chartLabels; + // if we were passed a single array of values we need to wrap it + // in another array! + if (!is_array(current($data))) { + $result['data'][0] = $data; + } else { + + foreach ($data as $key=>$val) { + if (!is_string($key)) + $key = '' . $key; + + array_push($result['dataNames'], $key); + array_push($result['data'], $val); + } + } + + return json_encode($result); + } + + + + + +} + + +function addInteractiveChartJavascript() { + $version = '0.2.0'; + $ajax_api_url = sfConfig::get('app_sdInteractiveChart_chart_js_url'); + $ajax_api = sfConfig::get('app_sdInteractiveChart_ajax_api'); + sfContext::getInstance()->getResponse()->addJavascript($ajax_api_url . $ajax_api); + + if (sfConfig::get('app_sdInteractiveChart_debug_mode')) + sfContext::getInstance()->getResponse()->addJavascript(sfConfig::get('app_sdInteractiveChart_web_dir') . "/js/interactiveCharts$version.js", 'last'); + else + sfContext::getInstance()->getResponse()->addJavascript(sfConfig::get('app_sdInteractiveChart_web_dir') . "/js/interactiveCharts$version-min.js", 'last'); +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdAreaGraph.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdAreaGraph.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdAreaGraph.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,21 @@ +<?php +/** + * sdLineChart - Class for creating area graphs. Extends the LineChart + * Google API: http://code.google.com/apis/visualization/documentation/gallery/areachart.html + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield - Second2 + * @version 0.2 + */ + +class AreaGraph extends LineGraph { + protected $chartType = 'AreaChart'; + + public function __construct() { + unset($this->setSmoothLines); + $this->chartPackage = 'corechart'; + } +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdAxisBaseChart.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdAxisBaseChart.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdAxisBaseChart.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,135 @@ +<?php +/** + * sdAxisBaseChart functions used by all charts that have the usual two + * axis, used by line, bar, column charts. + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield - Second2 + * @version 0.2 + */ + +class AxisBaseChart extends BaseChart { + protected $legend = null; + protected $legendTextStyle = null; + protected $hAxis = null; + protected $vAxis = null; + protected $title = null; + protected $titleTextStyle = null; + protected $reverseCategories = null; + protected $isStacked = null; + + static $LEGEND_RIGHT = 'right'; + static $LEGEND_TOP = 'top'; + static $LEGEND_BOTTOM = 'bottom'; + static $LEGEND_NONE = 'none'; + + public function __construct() { + $this->chartPackage = 'corechart'; + } + + + public function setBaselineColor($color) { + $this->hAxis['baselineColor'] = $color; + $this->vAxis['baselineColor'] = $color; + } + + public function setHorizontalAxisDirection($direction) { + $this->hAxis['direction'] = $direction; + } + + public function setAxisTextStyle($color, $fontName = false, $fontSize = false) { + $this->setHorizontalAxisTextStyle($color, $fontName, $fontSize); + $this->setVerticalAxisTextStyle($color, $fontName, $fontSize); + } + + public function setHorizontalAxisTextStyle($color = false, $fontName = false, $fontSize = false) { + if ($color) + $this->hAxis['textStyle']['color'] = $color; + if ($fontName) + $this->hAxis['textStyle']['fontName'] = $fontName; + if ($fontSize) + $this->hAxis['textStyle']['fontSize'] = $fontSize; + } + + public function setVerticalAxisTextStyle($color = false, $fontName = false, $fontSize = false) { + if ($color) + $this->vAxis['textStyle']['color'] = $color; + if ($fontName) + $this->vAxis['textStyle']['fontName'] = $fontName; + if ($fontSize) + $this->vAxis['textStyle']['fontSize'] = $fontSize; + } + + public function setHorizontalAxisTitle($title) { + $this->hAxis['title'] = $title; + } + + public function setVerticalAxisTitle($title) { + $this->vAxis['title'] = $title; + } + + + public function setTitle($title) { + $this->title = $title; + } + + /** + * @deprecated + * This function is now deprecated use setLegendPosition and setLegendTextStyle + * instead + * + * @param string $legendType + * @param array $textStyle - color, fontName and fontSize can be set. + */ + public function setLegend($legendType, $textStyle = null) { + $this->setLegendPosition($legendType); + if ($textStyle != null) { + $this->setLegendTextStyle(((array_key_exists('color', $textStyle)) ? $textStyle['color'] : false), + ((array_key_exists('fontName', $textStyle)) ? $textStyle['fontName'] : false), + ((array_key_exists('fontSize', $textStyle)) ? $textStyle['fontSize'] : false)); + } + } + + public function setLegendPosition($legendType) { + $this->legend = $legendType; + } + + public function setLegendTextStyle($color = false, $fontName = false, $fontSize = false) { + if ($color) + $this->legendTextStyle['color'] = $color; + if ($fontName) + $this->legendTextStyle['fontName'] = $fontName; + if ($fontSize) + $this->legendTextStyle['fontSize'] = $fontSize; + } + + public function setTitleTextStyle($color = false, $fontName = false, $fontSize = false) { + if ($color) + $this->titleTextStyle['color'] = $color; + if ($fontName) + $this->titleTextStyle['fontName'] = $fontName; + if ($fontSize) + $this->titleTextStyle['fontSize'] = $fontSize; + } + + public function setVerticalAxisExtremeValues($min, $max) { + $this->vAxis['minValue'] = $min; + $this->vAxis['maxValue'] = $max; + } + + public function setVerticalAxisLogScale($bool) { + $this->vAxis['logScale'] = $bool; + } + + public function setReverseCategories($bool) { + $this->reverseCategories = $bool; + } + + public function setIsStacked($bool) { + $this->isStacked = $bool; + } + +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdBarGraph.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdBarGraph.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdBarGraph.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,17 @@ +<?php +/** + * sdBarGraph - Creates bar graphs! + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield - Second2 + * @version 0.2 + */ + +class BarGraph extends AxisBaseChart { + protected $chartType = 'BarChart'; + + +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdBaseChart.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdBaseChart.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdBaseChart.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,208 @@ +<?php +/** + * sdBaseChart - parent charting class, all charts extend this class + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield + * @version 0.2 + */ + +class BaseChart { + protected $width = 200; + protected $height = 150; + protected $backgroundColor = '#fff'; + protected $url = null; + protected $ajaxData = null; + protected $chartPackage = ''; + + protected $colors = null; + protected $div = ''; + protected $html = ''; + protected $callback = null; + protected $data = ""; + protected $fontSize = null; + protected $fontName = null; + private $loadedPackages = array(); + + + /** + * If you need another javascript function to be called once the graph has + * succesfully been created and rendered then pass the name of the function + * to this function. + * + * @param string $callback - name of the javascript call back function + * the chart object will be passed to it + * as its only parameter + */ + public function setCallback($callback) { + $this->callback = $callback; + } + + public function setDefaultFont($fontName, $fontSize) { + $this->fontName = $fontName; + $this->fontSize = $fontSize; + } + + public function setDivName($name) { + $this->div = $name; + } + + public function setWidthAndHeight($width, $height) { + $this->width = $width; + $this->height = $height; + } + + public function setBackgroundColor($color) { + $this->backgroundColor = $color; + } + + + public function setDataColors($colors) { + if (!is_array($colors)) { + $this->colors = array($colors); + } else { + $this->colors = $colors; + } + } + + + /** + * Creates the Javascript string required to load in a certain + * Google visualization package + * + * @param string $packageName + */ + protected function loadPackage($packageName) { + if (!in_array($packageName, $this->loadedPackages)) { + $this->html .= "google.load('visualization', '1', {'packages':['$packageName']});"; + array_push($this->loadedPackages, $packageName); + } + } + + + protected function formatData($data, $labels = null) { + $addedColumns = false; + $this->html .= 'var data = new google.visualization.DataTable();' . "\n"; + if ($labels != null) + $this->html .= "data.addColumn('string','235');" . "\n"; + + $rowCountAdded = false; + $row = 1; + $latestRow = 0; + foreach($data as $dataText=>$dataSet) { + if ($latestRow < $row) { + if (is_string($dataText)) { + $this->html .= "data.addColumn('number','".$dataText."');" . "\n"; + } else + $this->html .= "data.addColumn('number','total');" . "\n"; + $latestRow = $row; + } + + + if (!$rowCountAdded) { + $total = (is_array($dataSet)) ? count($dataSet) : count($data); + $this->html .= 'data.addRows('.$total.');' . "\n"; + // Draw labels + foreach($labels as $key=>$label) { + $this->html .= "data.setValue($key, 0, '{$label}');" . "\n"; + } + + $rowCountAdded = true; + } + + + if (!is_array($dataSet)) { + $this->html .= "data.setValue($dataText, 1, $dataSet);" . "\n"; + } else { + foreach($dataSet as $key=>$nextRow) { + $this->html .= "data.setValue($key, $row, $nextRow);" . "\n"; + } + $row++; + } + } + } + + + /** + * Generates the HTML/JS output required to create the graph while loading + * the data for the graph from a randomly named javascript function. + * + * @param array $data - array of data to be used for the chart + * @param array $labels - labels to use on the chart + * @param string $divName - OPTIONAL name of the div to draw the chart in + */ + public function inlineGraph($data, $labels, $divName = '') { + $randNum = rand(1, 1500); + $functionName = 'produceData' . $randNum; + unset($randNum); + $this->html .= '<script type="text/javascript">' . "\n"; + $this->html .= "function $functionName() {" . "\n"; + $this->html .= $this->formatData($data, $labels); + $this->html .= 'return data;'; + $this->html .= '}'; + $this->html .= '</script>'; + $this->data = $functionName; + + $this->ajaxGraph('', array(), $divName); + } + + + /** + * Generates the HTML/JS output required to create the graph when loading the + * data for the graph via AJAX. + * + * @param string The url to call to retrieve the chart data + * @param array an array of params to pass to the ajax url + * @param string OPTIONAL the name of the div to render the chart in + */ + public function ajaxGraph($ajaxUrl, $ajaxParams, $divName = '') { + if ($divName != '') + $this->setDivName ($divName); + + $this->url = $ajaxUrl; + $this->ajaxData = $ajaxParams; + + $this->html .= '<script type="text/javascript">' . "\n"; + $this->loadPackage($this->chartPackage); + $this->html .= 'CHARTS.toCreate.push({'; + + foreach ($this as $key => $propety) { + if (($key == 'html') || ($key == 'chartPackage') || ($key == 'loadedPackages')) + continue; + if ((($key == 'callback') || ($key == 'data')) && ($propety != null)) { + $this->html .= "$key: $propety,"; + } else if (is_string($propety)) { + $this->html .= "$key: '$propety',"; + } else if (is_int($propety)) { + $this->html .= "$key: $propety,"; + } else if (is_bool($propety)) { + $propety = ($propety) ? 'true' : 'false'; + $this->html .= "$key: $propety,"; + } else if (is_array($propety)) { + $this->html .= "$key: " . json_encode($propety) . ','; + } + } + $this->html = trim($this->html, ','); + $this->html .= '});'; + $this->html .= '</script>'; + } + + + /** + * Does what it says on the tin. echo's the HTML/JS required to draw the chart! + * + * @param Bool return HTML or echo output + * @return void or string HTML + */ + public function render($returnHTML = false) { + if ($returnHTML) + return $this->html; + else + echo $this->html; + } + + +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdColumnGraph.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdColumnGraph.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdColumnGraph.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,18 @@ +<?php +/** + * sdColumnGraph - at the moment simply extends the BarGraph class + * so all the params exposed by the bargraph class are available but no more! + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield + * @version 0.2 + */ + +class ColumnGraph extends BarGraph { + protected $chartType = 'ColumnChart'; + + +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdGaugeGraph.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdGaugeGraph.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdGaugeGraph.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,87 @@ +<?php +/** + * sdGaugeGraph - Creates gauge style charts! + * Google API URL: http://code.google.com/apis/visualization/documentation/gallery/gauge.html + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield + * @version 0.2 + */ + +class GaugeGraph extends BaseChart { + protected $greenFrom = null; + protected $greenTo = null; + protected $yellowFrom = null; + protected $yellowTo = null; + protected $redFrom = null; + protected $redTo = null; + protected $min = null; + protected $max = null; + protected $minorTicks = null; + protected $majorTicks = null; + protected $chartType = 'Gauge'; + + + public function __construct() { + $this->chartPackage = 'gauge'; + } + + + + + /** + * Generates the HTML/JS output required to create the graph when loading the + * data for the graph via AJAX. + * + * @param string The url to call to retrieve the chart data + * @param array an array of params to pass to the ajax url + * @param string OPTIONAL the name of the div to render the chart in + */ + public function ajaxGraph($ajaxUrl, $ajaxParams, $divName = '') { + + // If the limits have been set relative then we better go and replace + // the values! + if ($this->greenTo == 'MAX') + $this->greenTo = $this->max; + + if ($this->redTo == 'MAX') + $this->redTo = $this->max; + + if ($this->yellowTo == 'MAX') + $this->yellowTo = $this->max; + + parent::ajaxGraph($ajaxUrl, $ajaxParams, $divName); + } + + + public function setGreenLimits($from, $to = 'MAX') { + $this->greenFrom = $from; + $this->greenTo = $to; + } + + public function setRedLimits($from, $to = 'MAX') { + $this->redFrom = $from; + $this->redTo = $to; + } + + public function setYellowLimits($from, $to = 'MAX') { + $this->yellowFrom = $from; + $this->yellowTo = $to; + } + + public function setLimits($min, $max) { + $this->min = $min; + $this->max = $max; + } + + public function setMajorTicks($majorTicks) { + $this->majorTicks = $majorTicks; + } + + public function setMinorTicks($minorTicks) { + $this->minorTicks = $minorTicks; + } +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdInteractiveChartConfig.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdInteractiveChartConfig.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdInteractiveChartConfig.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,27 @@ +<?php +/** + * sdInteractiveChart class + * + * This class handles some config stuff for the interactive charts. It allows + * the required javascript to be automatically added once the head of the site + * has finished. + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield - Second2 + * @version 0.2 + */ +class sdInteractiveChartConfig +{ + /** + * After the context has been initiated, we can add the required assets + */ + public static function listenToContextLoadFactoriesEvent() + { + require_once( dirname(__FILE__).'/helper/sdInteractiveChartHelper.php'); + addInteractiveChartJavascript(); + + + } + +} \ No newline at end of file Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdLineGraph.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdLineGraph.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdLineGraph.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,53 @@ +<?php +/** + * sdLineChart - Class for creating basic line graphs. Use the timeline graph + * for ploting values over time! + * Google API: http://code.google.com/apis/visualization/documentation/gallery/linechart.html + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield + * @version 0.2 + */ + +class LineGraph extends AxisBaseChart { + protected $chartType = 'LineChart'; + protected $smoothLines = null; + protected $pointSize = null; + + static $SMOOTH_LINES = 'function'; + static $NO_SMOOTH_LINES = 'none'; + + + /** + * Allows the horiziontal axis labels to be drawn at an angle, + * the default angle given by Google is 30(degrees) but during testing + * it appeared 45 degrees would be more useful, so this is the default + * which is used if slanted text is switched on. + * + * @param bool $bool + * @param int $angle + */ + public function setSlantedText($bool, $angle = 45) { + $this->hAxis['slantedText'] = $bool; + $this->hAxis['slantedTextAngle'] = $angle; + } + + + public function setSmoothLines($value) { + if ($value == LineGraph::$NO_SMOOTH_LINES) + $value = null; + $this->smoothLines = $value; + } + + public function setPointSize($size) { + $this->pointSize = $size; + } + + public function setHorizontalMaxAlternation($maxAlternation) { + $this->hAxis['maxAlternation'] = $maxAlternation; + } + +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/lib/sdPieGraph.class.php =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/lib/sdPieGraph.class.php (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/lib/sdPieGraph.class.php 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,72 @@ +<?php +/** + * sdGaugeGraph - Creates Pie chart (These can be 3D)! + * Google API URL: http://code.google.com/apis/visualization/documentation/gallery/piechart.html + * + * @package plugins + * @subpackage sdInteractiveChart + * @author Seb Dangerfield + * @version 0.2 + */ + + +class PieGraph extends BaseChart { + protected $is3D = null; + protected $chartType = 'PieChart'; + protected $pieSliceText = null; + protected $pieSliceTextStyle = null; + protected $sliceVisibilityThreshold = null; + protected $reverseCategories = null; + protected $legend = null; + protected $legendTextStyle = null; + + static $PIE_TEXT_PERCENTAGE = 'percentage'; + static $PIE_TEXT_VALUE = 'value'; + static $PIE_TEXT_LABEL = 'label'; + static $PIE_TEXT_NONE = 'none'; + + public function __construct() { + $this->chartPackage = 'corechart'; + } + + + public function setPieSliceText($text) { + $this->pieSliceText = $text; + } + + public function setPieSliceTextStyle($color = false, $fontName = false, $fontSize = false) { + if ($color) + $this->pieSliceTextStyle['color'] = $color; + if ($fontName) + $this->pieSliceTextStyle['fontName'] = $fontName; + if ($fontSize) + $this->pieSliceTextStyle['fontSize'] = $fontSize; + } + + public function set3D($bool) { + $this->is3D = $bool; + } + + public function setSliceVisibilityThreshold($sliceVisibilityThreshold) { + $this->sliceVisibilityThreshold = $sliceVisibilityThreshold; + } + + public function setLegendPosition($legendType) { + $this->legend = $legendType; + } + + public function setLegendTextStyle($color = false, $fontName = false, $fontSize = false) { + if ($color) + $this->legendTextStyle['color'] = $color; + if ($fontName) + $this->legendTextStyle['fontName'] = $fontName; + if ($fontSize) + $this->legendTextStyle['fontSize'] = $fontSize; + } + + public function setReverseCategories($bool) { + $this->reverseCategories = $bool; + } +} + +?> Added: plugins/sdInteractiveChartPlugin/trunk/package.xml =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/package.xml (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/package.xml 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,95 @@ +<?xml version="1.0" encoding="UTF-8"?> +<package packagerversion="1.4.6" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> + <name>sdInteractiveChartPlugin</name> + <channel>plugins.symfony-project.org</channel> + <summary>Create Interactive Javascript Charts created using PHP (Powered By Google's Visualization API)</summary> + <description>An easy to use plugin to allow you to use Google's Visualization API to create Javascript Charts without writing any Javascript.</description> + <lead> + <name>Seb Dangerfield</name> + <user>sebsecond2</user> + <email>[email protected]</email> + <active>yes</active> + </lead> + <date>2010-09-10</date> + <time>15:30:00</time> + <version> + <release>0.2.0</release> + <api>0.2.0</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <license uri="http://www.symfony-project.org/license">MIT license</license> + <notes>-</notes> + <contents> + <dir name="/"> + <file role="data" name="README" /> + <file role="data" name="LICENSE" /> + <dir name="config"> + <!-- config --> + <file role="data" name="app.yml" /> + <file role="data" name="config.php" /> + </dir> + <dir name="lib"> + <dir name="helper"> + <file role="data" name="sdInteractiveChartHelper.php" /> + </dir> + <file role="data" name="sdAreaGraph.class.php" /> + <file role="data" name="sdAxisBaseChart.class.php" /> + <file role="data" name="sdBarGraph.class.php" /> + <file role="data" name="sdBaseChart.class.php" /> + <file role="data" name="sdColumnGraph.class.php" /> + <file role="data" name="sdGaugeGraph.class.php" /> + <file role="data" name="sdInteractiveChartConfig.php" /> + <file role="data" name="sdLineGraph.class.php" /> + <file role="data" name="sdPieGraph.class.php" /> + + </dir> + <dir name="web"> + <dir name="js"> + <file role="data" name=".htaccess" /> + <file role="data" name="interactiveCharts0.2.0.js" /> + <file role="data" name="interactiveCharts0.2.0-min.js" /> + </dir> + </dir> + </dir> + </contents> + <dependencies> + <required> + <php> + <min>5.2.4</min> + </php> + <pearinstaller> + <min>1.4.1</min> + </pearinstaller> + <package> + <name>symfony</name> + <channel>pear.symfony-project.com</channel> + <min>1.1.0</min> + <max>1.4.7</max> + </package> + + </required> + </dependencies> + <phprelease /> + <changelog> + <release> + <version> + <release>0.2.0</release> + <api>0.2.0</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <date>2010-09-10</date> + <notes> +* Bug Fix: setting colors using CSS color names now works properly. +* Added more of the charts config options to each type of chart. +* Refactored code. +* Returns the chart index as well as the chart creation object to any callback fucntion + </notes> + </release> + </changelog> +</package>+ \ No newline at end of file Added: plugins/sdInteractiveChartPlugin/trunk/sdInteractiveChartPlugin-0.2.0.tgz =================================================================== (Binary files differ) Property changes on: plugins/sdInteractiveChartPlugin/trunk/sdInteractiveChartPlugin-0.2.0.tgz ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: plugins/sdInteractiveChartPlugin/trunk/web/js/.htaccess =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/web/js/.htaccess (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/web/js/.htaccess 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,9 @@ +# If your server has the expires module enabled then the following code will +# make sure the expires header for the javscript code is set to one month, this +# will mean the web browser should not even bother looking for a new version on the +# server if it already has copy, saving one http request and the bandwidth required. + +<ifModule mod_expires.c> + ExpiresActive On + ExpiresByType application/x-javascript "access plus 1 months" +</ifModule> Added: plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0-min.js =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0-min.js (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0-min.js 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1 @@ +var CHARTS={};CHARTS.toCreate=[];$(document).ready(function(){for(var b=CHARTS.toCreate.length,c=0;c<b;c++){var a=CHARTS.toCreate[c];if(a.chartType==undefined)typeof console!="undefined"&&console.error("No chart type (chartType) defined.");else a.url!=undefined&&a.url!=""?CHARTS.loadData(a,c):CHARTS.createChart(c)}});CHARTS.createChart=function(b){CHARTS.createBarChart(b)}; CHARTS.createBarChart=function(b){chartInfo=CHARTS.toCreate[b];if(typeof chartInfo.data=="function")chartInfo.data=chartInfo.data();eval("new google.visualization."+chartInfo.chartType+'(document.getElementById("'+chartInfo.div+'"))').draw(chartInfo.data,chartInfo);chartInfo.callback!=undefined&&typeof chartInfo.callback=="function"&&chartInfo.callback(chartInfo,b)}; CHARTS.loadData=function(b,c){if(b.ajaxData==undefined)b.ajaxData={};b.index=c;jQuery.get(b.url,b.ajaxData,function(a){var e=new google.visualization.DataTable;a.labels!=undefined&&e.addColumn("string","Labels");var d=false;for(i=0;i<a.data.length;i++){e.addColumn("number",a.dataNames[i]);if(!d){e.addRows(a.data[i].length);for(d=0;d<a.labels.length;d++)e.setValue(d,0,a.labels[d]);d=true}for(var f=0;f<a.data[i].length;f++)e.setValue(f,i+1,a.data[i][f])}delete a.data;delete a.labels;delete a.dataNames; CHARTS.toCreate[c].data=e;for(attrname in a)CHARTS.toCreate[c][attrname]=a[attrname];CHARTS.createChart(c)},"json")}; \ No newline at end of file Added: plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0.js =================================================================== --- plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0.js (rev 0) +++ plugins/sdInteractiveChartPlugin/trunk/web/js/interactiveCharts0.2.0.js 2010-09-10 15:16:54 UTC (rev 30878) @@ -0,0 +1,98 @@ +var CHARTS = {}; + +CHARTS.toCreate = Array(); + + +/** + * When the document has loaded we should go through the list of charts + * and create them! + * + */ +$(document).ready(function() { + var len = CHARTS.toCreate.length; + for (var i = 0; i < len; i++) { + var chart = CHARTS.toCreate[i]; + if (chart.chartType == undefined) { + if (typeof(console) != "undefined") { + console.error('No chart type (chartType) defined.'); + } + continue; + } + + if ((chart.url != undefined) && (chart.url != '')) { + // This chart requires data from an AJAX request! + CHARTS.loadData(chart, i); + } else { + // create the chart! + CHARTS.createChart(i); + } + } +}); + + +CHARTS.createChart = function(chartIndex) { + // I think all charts this plugin will implement can use the same creation code + // But just in case this can stay like it is for now! + CHARTS.createBarChart(chartIndex); +} + + +CHARTS.createBarChart = function(chartIndex) { + chartInfo = CHARTS.toCreate[chartIndex]; + // load on the data + if (typeof(chartInfo.data) == 'function') { + chartInfo.data = chartInfo.data(); + } + + var chart = eval('new google.visualization.' + chartInfo.chartType + '(document.getElementById("' + chartInfo.div + '"))'); + chart.draw(chartInfo.data, chartInfo); + // if there's a call back we better call it! + if ((chartInfo.callback != undefined) && (typeof(chartInfo.callback) == 'function')) { + chartInfo.callback(chartInfo, chartIndex); + } +} + + +CHARTS.loadData = function(graphObj, index) { + if (graphObj.ajaxData == undefined) { + graphObj.ajaxData = {}; + } + graphObj.index = index; + var ind = index; + jQuery.get(graphObj.url, graphObj.ajaxData, function(json) { + + var data = new google.visualization.DataTable(); + if (json.labels != undefined) { + data.addColumn('string', 'Labels'); + } + var rowCountAdded = false; + + + // now shoot round and add all the data! + for (i = 0; i < json.data.length; i++) { + data.addColumn('number', json.dataNames[i]); + if (!rowCountAdded) { + data.addRows(json.data[i].length); + // add labels! + for (var t = 0; t < json.labels.length; t++) { + data.setValue(t, 0, json.labels[t]); + } + rowCountAdded = true; + } + for (var j = 0; j < json.data[i].length; j++) { + data.setValue(j, i+1, json.data[i][j]); + } + + } + delete json.data; + delete json.labels; + delete json.dataNames; + CHARTS.toCreate[ind].data = data; + + for (attrname in json) { CHARTS.toCreate[ind][attrname] = json[attrname]; } + + // now lets call the chart creation function + CHARTS.createChart(ind); + }, + 'json'); +} -- You received this message because you are subscribed to the Google Groups "symfony SVN" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/symfony-svn?hl=en.
