3 * Pure JavaScript plotting plugin using jQuery
8 * Copyright (c) 2009-2012 Chris Leonello
9 * jqPlot is currently available for use in all personal or commercial projects
10 * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
11 * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
12 * choose the license that best suits your project and use it accordingly.
14 * Although not required, the author would appreciate an email letting him
15 * know of any substantial use of jqPlot. You can reach the author at:
16 * chris at jqplot dot com or see http://www.jqplot.com/info.php .
18 * If you are feeling kind and generous, consider supporting the project by
19 * making a donation at: http://www.jqplot.com/donate.php .
21 * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
25 * http://hexmen.com/blog/2007/03/printf-sprintf/
26 * http://hexmen.com/js/sprintf.js
27 * The author (Ash Searle) has placed this code in the public domain:
28 * "This code is unrestricted: you are free to use it however you like."
32 // class $.jqplot.EnhancedLegendRenderer
33 // Legend renderer which can specify the number of rows and/or columns in the legend.
34 $.jqplot.EnhancedLegendRenderer = function(){
35 $.jqplot.TableLegendRenderer.call(this);
38 $.jqplot.EnhancedLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
39 $.jqplot.EnhancedLegendRenderer.prototype.constructor = $.jqplot.EnhancedLegendRenderer;
41 // called with scope of legend.
42 $.jqplot.EnhancedLegendRenderer.prototype.init = function(options) {
44 // Maximum number of rows in the legend. 0 or null for unlimited.
45 this.numberRows = null;
46 // prop: numberColumns
47 // Maximum number of columns in the legend. 0 or null for unlimited.
48 this.numberColumns = null;
50 // false to not enable series on/off toggling on the legend.
51 // true or a fadein/fadeout speed (number of milliseconds or 'fast', 'normal', 'slow')
52 // to enable show/hide of series on click of legend item.
53 this.seriesToggle = 'normal';
54 // prop: seriesToggleReplot
55 // True to replot the chart after toggling series on/off.
56 // This will set the series show property to false.
57 // This allows for rescaling or other maniplation of chart.
58 // Set to an options object (e.g. {resetAxes: true}) for replot options.
59 this.seriesToggleReplot = false;
60 // prop: disableIEFading
61 // true to toggle series with a show/hide method only and not allow fading in/out.
62 // This is to overcome poor performance of fade in some versions of IE.
63 this.disableIEFading = true;
64 $.extend(true, this, options);
66 if (this.seriesToggle) {
67 $.jqplot.postDrawHooks.push(postDraw);
71 // called with scope of legend
72 $.jqplot.EnhancedLegendRenderer.prototype.draw = function(offsets, plot) {
75 var series = this._series;
77 var ss = 'position:absolute;';
78 ss += (this.background) ? 'background:'+this.background+';' : '';
79 ss += (this.border) ? 'border:'+this.border+';' : '';
80 ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
81 ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
82 ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
83 ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
84 ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
85 ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
86 ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
87 this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
88 if (this.seriesToggle) {
89 this._elem.css('z-index', '3');
95 if (this.numberRows) {
97 if (!this.numberColumns){
98 nc = Math.ceil(series.length/nr);
101 nc = this.numberColumns;
104 else if (this.numberColumns) {
105 nc = this.numberColumns;
106 nr = Math.ceil(series.length/this.numberColumns);
113 var i, j, tr, td1, td2, lt, rs, div, div0, div1;
115 // check to see if we need to reverse
116 for (i=series.length-1; i>=0; i--) {
117 if (nc == 1 && series[i]._stack || series[i].renderer.constructor == $.jqplot.BezierCurveRenderer){
122 for (i=0; i<nr; i++) {
123 tr = $(document.createElement('tr'));
124 tr.addClass('jqplot-table-legend');
126 tr.prependTo(this._elem);
129 tr.appendTo(this._elem);
131 for (j=0; j<nc; j++) {
132 if (idx < series.length && (series[idx].show || series[idx].showLabel)){
134 lt = this.labels[idx] || s.label.toString();
153 rs = (pad) ? this.rowSpacing : '0';
155 td1 = $(document.createElement('td'));
156 td1.addClass('jqplot-table-legend jqplot-table-legend-swatch');
157 td1.css({textAlign: 'center', paddingTop: rs});
159 div0 = $(document.createElement('div'));
160 div0.addClass('jqplot-table-legend-swatch-outline');
161 div1 = $(document.createElement('div'));
162 div1.addClass('jqplot-table-legend-swatch');
163 div1.css({backgroundColor: color, borderColor: color});
165 td1.append(div0.append(div1));
167 td2 = $(document.createElement('td'));
168 td2.addClass('jqplot-table-legend jqplot-table-legend-label');
169 td2.css('paddingTop', rs);
171 // td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
172 // '<div><div class="jqplot-table-legend-swatch" style="background-color:'+color+';border-color:'+color+';"></div>'+
174 // td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
175 if (this.escapeHtml){
182 if (this.showLabels) {td2.prependTo(tr);}
183 if (this.showSwatches) {td1.prependTo(tr);}
186 if (this.showSwatches) {td1.appendTo(tr);}
187 if (this.showLabels) {td2.appendTo(tr);}
190 if (this.seriesToggle) {
192 // add an overlay for clicking series on/off
193 // div0 = $(document.createElement('div'));
194 // div0.addClass('jqplot-table-legend-overlay');
195 // div0.css({position:'relative', left:0, top:0, height:'100%', width:'100%'});
199 if (typeof(this.seriesToggle) === 'string' || typeof(this.seriesToggle) === 'number') {
200 if (!$.jqplot.use_excanvas || !this.disableIEFading) {
201 speed = this.seriesToggle;
204 if (this.showSwatches) {
205 td1.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot}, handleToggle);
206 td1.addClass('jqplot-seriesToggle');
208 if (this.showLabels) {
209 td2.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot}, handleToggle);
210 td2.addClass('jqplot-seriesToggle');
213 // for series that are already hidden, add the hidden class
214 if (!s.show && s.showLabel) {
215 td1.addClass('jqplot-series-hidden');
216 td2.addClass('jqplot-series-hidden');
226 td1 = td2 = div0 = div1 = null;
232 var handleToggle = function (ev) {
241 if (s.canvas._elem.is(':hidden') || !s.show) {
245 var doLegendToggle = function() {
250 if ($.isPlainObject(replot)) {
251 $.extend(true, opts, replot);
255 // if showing, there was no canvas element to fade in, so hide here
256 // and then do a fade in.
257 if (showing && speed) {
258 var s = plot.series[sidx];
260 if (s.shadowCanvas._elem) {
261 s.shadowCanvas._elem.hide().fadeIn(speed);
263 s.canvas._elem.hide().fadeIn(speed);
264 s.canvas._elem.nextAll('.jqplot-point-label.jqplot-series-'+s.index).hide().fadeIn(speed);
270 var s = plot.series[sidx];
272 if (s.canvas._elem.is(':hidden') || !s.show) {
273 // Not sure if there is a better way to check for showSwatches and showLabels === true.
274 // Test for "undefined" since default values for both showSwatches and showLables is true.
275 if (typeof plot.options.legend.showSwatches === 'undefined' || plot.options.legend.showSwatches === true) {
276 plot.legend._elem.find('td').eq(sidx * 2).addClass('jqplot-series-hidden');
278 if (typeof plot.options.legend.showLabels === 'undefined' || plot.options.legend.showLabels === true) {
279 plot.legend._elem.find('td').eq((sidx * 2) + 1).addClass('jqplot-series-hidden');
283 if (typeof plot.options.legend.showSwatches === 'undefined' || plot.options.legend.showSwatches === true) {
284 plot.legend._elem.find('td').eq(sidx * 2).removeClass('jqplot-series-hidden');
286 if (typeof plot.options.legend.showLabels === 'undefined' || plot.options.legend.showLabels === true) {
287 plot.legend._elem.find('td').eq((sidx * 2) + 1).removeClass('jqplot-series-hidden');
295 s.toggleDisplay(ev, doLegendToggle);
298 // called with scope of plot.
299 var postDraw = function () {
300 if (this.legend.renderer.constructor == $.jqplot.EnhancedLegendRenderer && this.legend.seriesToggle){
301 var e = this.legend._elem.detach();
302 this.eventCanvas._elem.after(e);