OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / jquery / jqplot1.0 / plugins / jqplot.enhancedLegendRenderer.js
1 /**
2  * jqPlot
3  * Pure JavaScript plotting plugin using jQuery
4  *
5  * Version: 1.0.4
6  * Revision: 1120
7  *
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. 
13  *
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 .
17  *
18  * If you are feeling kind and generous, consider supporting the project by
19  * making a donation at: http://www.jqplot.com/donate.php .
20  *
21  * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
22  *
23  *     version 2007.04.27
24  *     author 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."
29  * 
30  */
31 (function($) {
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);
36     };
37     
38     $.jqplot.EnhancedLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
39     $.jqplot.EnhancedLegendRenderer.prototype.constructor = $.jqplot.EnhancedLegendRenderer;
40     
41     // called with scope of legend.
42     $.jqplot.EnhancedLegendRenderer.prototype.init = function(options) {
43         // prop: numberRows
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;
49         // prop: seriesToggle
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);
65         
66         if (this.seriesToggle) {
67             $.jqplot.postDrawHooks.push(postDraw);
68         }
69     };
70     
71     // called with scope of legend
72     $.jqplot.EnhancedLegendRenderer.prototype.draw = function(offsets, plot) {
73         var legend = this;
74         if (this.show) {
75             var series = this._series;
76                         var s;
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');
90             }
91         
92             var pad = false, 
93                 reverse = false,
94                 nr, nc;
95             if (this.numberRows) {
96                 nr = this.numberRows;
97                 if (!this.numberColumns){
98                     nc = Math.ceil(series.length/nr);
99                 }
100                 else{
101                     nc = this.numberColumns;
102                 }
103             }
104             else if (this.numberColumns) {
105                 nc = this.numberColumns;
106                 nr = Math.ceil(series.length/this.numberColumns);
107             }
108             else {
109                 nr = series.length;
110                 nc = 1;
111             }
112                 
113             var i, j, tr, td1, td2, lt, rs, div, div0, div1;
114             var idx = 0;
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){
118                     reverse = true;
119                 }
120             }    
121                 
122             for (i=0; i<nr; i++) {
123                 tr = $(document.createElement('tr'));
124                 tr.addClass('jqplot-table-legend');
125                 if (reverse){
126                     tr.prependTo(this._elem);
127                 }
128                 else{
129                     tr.appendTo(this._elem);
130                 }
131                 for (j=0; j<nc; j++) {
132                     if (idx < series.length && (series[idx].show || series[idx].showLabel)){
133                         s = series[idx];
134                         lt = this.labels[idx] || s.label.toString();
135                         if (lt) {
136                             var color = s.color;
137                             if (!reverse){
138                                 if (i>0){
139                                     pad = true;
140                                 }
141                                 else{
142                                     pad = false;
143                                 }
144                             }
145                             else{
146                                 if (i == nr -1){
147                                     pad = false;
148                                 }
149                                 else{
150                                     pad = true;
151                                 }
152                             }
153                             rs = (pad) ? this.rowSpacing : '0';
154
155                             td1 = $(document.createElement('td'));
156                             td1.addClass('jqplot-table-legend jqplot-table-legend-swatch');
157                             td1.css({textAlign: 'center', paddingTop: rs});
158
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});
164
165                             td1.append(div0.append(div1));
166
167                             td2 = $(document.createElement('td'));
168                             td2.addClass('jqplot-table-legend jqplot-table-legend-label');
169                             td2.css('paddingTop', rs);
170                     
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>'+
173                             //     '</div></td>');
174                             // td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
175                             if (this.escapeHtml){
176                                 td2.text(lt);
177                             }
178                             else {
179                                 td2.html(lt);
180                             }
181                             if (reverse) {
182                                 if (this.showLabels) {td2.prependTo(tr);}
183                                 if (this.showSwatches) {td1.prependTo(tr);}
184                             }
185                             else {
186                                 if (this.showSwatches) {td1.appendTo(tr);}
187                                 if (this.showLabels) {td2.appendTo(tr);}
188                             }
189                             
190                             if (this.seriesToggle) {
191
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%'});
196                                 // tr.append(div0);
197
198                                 var speed;
199                                 if (typeof(this.seriesToggle) === 'string' || typeof(this.seriesToggle) === 'number') {
200                                     if (!$.jqplot.use_excanvas || !this.disableIEFading) {
201                                         speed = this.seriesToggle;
202                                     }
203                                 } 
204                                 if (this.showSwatches) {
205                                     td1.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot}, handleToggle);
206                                     td1.addClass('jqplot-seriesToggle');
207                                 }
208                                 if (this.showLabels)  {
209                                     td2.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot}, handleToggle);
210                                     td2.addClass('jqplot-seriesToggle');
211                                 }
212
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');
217                                 }
218                             }
219                             
220                             pad = true;
221                         }
222                     }
223                     idx++;
224                 }
225                 
226                 td1 = td2 = div0 = div1 = null;   
227             }
228         }
229         return this._elem;
230     };
231
232     var handleToggle = function (ev) {
233         var d = ev.data,
234             s = d.series,
235             replot = d.replot,
236             plot = d.plot,
237             speed = d.speed,
238             sidx = s.index,
239             showing = false;
240
241         if (s.canvas._elem.is(':hidden') || !s.show) {
242             showing = true;
243         }
244
245         var doLegendToggle = function() {
246
247             if (replot) {
248                 var opts = {};
249
250                 if ($.isPlainObject(replot)) {
251                     $.extend(true, opts, replot);
252                 }
253
254                 plot.replot(opts);
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];
259
260                     if (s.shadowCanvas._elem) {
261                         s.shadowCanvas._elem.hide().fadeIn(speed);
262                     }
263                     s.canvas._elem.hide().fadeIn(speed);
264                     s.canvas._elem.nextAll('.jqplot-point-label.jqplot-series-'+s.index).hide().fadeIn(speed);
265                 }
266
267             }
268
269             else {
270                 var s = plot.series[sidx];
271
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');
277                     }
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');
280                     }
281                 }
282                 else {
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');
285                     }
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');
288                     }
289                 }
290
291             }
292
293         };
294
295         s.toggleDisplay(ev, doLegendToggle);
296     };
297     
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);
303         }
304     };
305 })(jQuery);