? 797364_drupal_api_v2.patch ? LICENSE.txt ? PATCHES.txt Index: graphael/graphael.jquery.js =================================================================== RCS file: graphael/graphael.jquery.js diff -N graphael/graphael.jquery.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ graphael/graphael.jquery.js 14 May 2010 17:44:17 -0000 @@ -0,0 +1,244 @@ +// $Id$ + +/** + * jQuery bindings for gRaphael + * ---------------------------- + * + * Basic usage: + * + * $('div.my-graph').graphael('bar', [0, 5, 10]); + * + * Parameters: + * + * @method + * String key, one of the following: 'bar', 'dot', 'pie', 'line' or 'get'. + * The 'get' method may only be used against a previously instantiated + * graph and will retrieve the gRaphael graph object. All other methods will + * create a corresponding gRaphael graph of the specified type. + * + * @values + * A non-associative array of values that correspond to the parameters of + * the specified graph type. For example, the 'bar' method expects only one + * array of values, while the 'line' method requires two arrays (x and y, + * respectively). + * + * @params + * An associative array of parameters. Supported parameters: + * + * 'opts': Options that are passed directly to the gRaphael method. + * 'padding': Pixels of padding to add to the inside of the gRaphael element. + * Defaults to 10. Set to 0 to draw the chart from edge to edge. + * + * Events + * ------ + * + * @init.graphael + * Triggered when a gRaphael graph is first created. By binding to this + * event, the graph object can be retrieved and different hover, etc. effects + * can be added to a graph. + * + * $('div.my-graph').bind('init.graphael', function() { + * $(this).graphael('get').hover(function() {}, function() {}); + * }); + * + * Static methods + * -------------- + * + * $().graphael.labelShow(graph, element, params); + * + * Chart element hover in callback for displaying a label. + * + * Parameters: + * + * @graph + * The jQuery object for the gRaphael DOM element. + * + * @element + * The element for which labeling should occur. `this` in the context of a + * chart.hover() event. + * + * @params + * An associative array of parameters. Supported parameters: + * + * 'label': The label type to be used. May be one of the following: + * 'tag', 'popup', 'flag', 'label', 'drop', 'blob'. + * 'attrLabel': An associative array of attributes supported by gRaphael. + * 'attrText': An associative array of attributes supported by gRaphael. + * + * + * $().graphael.labelHide(graph, element, params); + * + * Chart element hover out callback for hiding a label. + * + * Parameters: + * + * @graph + * The jQuery object for the gRaphael DOM element. + * + * @element + * The element for which labeling should occur. `this` in the context of a + * chart.hover() event. + * + * @params + * An associative array of parameters. Currently no supported parameters. + * + * + * $().graphael.elementPosition(graph, element); + * + * Helper method to retrieve the position of an element in a graph value + * series, starting from 0. Useful for retrieving external data by index of + * the element in question. + * + * Parameters: + * + * @graph + * The jQuery object for the gRaphael DOM element. + * + * @element + * The element for which the index should be retrieved. `this` in the context + * of a chart.hover() event. + * + * + * $().graphael.elementValue(graph, element); + * + * Helper method to retrieve the value of an element in a graph value series. + * + * Parameters: + * + * @graph + * The jQuery object for the gRaphael DOM element. + * + * @element + * The element for which the value should be retrieved. `this` in the context + * of a chart.hover() event. + */ +(function($) { + $.fn.graphael = function(method, values, params) { + var r = Raphael(this.get(0)); + var chart = false; + params = params || {}; + params.opts = params.opts || {}; + params.padding = params.padding || 10; + + var w = this.width() - (params.padding * 2); + var h = this.height() - (params.padding * 2); + var x = params.padding; + var y = params.padding; + + // Set supported parameters. + + // 'color': array of CSS hex color values, example: '#fff'. + if (params.colors) { + r.g.colors = params.colors; + } + // 'font': string of CSS font shorthand, example: '12px Arial'. + if (params.font) { + r.g.txtattr.font = params.font; + } + + switch (method) { + case 'bar': + chart = r.g.barchart(x, y, w, h, values, params.opts); + break; + case 'dot': + chart = r.g.dotchart(x, y, w, h, values[0], values[1], values[2], params.opts); + break; + case 'pie': + var pie_x = (w * 0.5) + x; + var pie_y = (h * 0.5) + y; + var radius = w > h ? h * 0.5 : w * 0.5; + + // If a legend has been provided, attempt to position the pie chart + // intelligently in relation to the legend position. + if (params.opts.legend) { + switch (params.opts.legendpos) { + case 'north': + pie_y = (h - radius) + y; + break; + case 'south': + pie_y = (radius) + y; + break; + case 'west': + pie_x = (w - radius) + x; + break; + // East is the default position. + default: + pie_x = (radius) + x; + break; + } + } + + chart = r.g.piechart(pie_x, pie_y, radius, values, params.opts); + break; + case 'line': + chart = r.g.linechart(x, y, w, h, values[0], values[1], params.opts); + break; + } + if (chart) { + this.data('method', method); + this.data('raphael', r); + this.data('chart', chart); + this.trigger('init.graphael'); + } + return this; + }; + + $.fn.graphael.labelShow = function(graph, element, params) { + var r = graph.data('raphael'); + var method = graph.data('method'); + var label = params.label || 'popup'; + var attrLabel = params.attrLabel || { fill:'#fff' }; + var attrText = params.attrText || { fill:'#666', font: '12px Arial' }; + var value = params.value || $.fn.graphael.elementValue(graph, element); + + switch (method) { + case 'pie': + element.label = r.g[label](element.mx, element.my, value).attr([attrLabel, attrText]); + break; + case 'bar': + element.label = r.g[label](element.bar.x, element.bar.y, value).attr([attrLabel, attrText]); + break; + case 'line': + element.label = r.g[label](element.x, element.y, value).attr([attrLabel, attrText]); + break; + case 'dot': + element.label = r.g[label](element.x, element.y, value).attr([attrLabel, attrText]); + break; + } + }; + + $.fn.graphael.labelHide = function(graph, element, params) { + var method = graph.data('method'); + element.label.animate({opacity: 0}, 200, function () {this.remove();}); + }; + + $.fn.graphael.elementPosition = function(graph, element) { + var method = graph.data('method'); + switch (method) { + case 'pie': + return element.value.order; + case 'bar': + return element.bar.id; + case 'line': + return element.id; + case 'dot': + return element.dot.id; + } + return false; + }; + + $.fn.graphael.elementValue = function(graph, element) { + var method = graph.data('method'); + switch (method) { + case 'pie': + return element.value; + case 'bar': + return element.bar.value; + case 'line': + return element.value; + case 'dot': + return element.value; + } + return false; + }; +})(jQuery); Index: graphael/graphael.js =================================================================== RCS file: graphael/graphael.js diff -N graphael/graphael.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ graphael/graphael.js 14 May 2010 17:44:17 -0000 @@ -0,0 +1,39 @@ +// $Id$ + +/** + * Behavior for initializing gRaphael graphs generated through the + * theme('graphael') theme function in Drupal. + */ +Drupal.behaviors.graphael = function (context) { + $('.graphael-graph:not(.graphaelProcessed)').each(function() { + var id = $(this).attr('id'); + if (Drupal.settings.graphael[id]) { + var d = Drupal.settings.graphael[id]; + + $(this).bind('init.graphael', function() { + // Extension: basic labeling. + if (d.extend.label) { + var graph = $(this); + var chart = $(this).data('chart'); + var params = d.extend.label.params || {}; + chart.hover( + function() { + var position = $().graphael.elementPosition(graph, this); + if (position !== false && d.extend.label.values && d.extend.label.values[position]) { + params.value = d.extend.label.values[position]; + } + $().graphael.labelShow(graph, this, params); + }, + function() { $().graphael.labelHide(graph, this, params); } + ); + } + }); + + // Attach the original graph settings to the DOM element if they need be + // used by any extending functionality. + $(this) + .data('settings', d) + .graphael(d.method, d.values, d.params); + } + }).addClass('graphaelProcessed'); +}; Index: graphael/graphael.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/raphael/graphael/graphael.module,v retrieving revision 1.1 diff -u -p -r1.1 graphael.module --- graphael/graphael.module 23 Apr 2010 09:37:43 -0000 1.1 +++ graphael/graphael.module 14 May 2010 17:44:17 -0000 @@ -59,3 +59,50 @@ function graphael_add($files = array(), } } +/** + * Implementation of hook_theme(). + */ +function graphael_theme() { + return array( + 'graphael' => array( + 'path' => drupal_get_path('module', 'graphael'), + 'template' => 'graphael', + 'arguments' => array( + 'method' => '', + 'values' => array(), + 'params' => array(), + 'extend' => array(), + 'attr' => array(), + ), + ), + ); +} + +/** + * Preprocessor for theme('graphael'). + */ +function template_preprocess_graphael(&$vars) { + // Add required JS. + $path = drupal_get_path('module', 'graphael'); + drupal_add_js($path .'/graphael.jquery.js'); + drupal_add_js($path .'/graphael.js'); + graphael_add($vars['method']); + + // Auto id assignment. + static $autoid = 0; + if (!isset($vars['attr']['id'])) { + $autoid++; + $vars['attr']['id'] = 'graphael-graph-'. $autoid; + } + + // Class that the graphael Drupal behavior will use. + if (isset($vars['attr']['class'])) { + $vars['attr']['class'] .= ' graphael-graph'; + } + else { + $vars['attr']['class'] = 'graphael-graph'; + } + + // Default dimensions. + $vars['attr']['style'] = isset($vars['attr']['style']) ? $vars['attr']['style'] : 'height:200px; width:400px;'; +} Index: graphael/graphael.tpl.php =================================================================== RCS file: graphael/graphael.tpl.php diff -N graphael/graphael.tpl.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ graphael/graphael.tpl.php 14 May 2010 17:44:17 -0000 @@ -0,0 +1,24 @@ +<?php +// $Id$ + +/** + * @file graphael.tpl.php + * + * Theme template for gRaphael graphs. + * + * Available variables: + * - $method: The gRaphael graph type to use. Example: 'bar'. + * - $values: The values to be passed to $.graphael(). + * - $params: The params array to be passed to $.graphael(). + * - $extend: Added values that may be used by extending functionality. + * + * Note that it is the template's responsibility to add the graph to the Drupal + * settings js. This is done in the template file rather than in a preprocess + * function as it ensures that all other preprocessors have a chance to run + * prior to the graph values being collected and added. + */ + +$graph = array('method' => $method, 'values' => $values, 'params' => $params, 'extend' => $extend); +drupal_add_js(array('graphael' => array($attr['id'] => $graph)), 'setting'); +?> +<div <?php print drupal_attributes($attr) ?>></div>