| 5 |
lars |
1 |
/**
|
|
|
2 |
* @license Highmaps JS v1.1.9 (2015-10-07)
|
|
|
3 |
* Data module
|
|
|
4 |
*
|
|
|
5 |
* (c) 2012-2014 Torstein Honsi
|
|
|
6 |
*
|
|
|
7 |
* License: www.highcharts.com/license
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
// JSLint options:
|
|
|
11 |
/*global jQuery, HighchartsAdapter */
|
|
|
12 |
|
|
|
13 |
(function (Highcharts) {
|
|
|
14 |
|
|
|
15 |
// Utilities
|
|
|
16 |
var each = Highcharts.each,
|
|
|
17 |
pick = Highcharts.pick,
|
|
|
18 |
inArray = HighchartsAdapter.inArray,
|
|
|
19 |
splat = Highcharts.splat,
|
|
|
20 |
SeriesBuilder;
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
// The Data constructor
|
|
|
24 |
var Data = function (dataOptions, chartOptions) {
|
|
|
25 |
this.init(dataOptions, chartOptions);
|
|
|
26 |
};
|
|
|
27 |
|
|
|
28 |
// Set the prototype properties
|
|
|
29 |
Highcharts.extend(Data.prototype, {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Initialize the Data object with the given options
|
|
|
33 |
*/
|
|
|
34 |
init: function (options, chartOptions) {
|
|
|
35 |
this.options = options;
|
|
|
36 |
this.chartOptions = chartOptions;
|
|
|
37 |
this.columns = options.columns || this.rowsToColumns(options.rows) || [];
|
|
|
38 |
this.firstRowAsNames = pick(options.firstRowAsNames, true);
|
|
|
39 |
this.decimalRegex = options.decimalPoint && new RegExp('^([0-9]+)' + options.decimalPoint + '([0-9]+)$');
|
|
|
40 |
|
|
|
41 |
// This is a two-dimensional array holding the raw, trimmed string values
|
|
|
42 |
// with the same organisation as the columns array. It makes it possible
|
|
|
43 |
// for example to revert from interpreted timestamps to string-based
|
|
|
44 |
// categories.
|
|
|
45 |
this.rawColumns = [];
|
|
|
46 |
|
|
|
47 |
// No need to parse or interpret anything
|
|
|
48 |
if (this.columns.length) {
|
|
|
49 |
this.dataFound();
|
|
|
50 |
|
|
|
51 |
// Parse and interpret
|
|
|
52 |
} else {
|
|
|
53 |
|
|
|
54 |
// Parse a CSV string if options.csv is given
|
|
|
55 |
this.parseCSV();
|
|
|
56 |
|
|
|
57 |
// Parse a HTML table if options.table is given
|
|
|
58 |
this.parseTable();
|
|
|
59 |
|
|
|
60 |
// Parse a Google Spreadsheet
|
|
|
61 |
this.parseGoogleSpreadsheet();
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
},
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Get the column distribution. For example, a line series takes a single column for
|
|
|
68 |
* Y values. A range series takes two columns for low and high values respectively,
|
|
|
69 |
* and an OHLC series takes four columns.
|
|
|
70 |
*/
|
|
|
71 |
getColumnDistribution: function () {
|
|
|
72 |
var chartOptions = this.chartOptions,
|
|
|
73 |
options = this.options,
|
|
|
74 |
xColumns = [],
|
|
|
75 |
getValueCount = function (type) {
|
|
|
76 |
return (Highcharts.seriesTypes[type || 'line'].prototype.pointArrayMap || [0]).length;
|
|
|
77 |
},
|
|
|
78 |
getPointArrayMap = function (type) {
|
|
|
79 |
return Highcharts.seriesTypes[type || 'line'].prototype.pointArrayMap;
|
|
|
80 |
},
|
|
|
81 |
globalType = chartOptions && chartOptions.chart && chartOptions.chart.type,
|
|
|
82 |
individualCounts = [],
|
|
|
83 |
seriesBuilders = [],
|
|
|
84 |
seriesIndex = 0,
|
|
|
85 |
i;
|
|
|
86 |
|
|
|
87 |
each((chartOptions && chartOptions.series) || [], function (series) {
|
|
|
88 |
individualCounts.push(getValueCount(series.type || globalType));
|
|
|
89 |
});
|
|
|
90 |
|
|
|
91 |
// Collect the x-column indexes from seriesMapping
|
|
|
92 |
each((options && options.seriesMapping) || [], function (mapping) {
|
|
|
93 |
xColumns.push(mapping.x || 0);
|
|
|
94 |
});
|
|
|
95 |
|
|
|
96 |
// If there are no defined series with x-columns, use the first column as x column
|
|
|
97 |
if (xColumns.length === 0) {
|
|
|
98 |
xColumns.push(0);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
// Loop all seriesMappings and constructs SeriesBuilders from
|
|
|
102 |
// the mapping options.
|
|
|
103 |
each((options && options.seriesMapping) || [], function (mapping) {
|
|
|
104 |
var builder = new SeriesBuilder(),
|
|
|
105 |
name,
|
|
|
106 |
numberOfValueColumnsNeeded = individualCounts[seriesIndex] || getValueCount(globalType),
|
|
|
107 |
seriesArr = (chartOptions && chartOptions.series) || [],
|
|
|
108 |
series = seriesArr[seriesIndex] || {},
|
|
|
109 |
pointArrayMap = getPointArrayMap(series.type || globalType) || ['y'];
|
|
|
110 |
|
|
|
111 |
// Add an x reader from the x property or from an undefined column
|
|
|
112 |
// if the property is not set. It will then be auto populated later.
|
|
|
113 |
builder.addColumnReader(mapping.x, 'x');
|
|
|
114 |
|
|
|
115 |
// Add all column mappings
|
|
|
116 |
for (name in mapping) {
|
|
|
117 |
if (mapping.hasOwnProperty(name) && name !== 'x') {
|
|
|
118 |
builder.addColumnReader(mapping[name], name);
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// Add missing columns
|
|
|
123 |
for (i = 0; i < numberOfValueColumnsNeeded; i++) {
|
|
|
124 |
if (!builder.hasReader(pointArrayMap[i])) {
|
|
|
125 |
//builder.addNextColumnReader(pointArrayMap[i]);
|
|
|
126 |
// Create and add a column reader for the next free column index
|
|
|
127 |
builder.addColumnReader(undefined, pointArrayMap[i]);
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
seriesBuilders.push(builder);
|
|
|
132 |
seriesIndex++;
|
|
|
133 |
});
|
|
|
134 |
|
|
|
135 |
var globalPointArrayMap = getPointArrayMap(globalType);
|
|
|
136 |
if (globalPointArrayMap === undefined) {
|
|
|
137 |
globalPointArrayMap = ['y'];
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
this.valueCount = {
|
|
|
141 |
global: getValueCount(globalType),
|
|
|
142 |
xColumns: xColumns,
|
|
|
143 |
individual: individualCounts,
|
|
|
144 |
seriesBuilders: seriesBuilders,
|
|
|
145 |
globalPointArrayMap: globalPointArrayMap
|
|
|
146 |
};
|
|
|
147 |
},
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* When the data is parsed into columns, either by CSV, table, GS or direct input,
|
|
|
151 |
* continue with other operations.
|
|
|
152 |
*/
|
|
|
153 |
dataFound: function () {
|
|
|
154 |
|
|
|
155 |
if (this.options.switchRowsAndColumns) {
|
|
|
156 |
this.columns = this.rowsToColumns(this.columns);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// Interpret the info about series and columns
|
|
|
160 |
this.getColumnDistribution();
|
|
|
161 |
|
|
|
162 |
// Interpret the values into right types
|
|
|
163 |
this.parseTypes();
|
|
|
164 |
|
|
|
165 |
// Handle columns if a handleColumns callback is given
|
|
|
166 |
if (this.parsed() !== false) {
|
|
|
167 |
|
|
|
168 |
// Complete if a complete callback is given
|
|
|
169 |
this.complete();
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
},
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Parse a CSV input string
|
|
|
176 |
*/
|
|
|
177 |
parseCSV: function () {
|
|
|
178 |
var self = this,
|
|
|
179 |
options = this.options,
|
|
|
180 |
csv = options.csv,
|
|
|
181 |
columns = this.columns,
|
|
|
182 |
startRow = options.startRow || 0,
|
|
|
183 |
endRow = options.endRow || Number.MAX_VALUE,
|
|
|
184 |
startColumn = options.startColumn || 0,
|
|
|
185 |
endColumn = options.endColumn || Number.MAX_VALUE,
|
|
|
186 |
itemDelimiter,
|
|
|
187 |
lines,
|
|
|
188 |
activeRowNo = 0;
|
|
|
189 |
|
|
|
190 |
if (csv) {
|
|
|
191 |
|
|
|
192 |
lines = csv
|
|
|
193 |
.replace(/\r\n/g, "\n") // Unix
|
|
|
194 |
.replace(/\r/g, "\n") // Mac
|
|
|
195 |
.split(options.lineDelimiter || "\n");
|
|
|
196 |
|
|
|
197 |
itemDelimiter = options.itemDelimiter || (csv.indexOf('\t') !== -1 ? '\t' : ',');
|
|
|
198 |
|
|
|
199 |
each(lines, function (line, rowNo) {
|
|
|
200 |
var trimmed = self.trim(line),
|
|
|
201 |
isComment = trimmed.indexOf('#') === 0,
|
|
|
202 |
isBlank = trimmed === '',
|
|
|
203 |
items;
|
|
|
204 |
|
|
|
205 |
if (rowNo >= startRow && rowNo <= endRow && !isComment && !isBlank) {
|
|
|
206 |
items = line.split(itemDelimiter);
|
|
|
207 |
each(items, function (item, colNo) {
|
|
|
208 |
if (colNo >= startColumn && colNo <= endColumn) {
|
|
|
209 |
if (!columns[colNo - startColumn]) {
|
|
|
210 |
columns[colNo - startColumn] = [];
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
columns[colNo - startColumn][activeRowNo] = item;
|
|
|
214 |
}
|
|
|
215 |
});
|
|
|
216 |
activeRowNo += 1;
|
|
|
217 |
}
|
|
|
218 |
});
|
|
|
219 |
|
|
|
220 |
this.dataFound();
|
|
|
221 |
}
|
|
|
222 |
},
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Parse a HTML table
|
|
|
226 |
*/
|
|
|
227 |
parseTable: function () {
|
|
|
228 |
var options = this.options,
|
|
|
229 |
table = options.table,
|
|
|
230 |
columns = this.columns,
|
|
|
231 |
startRow = options.startRow || 0,
|
|
|
232 |
endRow = options.endRow || Number.MAX_VALUE,
|
|
|
233 |
startColumn = options.startColumn || 0,
|
|
|
234 |
endColumn = options.endColumn || Number.MAX_VALUE;
|
|
|
235 |
|
|
|
236 |
if (table) {
|
|
|
237 |
|
|
|
238 |
if (typeof table === 'string') {
|
|
|
239 |
table = document.getElementById(table);
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
each(table.getElementsByTagName('tr'), function (tr, rowNo) {
|
|
|
243 |
if (rowNo >= startRow && rowNo <= endRow) {
|
|
|
244 |
each(tr.children, function (item, colNo) {
|
|
|
245 |
if ((item.tagName === 'TD' || item.tagName === 'TH') && colNo >= startColumn && colNo <= endColumn) {
|
|
|
246 |
if (!columns[colNo - startColumn]) {
|
|
|
247 |
columns[colNo - startColumn] = [];
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
columns[colNo - startColumn][rowNo - startRow] = item.innerHTML;
|
|
|
251 |
}
|
|
|
252 |
});
|
|
|
253 |
}
|
|
|
254 |
});
|
|
|
255 |
|
|
|
256 |
this.dataFound(); // continue
|
|
|
257 |
}
|
|
|
258 |
},
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
*/
|
|
|
262 |
parseGoogleSpreadsheet: function () {
|
|
|
263 |
var self = this,
|
|
|
264 |
options = this.options,
|
|
|
265 |
googleSpreadsheetKey = options.googleSpreadsheetKey,
|
|
|
266 |
columns = this.columns,
|
|
|
267 |
startRow = options.startRow || 0,
|
|
|
268 |
endRow = options.endRow || Number.MAX_VALUE,
|
|
|
269 |
startColumn = options.startColumn || 0,
|
|
|
270 |
endColumn = options.endColumn || Number.MAX_VALUE,
|
|
|
271 |
gr, // google row
|
|
|
272 |
gc; // google column
|
|
|
273 |
|
|
|
274 |
if (googleSpreadsheetKey) {
|
|
|
275 |
jQuery.ajax({
|
|
|
276 |
dataType: 'json',
|
|
|
277 |
url: 'https://spreadsheets.google.com/feeds/cells/' +
|
|
|
278 |
googleSpreadsheetKey + '/' + (options.googleSpreadsheetWorksheet || 'od6') +
|
|
|
279 |
'/public/values?alt=json-in-script&callback=?',
|
|
|
280 |
error: options.error,
|
|
|
281 |
success: function (json) {
|
|
|
282 |
// Prepare the data from the spreadsheat
|
|
|
283 |
var cells = json.feed.entry,
|
|
|
284 |
cell,
|
|
|
285 |
cellCount = cells.length,
|
|
|
286 |
colCount = 0,
|
|
|
287 |
rowCount = 0,
|
|
|
288 |
i;
|
|
|
289 |
|
|
|
290 |
// First, find the total number of columns and rows that
|
|
|
291 |
// are actually filled with data
|
|
|
292 |
for (i = 0; i < cellCount; i++) {
|
|
|
293 |
cell = cells[i];
|
|
|
294 |
colCount = Math.max(colCount, cell.gs$cell.col);
|
|
|
295 |
rowCount = Math.max(rowCount, cell.gs$cell.row);
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
// Set up arrays containing the column data
|
|
|
299 |
for (i = 0; i < colCount; i++) {
|
|
|
300 |
if (i >= startColumn && i <= endColumn) {
|
|
|
301 |
// Create new columns with the length of either end-start or rowCount
|
|
|
302 |
columns[i - startColumn] = [];
|
|
|
303 |
|
|
|
304 |
// Setting the length to avoid jslint warning
|
|
|
305 |
columns[i - startColumn].length = Math.min(rowCount, endRow - startRow);
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
// Loop over the cells and assign the value to the right
|
|
|
310 |
// place in the column arrays
|
|
|
311 |
for (i = 0; i < cellCount; i++) {
|
|
|
312 |
cell = cells[i];
|
|
|
313 |
gr = cell.gs$cell.row - 1; // rows start at 1
|
|
|
314 |
gc = cell.gs$cell.col - 1; // columns start at 1
|
|
|
315 |
|
|
|
316 |
// If both row and col falls inside start and end
|
|
|
317 |
// set the transposed cell value in the newly created columns
|
|
|
318 |
if (gc >= startColumn && gc <= endColumn &&
|
|
|
319 |
gr >= startRow && gr <= endRow) {
|
|
|
320 |
columns[gc - startColumn][gr - startRow] = cell.content.$t;
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
self.dataFound();
|
|
|
324 |
}
|
|
|
325 |
});
|
|
|
326 |
}
|
|
|
327 |
},
|
|
|
328 |
|
|
|
329 |
/**
|
|
|
330 |
* Trim a string from whitespace
|
|
|
331 |
*/
|
|
|
332 |
trim: function (str, inside) {
|
|
|
333 |
if (typeof str === 'string') {
|
|
|
334 |
str = str.replace(/^\s+|\s+$/g, '');
|
|
|
335 |
|
|
|
336 |
// Clear white space insdie the string, like thousands separators
|
|
|
337 |
if (inside && /^[0-9\s]+$/.test(str)) {
|
|
|
338 |
str = str.replace(/\s/g, '');
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
if (this.decimalRegex) {
|
|
|
342 |
str = str.replace(this.decimalRegex, '$1.$2');
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
return str;
|
|
|
346 |
},
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Parse numeric cells in to number types and date types in to true dates.
|
|
|
350 |
*/
|
|
|
351 |
parseTypes: function () {
|
|
|
352 |
var columns = this.columns,
|
|
|
353 |
col = columns.length;
|
|
|
354 |
|
|
|
355 |
while (col--) {
|
|
|
356 |
this.parseColumn(columns[col], col);
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
},
|
|
|
360 |
|
|
|
361 |
/**
|
|
|
362 |
* Parse a single column. Set properties like .isDatetime and .isNumeric.
|
|
|
363 |
*/
|
|
|
364 |
parseColumn: function (column, col) {
|
|
|
365 |
var rawColumns = this.rawColumns,
|
|
|
366 |
columns = this.columns,
|
|
|
367 |
row = column.length,
|
|
|
368 |
val,
|
|
|
369 |
floatVal,
|
|
|
370 |
trimVal,
|
|
|
371 |
trimInsideVal,
|
|
|
372 |
firstRowAsNames = this.firstRowAsNames,
|
|
|
373 |
isXColumn = inArray(col, this.valueCount.xColumns) !== -1,
|
|
|
374 |
dateVal,
|
|
|
375 |
backup = [],
|
|
|
376 |
diff,
|
|
|
377 |
chartOptions = this.chartOptions,
|
|
|
378 |
descending,
|
|
|
379 |
columnTypes = this.options.columnTypes || [],
|
|
|
380 |
columnType = columnTypes[col],
|
|
|
381 |
forceCategory = isXColumn && ((chartOptions && chartOptions.xAxis && splat(chartOptions.xAxis)[0].type === 'category') || columnType === 'string');
|
|
|
382 |
|
|
|
383 |
if (!rawColumns[col]) {
|
|
|
384 |
rawColumns[col] = [];
|
|
|
385 |
}
|
|
|
386 |
while (row--) {
|
|
|
387 |
val = backup[row] || column[row];
|
|
|
388 |
|
|
|
389 |
trimVal = this.trim(val);
|
|
|
390 |
trimInsideVal = this.trim(val, true);
|
|
|
391 |
floatVal = parseFloat(trimInsideVal);
|
|
|
392 |
|
|
|
393 |
// Set it the first time
|
|
|
394 |
if (rawColumns[col][row] === undefined) {
|
|
|
395 |
rawColumns[col][row] = trimVal;
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
// Disable number or date parsing by setting the X axis type to category
|
|
|
399 |
if (forceCategory || (row === 0 && firstRowAsNames)) {
|
|
|
400 |
column[row] = trimVal;
|
|
|
401 |
|
|
|
402 |
} else if (+trimInsideVal === floatVal) { // is numeric
|
|
|
403 |
|
|
|
404 |
column[row] = floatVal;
|
|
|
405 |
|
|
|
406 |
// If the number is greater than milliseconds in a year, assume datetime
|
|
|
407 |
if (floatVal > 365 * 24 * 3600 * 1000 && columnType !== 'float') {
|
|
|
408 |
column.isDatetime = true;
|
|
|
409 |
} else {
|
|
|
410 |
column.isNumeric = true;
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
if (column[row + 1] !== undefined) {
|
|
|
414 |
descending = floatVal > column[row + 1];
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
// String, continue to determine if it is a date string or really a string
|
|
|
418 |
} else {
|
|
|
419 |
dateVal = this.parseDate(val);
|
|
|
420 |
// Only allow parsing of dates if this column is an x-column
|
|
|
421 |
if (isXColumn && typeof dateVal === 'number' && !isNaN(dateVal) && columnType !== 'float') { // is date
|
|
|
422 |
backup[row] = val;
|
|
|
423 |
column[row] = dateVal;
|
|
|
424 |
column.isDatetime = true;
|
|
|
425 |
|
|
|
426 |
// Check if the dates are uniformly descending or ascending. If they
|
|
|
427 |
// are not, chances are that they are a different time format, so check
|
|
|
428 |
// for alternative.
|
|
|
429 |
if (column[row + 1] !== undefined) {
|
|
|
430 |
diff = dateVal > column[row + 1];
|
|
|
431 |
if (diff !== descending && descending !== undefined) {
|
|
|
432 |
if (this.alternativeFormat) {
|
|
|
433 |
this.dateFormat = this.alternativeFormat;
|
|
|
434 |
row = column.length;
|
|
|
435 |
this.alternativeFormat = this.dateFormats[this.dateFormat].alternative;
|
|
|
436 |
} else {
|
|
|
437 |
column.unsorted = true;
|
|
|
438 |
}
|
|
|
439 |
}
|
|
|
440 |
descending = diff;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
} else { // string
|
|
|
444 |
column[row] = trimVal === '' ? null : trimVal;
|
|
|
445 |
if (row !== 0 && (column.isDatetime || column.isNumeric)) {
|
|
|
446 |
column.mixed = true;
|
|
|
447 |
}
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
// If strings are intermixed with numbers or dates in a parsed column, it is an indication
|
|
|
453 |
// that parsing went wrong or the data was not intended to display as numbers or dates and
|
|
|
454 |
// parsing is too aggressive. Fall back to categories. Demonstrated in the
|
|
|
455 |
// highcharts/demo/column-drilldown sample.
|
|
|
456 |
if (isXColumn && column.mixed) {
|
|
|
457 |
columns[col] = rawColumns[col];
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
// If the 0 column is date or number and descending, reverse all columns.
|
|
|
461 |
if (isXColumn && descending && this.options.sort) {
|
|
|
462 |
for (col = 0; col < columns.length; col++) {
|
|
|
463 |
columns[col].reverse();
|
|
|
464 |
if (firstRowAsNames) {
|
|
|
465 |
columns[col].unshift(columns[col].pop());
|
|
|
466 |
}
|
|
|
467 |
}
|
|
|
468 |
}
|
|
|
469 |
},
|
|
|
470 |
|
|
|
471 |
/**
|
|
|
472 |
* A collection of available date formats, extendable from the outside to support
|
|
|
473 |
* custom date formats.
|
|
|
474 |
*/
|
|
|
475 |
dateFormats: {
|
|
|
476 |
'YYYY-mm-dd': {
|
|
|
477 |
regex: /^([0-9]{4})[\-\/\.]([0-9]{2})[\-\/\.]([0-9]{2})$/,
|
|
|
478 |
parser: function (match) {
|
|
|
479 |
return Date.UTC(+match[1], match[2] - 1, +match[3]);
|
|
|
480 |
}
|
|
|
481 |
},
|
|
|
482 |
'dd/mm/YYYY': {
|
|
|
483 |
regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{4})$/,
|
|
|
484 |
parser: function (match) {
|
|
|
485 |
return Date.UTC(+match[3], match[2] - 1, +match[1]);
|
|
|
486 |
},
|
|
|
487 |
alternative: 'mm/dd/YYYY' // different format with the same regex
|
|
|
488 |
},
|
|
|
489 |
'mm/dd/YYYY': {
|
|
|
490 |
regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{4})$/,
|
|
|
491 |
parser: function (match) {
|
|
|
492 |
return Date.UTC(+match[3], match[1] - 1, +match[2]);
|
|
|
493 |
}
|
|
|
494 |
},
|
|
|
495 |
'dd/mm/YY': {
|
|
|
496 |
regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{2})$/,
|
|
|
497 |
parser: function (match) {
|
|
|
498 |
return Date.UTC(+match[3] + 2000, match[2] - 1, +match[1]);
|
|
|
499 |
},
|
|
|
500 |
alternative: 'mm/dd/YY' // different format with the same regex
|
|
|
501 |
},
|
|
|
502 |
'mm/dd/YY': {
|
|
|
503 |
regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{2})$/,
|
|
|
504 |
parser: function (match) {
|
|
|
505 |
return Date.UTC(+match[3] + 2000, match[1] - 1, +match[2]);
|
|
|
506 |
}
|
|
|
507 |
}
|
|
|
508 |
},
|
|
|
509 |
|
|
|
510 |
/**
|
|
|
511 |
* Parse a date and return it as a number. Overridable through options.parseDate.
|
|
|
512 |
*/
|
|
|
513 |
parseDate: function (val) {
|
|
|
514 |
var parseDate = this.options.parseDate,
|
|
|
515 |
ret,
|
|
|
516 |
key,
|
|
|
517 |
format,
|
|
|
518 |
dateFormat = this.options.dateFormat || this.dateFormat,
|
|
|
519 |
match;
|
|
|
520 |
|
|
|
521 |
if (parseDate) {
|
|
|
522 |
ret = parseDate(val);
|
|
|
523 |
|
|
|
524 |
} else if (typeof val === 'string') {
|
|
|
525 |
// Auto-detect the date format the first time
|
|
|
526 |
if (!dateFormat) {
|
|
|
527 |
for (key in this.dateFormats) {
|
|
|
528 |
format = this.dateFormats[key];
|
|
|
529 |
match = val.match(format.regex);
|
|
|
530 |
if (match) {
|
|
|
531 |
this.dateFormat = dateFormat = key;
|
|
|
532 |
this.alternativeFormat = format.alternative;
|
|
|
533 |
ret = format.parser(match);
|
|
|
534 |
break;
|
|
|
535 |
}
|
|
|
536 |
}
|
|
|
537 |
// Next time, use the one previously found
|
|
|
538 |
} else {
|
|
|
539 |
format = this.dateFormats[dateFormat];
|
|
|
540 |
match = val.match(format.regex);
|
|
|
541 |
if (match) {
|
|
|
542 |
ret = format.parser(match);
|
|
|
543 |
}
|
|
|
544 |
}
|
|
|
545 |
// Fall back to Date.parse
|
|
|
546 |
if (!match) {
|
|
|
547 |
match = Date.parse(val);
|
|
|
548 |
// External tools like Date.js and MooTools extend Date object and
|
|
|
549 |
// returns a date.
|
|
|
550 |
if (typeof match === 'object' && match !== null && match.getTime) {
|
|
|
551 |
ret = match.getTime() - match.getTimezoneOffset() * 60000;
|
|
|
552 |
|
|
|
553 |
// Timestamp
|
|
|
554 |
} else if (typeof match === 'number' && !isNaN(match)) {
|
|
|
555 |
ret = match - (new Date(match)).getTimezoneOffset() * 60000;
|
|
|
556 |
}
|
|
|
557 |
}
|
|
|
558 |
}
|
|
|
559 |
return ret;
|
|
|
560 |
},
|
|
|
561 |
|
|
|
562 |
/**
|
|
|
563 |
* Reorganize rows into columns
|
|
|
564 |
*/
|
|
|
565 |
rowsToColumns: function (rows) {
|
|
|
566 |
var row,
|
|
|
567 |
rowsLength,
|
|
|
568 |
col,
|
|
|
569 |
colsLength,
|
|
|
570 |
columns;
|
|
|
571 |
|
|
|
572 |
if (rows) {
|
|
|
573 |
columns = [];
|
|
|
574 |
rowsLength = rows.length;
|
|
|
575 |
for (row = 0; row < rowsLength; row++) {
|
|
|
576 |
colsLength = rows[row].length;
|
|
|
577 |
for (col = 0; col < colsLength; col++) {
|
|
|
578 |
if (!columns[col]) {
|
|
|
579 |
columns[col] = [];
|
|
|
580 |
}
|
|
|
581 |
columns[col][row] = rows[row][col];
|
|
|
582 |
}
|
|
|
583 |
}
|
|
|
584 |
}
|
|
|
585 |
return columns;
|
|
|
586 |
},
|
|
|
587 |
|
|
|
588 |
/**
|
|
|
589 |
* A hook for working directly on the parsed columns
|
|
|
590 |
*/
|
|
|
591 |
parsed: function () {
|
|
|
592 |
if (this.options.parsed) {
|
|
|
593 |
return this.options.parsed.call(this, this.columns);
|
|
|
594 |
}
|
|
|
595 |
},
|
|
|
596 |
|
|
|
597 |
getFreeIndexes: function (numberOfColumns, seriesBuilders) {
|
|
|
598 |
var s,
|
|
|
599 |
i,
|
|
|
600 |
freeIndexes = [],
|
|
|
601 |
freeIndexValues = [],
|
|
|
602 |
referencedIndexes;
|
|
|
603 |
|
|
|
604 |
// Add all columns as free
|
|
|
605 |
for (i = 0; i < numberOfColumns; i = i + 1) {
|
|
|
606 |
freeIndexes.push(true);
|
|
|
607 |
}
|
|
|
608 |
|
|
|
609 |
// Loop all defined builders and remove their referenced columns
|
|
|
610 |
for (s = 0; s < seriesBuilders.length; s = s + 1) {
|
|
|
611 |
referencedIndexes = seriesBuilders[s].getReferencedColumnIndexes();
|
|
|
612 |
|
|
|
613 |
for (i = 0; i < referencedIndexes.length; i = i + 1) {
|
|
|
614 |
freeIndexes[referencedIndexes[i]] = false;
|
|
|
615 |
}
|
|
|
616 |
}
|
|
|
617 |
|
|
|
618 |
// Collect the values for the free indexes
|
|
|
619 |
for (i = 0; i < freeIndexes.length; i = i + 1) {
|
|
|
620 |
if (freeIndexes[i]) {
|
|
|
621 |
freeIndexValues.push(i);
|
|
|
622 |
}
|
|
|
623 |
}
|
|
|
624 |
|
|
|
625 |
return freeIndexValues;
|
|
|
626 |
},
|
|
|
627 |
|
|
|
628 |
/**
|
|
|
629 |
* If a complete callback function is provided in the options, interpret the
|
|
|
630 |
* columns into a Highcharts options object.
|
|
|
631 |
*/
|
|
|
632 |
complete: function () {
|
|
|
633 |
|
|
|
634 |
var columns = this.columns,
|
|
|
635 |
xColumns = [],
|
|
|
636 |
type,
|
|
|
637 |
options = this.options,
|
|
|
638 |
series,
|
|
|
639 |
data,
|
|
|
640 |
i,
|
|
|
641 |
j,
|
|
|
642 |
r,
|
|
|
643 |
seriesIndex,
|
|
|
644 |
chartOptions,
|
|
|
645 |
allSeriesBuilders = [],
|
|
|
646 |
builder,
|
|
|
647 |
freeIndexes,
|
|
|
648 |
typeCol,
|
|
|
649 |
index;
|
|
|
650 |
|
|
|
651 |
xColumns.length = columns.length;
|
|
|
652 |
if (options.complete || options.afterComplete) {
|
|
|
653 |
|
|
|
654 |
// Get the names and shift the top row
|
|
|
655 |
for (i = 0; i < columns.length; i++) {
|
|
|
656 |
if (this.firstRowAsNames) {
|
|
|
657 |
columns[i].name = columns[i].shift();
|
|
|
658 |
}
|
|
|
659 |
}
|
|
|
660 |
|
|
|
661 |
// Use the next columns for series
|
|
|
662 |
series = [];
|
|
|
663 |
freeIndexes = this.getFreeIndexes(columns.length, this.valueCount.seriesBuilders);
|
|
|
664 |
|
|
|
665 |
// Populate defined series
|
|
|
666 |
for (seriesIndex = 0; seriesIndex < this.valueCount.seriesBuilders.length; seriesIndex++) {
|
|
|
667 |
builder = this.valueCount.seriesBuilders[seriesIndex];
|
|
|
668 |
|
|
|
669 |
// If the builder can be populated with remaining columns, then add it to allBuilders
|
|
|
670 |
if (builder.populateColumns(freeIndexes)) {
|
|
|
671 |
allSeriesBuilders.push(builder);
|
|
|
672 |
}
|
|
|
673 |
}
|
|
|
674 |
|
|
|
675 |
// Populate dynamic series
|
|
|
676 |
while (freeIndexes.length > 0) {
|
|
|
677 |
builder = new SeriesBuilder();
|
|
|
678 |
builder.addColumnReader(0, 'x');
|
|
|
679 |
|
|
|
680 |
// Mark index as used (not free)
|
|
|
681 |
index = inArray(0, freeIndexes);
|
|
|
682 |
if (index !== -1) {
|
|
|
683 |
freeIndexes.splice(index, 1);
|
|
|
684 |
}
|
|
|
685 |
|
|
|
686 |
for (i = 0; i < this.valueCount.global; i++) {
|
|
|
687 |
// Create and add a column reader for the next free column index
|
|
|
688 |
builder.addColumnReader(undefined, this.valueCount.globalPointArrayMap[i]);
|
|
|
689 |
}
|
|
|
690 |
|
|
|
691 |
// If the builder can be populated with remaining columns, then add it to allBuilders
|
|
|
692 |
if (builder.populateColumns(freeIndexes)) {
|
|
|
693 |
allSeriesBuilders.push(builder);
|
|
|
694 |
}
|
|
|
695 |
}
|
|
|
696 |
|
|
|
697 |
// Get the data-type from the first series x column
|
|
|
698 |
if (allSeriesBuilders.length > 0 && allSeriesBuilders[0].readers.length > 0) {
|
|
|
699 |
typeCol = columns[allSeriesBuilders[0].readers[0].columnIndex];
|
|
|
700 |
if (typeCol !== undefined) {
|
|
|
701 |
if (typeCol.isDatetime) {
|
|
|
702 |
type = 'datetime';
|
|
|
703 |
} else if (!typeCol.isNumeric) {
|
|
|
704 |
type = 'category';
|
|
|
705 |
}
|
|
|
706 |
}
|
|
|
707 |
}
|
|
|
708 |
// Axis type is category, then the "x" column should be called "name"
|
|
|
709 |
if (type === 'category') {
|
|
|
710 |
for (seriesIndex = 0; seriesIndex < allSeriesBuilders.length; seriesIndex++) {
|
|
|
711 |
builder = allSeriesBuilders[seriesIndex];
|
|
|
712 |
for (r = 0; r < builder.readers.length; r++) {
|
|
|
713 |
if (builder.readers[r].configName === 'x') {
|
|
|
714 |
builder.readers[r].configName = 'name';
|
|
|
715 |
}
|
|
|
716 |
}
|
|
|
717 |
}
|
|
|
718 |
}
|
|
|
719 |
|
|
|
720 |
// Read data for all builders
|
|
|
721 |
for (seriesIndex = 0; seriesIndex < allSeriesBuilders.length; seriesIndex++) {
|
|
|
722 |
builder = allSeriesBuilders[seriesIndex];
|
|
|
723 |
|
|
|
724 |
// Iterate down the cells of each column and add data to the series
|
|
|
725 |
data = [];
|
|
|
726 |
for (j = 0; j < columns[0].length; j++) { // TODO: which column's length should we use here
|
|
|
727 |
data[j] = builder.read(columns, j);
|
|
|
728 |
}
|
|
|
729 |
|
|
|
730 |
// Add the series
|
|
|
731 |
series[seriesIndex] = {
|
|
|
732 |
data: data
|
|
|
733 |
};
|
|
|
734 |
if (builder.name) {
|
|
|
735 |
series[seriesIndex].name = builder.name;
|
|
|
736 |
}
|
|
|
737 |
if (type === 'category') {
|
|
|
738 |
series[seriesIndex].turboThreshold = 0;
|
|
|
739 |
}
|
|
|
740 |
}
|
|
|
741 |
|
|
|
742 |
|
|
|
743 |
|
|
|
744 |
// Do the callback
|
|
|
745 |
chartOptions = {
|
|
|
746 |
series: series
|
|
|
747 |
};
|
|
|
748 |
if (type) {
|
|
|
749 |
chartOptions.xAxis = {
|
|
|
750 |
type: type
|
|
|
751 |
};
|
|
|
752 |
}
|
|
|
753 |
|
|
|
754 |
if (options.complete) {
|
|
|
755 |
options.complete(chartOptions);
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
// The afterComplete hook is used internally to avoid conflict with the externally
|
|
|
759 |
// available complete option.
|
|
|
760 |
if (options.afterComplete) {
|
|
|
761 |
options.afterComplete(chartOptions);
|
|
|
762 |
}
|
|
|
763 |
}
|
|
|
764 |
}
|
|
|
765 |
});
|
|
|
766 |
|
|
|
767 |
// Register the Data prototype and data function on Highcharts
|
|
|
768 |
Highcharts.Data = Data;
|
|
|
769 |
Highcharts.data = function (options, chartOptions) {
|
|
|
770 |
return new Data(options, chartOptions);
|
|
|
771 |
};
|
|
|
772 |
|
|
|
773 |
// Extend Chart.init so that the Chart constructor accepts a new configuration
|
|
|
774 |
// option group, data.
|
|
|
775 |
Highcharts.wrap(Highcharts.Chart.prototype, 'init', function (proceed, userOptions, callback) {
|
|
|
776 |
var chart = this;
|
|
|
777 |
|
|
|
778 |
if (userOptions && userOptions.data) {
|
|
|
779 |
Highcharts.data(Highcharts.extend(userOptions.data, {
|
|
|
780 |
|
|
|
781 |
afterComplete: function (dataOptions) {
|
|
|
782 |
var i, series;
|
|
|
783 |
|
|
|
784 |
// Merge series configs
|
|
|
785 |
if (userOptions.hasOwnProperty('series')) {
|
|
|
786 |
if (typeof userOptions.series === 'object') {
|
|
|
787 |
i = Math.max(userOptions.series.length, dataOptions.series.length);
|
|
|
788 |
while (i--) {
|
|
|
789 |
series = userOptions.series[i] || {};
|
|
|
790 |
userOptions.series[i] = Highcharts.merge(series, dataOptions.series[i]);
|
|
|
791 |
}
|
|
|
792 |
} else { // Allow merging in dataOptions.series (#2856)
|
|
|
793 |
delete userOptions.series;
|
|
|
794 |
}
|
|
|
795 |
}
|
|
|
796 |
|
|
|
797 |
// Do the merge
|
|
|
798 |
userOptions = Highcharts.merge(dataOptions, userOptions);
|
|
|
799 |
|
|
|
800 |
proceed.call(chart, userOptions, callback);
|
|
|
801 |
}
|
|
|
802 |
}), userOptions);
|
|
|
803 |
} else {
|
|
|
804 |
proceed.call(chart, userOptions, callback);
|
|
|
805 |
}
|
|
|
806 |
});
|
|
|
807 |
|
|
|
808 |
/**
|
|
|
809 |
* Creates a new SeriesBuilder. A SeriesBuilder consists of a number
|
|
|
810 |
* of ColumnReaders that reads columns and give them a name.
|
|
|
811 |
* Ex: A series builder can be constructed to read column 3 as 'x' and
|
|
|
812 |
* column 7 and 8 as 'y1' and 'y2'.
|
|
|
813 |
* The output would then be points/rows of the form {x: 11, y1: 22, y2: 33}
|
|
|
814 |
*
|
|
|
815 |
* The name of the builder is taken from the second column. In the above
|
|
|
816 |
* example it would be the column with index 7.
|
|
|
817 |
* @constructor
|
|
|
818 |
*/
|
|
|
819 |
SeriesBuilder = function () {
|
|
|
820 |
this.readers = [];
|
|
|
821 |
this.pointIsArray = true;
|
|
|
822 |
};
|
|
|
823 |
|
|
|
824 |
/**
|
|
|
825 |
* Populates readers with column indexes. A reader can be added without
|
|
|
826 |
* a specific index and for those readers the index is taken sequentially
|
|
|
827 |
* from the free columns (this is handled by the ColumnCursor instance).
|
|
|
828 |
* @returns {boolean}
|
|
|
829 |
*/
|
|
|
830 |
SeriesBuilder.prototype.populateColumns = function (freeIndexes) {
|
|
|
831 |
var builder = this,
|
|
|
832 |
enoughColumns = true;
|
|
|
833 |
|
|
|
834 |
// Loop each reader and give it an index if its missing.
|
|
|
835 |
// The freeIndexes.shift() will return undefined if there
|
|
|
836 |
// are no more columns.
|
|
|
837 |
each(builder.readers, function (reader) {
|
|
|
838 |
if (reader.columnIndex === undefined) {
|
|
|
839 |
reader.columnIndex = freeIndexes.shift();
|
|
|
840 |
}
|
|
|
841 |
});
|
|
|
842 |
|
|
|
843 |
// Now, all readers should have columns mapped. If not
|
|
|
844 |
// then return false to signal that this series should
|
|
|
845 |
// not be added.
|
|
|
846 |
each(builder.readers, function (reader) {
|
|
|
847 |
if (reader.columnIndex === undefined) {
|
|
|
848 |
enoughColumns = false;
|
|
|
849 |
}
|
|
|
850 |
});
|
|
|
851 |
|
|
|
852 |
return enoughColumns;
|
|
|
853 |
};
|
|
|
854 |
|
|
|
855 |
/**
|
|
|
856 |
* Reads a row from the dataset and returns a point or array depending
|
|
|
857 |
* on the names of the readers.
|
|
|
858 |
* @param columns
|
|
|
859 |
* @param rowIndex
|
|
|
860 |
* @returns {Array | Object}
|
|
|
861 |
*/
|
|
|
862 |
SeriesBuilder.prototype.read = function (columns, rowIndex) {
|
|
|
863 |
var builder = this,
|
|
|
864 |
pointIsArray = builder.pointIsArray,
|
|
|
865 |
point = pointIsArray ? [] : {},
|
|
|
866 |
columnIndexes;
|
|
|
867 |
|
|
|
868 |
// Loop each reader and ask it to read its value.
|
|
|
869 |
// Then, build an array or point based on the readers names.
|
|
|
870 |
each(builder.readers, function (reader) {
|
|
|
871 |
var value = columns[reader.columnIndex][rowIndex];
|
|
|
872 |
if (pointIsArray) {
|
|
|
873 |
point.push(value);
|
|
|
874 |
} else {
|
|
|
875 |
point[reader.configName] = value;
|
|
|
876 |
}
|
|
|
877 |
});
|
|
|
878 |
|
|
|
879 |
// The name comes from the first column (excluding the x column)
|
|
|
880 |
if (this.name === undefined && builder.readers.length >= 2) {
|
|
|
881 |
columnIndexes = builder.getReferencedColumnIndexes();
|
|
|
882 |
if (columnIndexes.length >= 2) {
|
|
|
883 |
// remove the first one (x col)
|
|
|
884 |
columnIndexes.shift();
|
|
|
885 |
|
|
|
886 |
// Sort the remaining
|
|
|
887 |
columnIndexes.sort();
|
|
|
888 |
|
|
|
889 |
// Now use the lowest index as name column
|
|
|
890 |
this.name = columns[columnIndexes.shift()].name;
|
|
|
891 |
}
|
|
|
892 |
}
|
|
|
893 |
|
|
|
894 |
return point;
|
|
|
895 |
};
|
|
|
896 |
|
|
|
897 |
/**
|
|
|
898 |
* Creates and adds ColumnReader from the given columnIndex and configName.
|
|
|
899 |
* ColumnIndex can be undefined and in that case the reader will be given
|
|
|
900 |
* an index when columns are populated.
|
|
|
901 |
* @param columnIndex {Number | undefined}
|
|
|
902 |
* @param configName
|
|
|
903 |
*/
|
|
|
904 |
SeriesBuilder.prototype.addColumnReader = function (columnIndex, configName) {
|
|
|
905 |
this.readers.push({
|
|
|
906 |
columnIndex: columnIndex,
|
|
|
907 |
configName: configName
|
|
|
908 |
});
|
|
|
909 |
|
|
|
910 |
if (!(configName === 'x' || configName === 'y' || configName === undefined)) {
|
|
|
911 |
this.pointIsArray = false;
|
|
|
912 |
}
|
|
|
913 |
};
|
|
|
914 |
|
|
|
915 |
/**
|
|
|
916 |
* Returns an array of column indexes that the builder will use when
|
|
|
917 |
* reading data.
|
|
|
918 |
* @returns {Array}
|
|
|
919 |
*/
|
|
|
920 |
SeriesBuilder.prototype.getReferencedColumnIndexes = function () {
|
|
|
921 |
var i,
|
|
|
922 |
referencedColumnIndexes = [],
|
|
|
923 |
columnReader;
|
|
|
924 |
|
|
|
925 |
for (i = 0; i < this.readers.length; i = i + 1) {
|
|
|
926 |
columnReader = this.readers[i];
|
|
|
927 |
if (columnReader.columnIndex !== undefined) {
|
|
|
928 |
referencedColumnIndexes.push(columnReader.columnIndex);
|
|
|
929 |
}
|
|
|
930 |
}
|
|
|
931 |
|
|
|
932 |
return referencedColumnIndexes;
|
|
|
933 |
};
|
|
|
934 |
|
|
|
935 |
/**
|
|
|
936 |
* Returns true if the builder has a reader for the given configName.
|
|
|
937 |
* @param configName
|
|
|
938 |
* @returns {boolean}
|
|
|
939 |
*/
|
|
|
940 |
SeriesBuilder.prototype.hasReader = function (configName) {
|
|
|
941 |
var i, columnReader;
|
|
|
942 |
for (i = 0; i < this.readers.length; i = i + 1) {
|
|
|
943 |
columnReader = this.readers[i];
|
|
|
944 |
if (columnReader.configName === configName) {
|
|
|
945 |
return true;
|
|
|
946 |
}
|
|
|
947 |
}
|
|
|
948 |
// Else return undefined
|
|
|
949 |
};
|
|
|
950 |
|
|
|
951 |
|
|
|
952 |
|
|
|
953 |
}(Highcharts));
|