Debian Codegraph @

js/widgets/widget-chart.js

raw symbols SCIP
1 /* Widget: chart (beta) - updated 10/31/2015 (v2.24.0) *//*
2 * Requires tablesorter v2.8+ and jQuery 1.7+
3 */
4 /*jshint browser:true, jquery:true, unused:false */
5 /*global jQuery: false */
6 ;(function($) {
7 'use strict';
8
9 var ts = $.tablesorter,
10
11 // temp variables
12 chart_cols = [],
13 chart_headers = [],
14 // google charts
15 chart_rows = [],
16 chart_data = [],
17 // highcharts
18 chart_categories = [],
19 chart_series = [],
20 // fusioncharts
21 chart_category = [],
22 chart_dataset = [],
23
24 chart = ts.chart = {
25
26 // regex used to strip out non-digit values before sending
27 // the string to the $.tablesorter.formatFloat function
28 nonDigit : /[^\d,.\-()]/g,
29
30 init: function(c, wo) {
31 c.$table
32 .off(wo.chart_event)
33 .on(wo.chart_event, function() {
34 if (this.hasInitialized) {
35 // refresh 'c' variable in case options are updated dynamically
36 var c = this.config;
37 chart.getCols(c, c.widgetOptions);
38 chart.getData(c, c.widgetOptions);
39 }
40 });
41 },
42
43 getCols: function(c, wo) {
44 var i;
45 chart_cols = [];
46 chart_series = [];
47 chart_dataset = [];
48
49 for ( i = 0; i < c.columns; i++ ) {
50 if ( wo.chart_useSelector && ts.hasWidget( c.table, 'columnSelector' ) && !c.selector.auto ) {
51 if ( ( c.selector.states[i] && $.inArray(i, wo.chart_ignoreColumns) < 0 ) ||
52 i === wo.chart_labelCol || i === wo.chart_sort[0][0] ) {
53 chart_cols.push(i);
54 }
55 } else {
56 if ( $.inArray(i, wo.chart_ignoreColumns) < 0 || i === wo.chart_labelCol || i === wo.chart_sort[0][0] ) {
57 chart_cols.push(i);
58 }
59 }
60 }
61 },
62
63 getData: function(c, wo) {
64 chart.getHeaders(c, wo);
65 chart.getRows(c, wo);
66
67 /* == Google data ==
68 array of arrays (Google charts)
69 [
70 [ "Year", "Sales", "Expenses" ],
71 [ "2004", 1000, 400 ],
72 [ "2005", 1170, 460 ],
73 [ "2006", 660, 1120 ],
74 [ "2007", 1030, 540 ]
75 ]
76
77 == Highcharts ==
78 categories -> [ '2004', '2005', '2006', '2007' ]
79 series -> [{
80 name: 'Sales',
81 data: [ 1000, 1170, 660, 1030 ]
82 }, {
83 name: 'Expenses',
84 data: [ 400, 460, 1120, 540 ]
85 }]
86
87 == Fusioncharts
88 "categories": [{
89 "category": [
90 {"label": "2004"},
91 {"label": "2005"},
92 {"label": "2006"},
93 {"label": "2007"}
94 ]
95 }],
96 "dataset": [
97 {
98 "seriesname": "Sales",
99 "data": [
100 {"value": "1000"},
101 {"value": "1170"},
102 {"value": "660"},
103 {"value": "1030"}
104 ]
105 },{
106 "seriesname": "Expenses",
107 "data": [
108 {"value": "400"},
109 {"value": "600"},
110 {"value": "1120"},
111 {"value": "540"}
112 ]
113 }
114 ]
115 */
116
117 chart_data = [ chart_headers ];
118 $.each(chart_rows, function(k, row) {
119 chart_data.push(row);
120 });
121
122 c.chart = {
123 // google
124 data: chart_data,
125 // highcharts
126 categories: chart_categories,
127 series: chart_series,
128 // FusionCharts
129 category: chart_category,
130 dataset: chart_dataset
131 };
132 },
133
134 getHeaders: function(c, wo) {
135 var text;
136 chart_headers = [];
137 chart_series = [];
138 chart_dataset = [];
139 chart_headers.push( c.headerContent[wo.chart_labelCol] );
140 $.each(chart_cols, function(k, col) {
141 if (col === wo.chart_labelCol) {
142 return true;
143 }
144 text = c.headerContent[col];
145 chart_headers.push( text );
146 chart_series.push( { name: text, data: [] } );
147 chart_dataset.push( { seriesname: text, data: [] } );
148 });
149 },
150
151 getRows: function(c, wo) {
152 var norm_rows = c.cache[0].normalized,
153 rows = [];
154 chart_rows = [];
155 chart_categories = [];
156 chart_category = [];
157
158 $.each(norm_rows, function(indx, rowVal) {
159 var i, txt,
160 $tr = rowVal[c.columns].$row,
161 $cells = $tr.children('th,td'),
162 row = [];
163 if (
164 (/v/i.test(wo.chart_incRows) && $tr.is(':visible')) ||
165 (/f/i.test(wo.chart_incRows) && !$tr.hasClass(wo.filter_filteredRow || 'filtered')) ||
166 (!/(v|f)/i.test(wo.chart_incRows))
167 ) {
168 // Add all cols (don't mess up indx for sorting)
169 for (i = 0; i < c.columns; i++) {
170 if ( $.inArray(indx, wo.chart_parsed) >= 0 ) {
171 row.push( rowVal[i] );
172 } else {
173 txt = $cells[i].getAttribute( c.textAttribute ) || $cells[i].textContent || $cells.eq(i).text();
174 row.push( $.trim( txt ) );
175 }
176 }
177 rows.push(row);
178 }
179 });
180
181 // sort based on chart_sort
182 rows.sort(function(a, b) {
183 if ( wo.chart_sort[0][1] === 1 ) {
184 return ts.sortNatural( b[wo.chart_sort[0][0]], a[wo.chart_sort[0][0]] );
185 }
186 return ts.sortNatural( a[wo.chart_sort[0][0]], b[wo.chart_sort[0][0]] );
187 });
188
189 $.each(rows, function(i, rowVal) {
190 var value,
191 objIndex = 0,
192 row = [],
193 label = rowVal[wo.chart_labelCol];
194
195 row.push( '' + label );
196
197 $.each(rowVal, function(indx, cellValue) {
198 var tempVal;
199 if (indx === wo.chart_labelCol) {
200 chart_categories.push( cellValue );
201 chart_category.push({ 'label': cellValue });
202 return true;
203 }
204 value = false;
205 if ( wo.chart_useSelector && ts.hasWidget( c.table, 'columnSelector' ) && !c.selector.auto ) {
206 if ( c.selector.states[indx] && $.inArray(indx, wo.chart_ignoreColumns) < 0 ) {
207 value = '' + cellValue;
208 }
209 } else {
210 if ($.inArray(indx, wo.chart_ignoreColumns) < 0) {
211 value = '' + cellValue;
212 }
213 }
214
215 if (value !== false) {
216 if ( /s/i.test( '' + wo.chart_layout[indx] ) ) {
217 row.push( value );
218 chart_series[objIndex].data.push( value );
219 chart_dataset[objIndex].data.push( value );
220 } else {
221 // using format float, after stripping out all non-digit values
222 tempVal = ts.formatFloat( value.replace( chart.nonDigit, '' ), c.table );
223 tempVal = isNaN(tempVal) ? value : tempVal;
224 // if tempVal ends up being an empty string, fall back to value
225 row.push( tempVal );
226 chart_series[objIndex].data.push( tempVal );
227 chart_dataset[objIndex].data.push( { value : tempVal } );
228 }
229 objIndex++;
230 }
231 });
232 chart_rows.push(row);
233 });
234 },
235
236 remove: function(c, wo) {
237 c.$table.off(wo.chart_event);
238 }
239
240 };
241
242 ts.addWidget({
243 id: 'chart',
244 options: {
245 // (a)ll, (v)isible or (f)iltered - only the first letter is needed
246 chart_incRows: 'filtered',
247 // prefer columnSelector for ignoreColumns
248 chart_useSelector: false,
249 // columns to ignore [0, 1,... ] (zero-based index)
250 chart_ignoreColumns: [],
251 // Use parsed data instead of cell.text()
252 chart_parsed: [],
253 // data output layout, float is default
254 chart_layout: {
255 // first element is a string, all others will be float
256 0: 'string'
257 },
258 // Set the label column
259 chart_labelCol: 0,
260 // data sort, should always be first row, might want [[0,1]]
261 chart_sort: [ [ 0, 0 ] ],
262 // event to trigger get updated data
263 chart_event: 'chartData'
264 },
265
266 init: function(table, thisWidget, c, wo) {
267 chart.init(c, wo);
268 },
269
270 remove: function(table, c, wo) {
271 chart.remove(c, wo);
272 }
273 });
274
275 })(jQuery);