vendor/assets/javascripts/highcharts/modules/data.js in highcharts-js-rails-0.2.1 vs vendor/assets/javascripts/highcharts/modules/data.js in highcharts-js-rails-1.0.0
- old
+ new
@@ -1,9 +1,10 @@
/**
- * @license Data plugin for Highcharts v0.1
+ * @license Data plugin for Highcharts
*
- * (c) 2012 Torstein Hønsi
+ * (c) 2012-2013 Torstein Hønsi
+ * Last revision 2012-11-27
*
* License: www.highcharts.com/license
*/
/*
@@ -42,18 +43,18 @@
*
* - googleSpreadsheetKey : String
* A Google Spreadsheet key. See https://developers.google.com/gdata/samples/spreadsheet_sample
* for general information on GS.
*
- * - googleSpreadsheetKey : String
+ * - googleSpreadsheetWorksheet : String
* The Google Spreadsheet worksheet. The available id's can be read from
* https://spreadsheets.google.com/feeds/worksheets/{key}/public/basic
*
- * - itemDilimiter : String
+ * - itemDelimiter : String
* Item or cell delimiter for parsing CSV. Defaults to ",".
*
- * - lineDilimiter : String
+ * - lineDelimiter : String
* Line delimiter for parsing CSV. Defaults to "\n".
*
* - parsed : Function
* A callback function to access the parsed columns, the two-dimentional input data
* array directly, before they are interpreted into series data and categories.
@@ -74,11 +75,13 @@
* - table : String|HTMLElement
* A HTML table or the id of such to be parsed as input data. Related options ara startRow,
* endRow, startColumn and endColumn to delimit what part of the table is used.
*/
+// JSLint options:
/*global jQuery */
+
(function (Highcharts) {
// Utilities
var each = Highcharts.each;
@@ -116,11 +119,10 @@
}
},
dataFound: function () {
-
// Interpret the values into right types
this.parseTypes();
// Use first row for series names?
this.findHeaderRow();
@@ -135,40 +137,49 @@
/**
* Parse a CSV input string
*/
parseCSV: function () {
- var options = this.options,
+ var self = this,
+ options = this.options,
csv = options.csv,
columns = this.columns,
startRow = options.startRow || 0,
endRow = options.endRow || Number.MAX_VALUE,
startColumn = options.startColumn || 0,
endColumn = options.endColumn || Number.MAX_VALUE,
- lines;
+ lines,
+ activeRowNo = 0;
if (csv) {
lines = csv
.replace(/\r\n/g, "\n") // Unix
.replace(/\r/g, "\n") // Mac
.split(options.lineDelimiter || "\n");
each(lines, function (line, rowNo) {
- if (rowNo >= startRow && rowNo <= endRow) {
- var items = line.split(options.itemDelimiter || ',');
+ var trimmed = self.trim(line),
+ isComment = trimmed.indexOf('#') === 0,
+ isBlank = trimmed === '',
+ items;
+
+ if (rowNo >= startRow && rowNo <= endRow && !isComment && !isBlank) {
+ items = line.split(options.itemDelimiter || ',');
each(items, function (item, colNo) {
if (colNo >= startColumn && colNo <= endColumn) {
if (!columns[colNo - startColumn]) {
columns[colNo - startColumn] = [];
}
- columns[colNo - startColumn][rowNo - startRow] = item;
+ columns[colNo - startColumn][activeRowNo] = item;
}
});
+ activeRowNo += 1;
}
- });
+ });
+
this.dataFound();
}
},
/**
@@ -211,17 +222,22 @@
},
/**
* TODO:
* - switchRowsAndColumns
- * - startRow, endRow etc.
*/
parseGoogleSpreadsheet: function () {
var self = this,
options = this.options,
googleSpreadsheetKey = options.googleSpreadsheetKey,
- columns = this.columns;
+ columns = this.columns,
+ startRow = options.startRow || 0,
+ endRow = options.endRow || Number.MAX_VALUE,
+ startColumn = options.startColumn || 0,
+ endColumn = options.endColumn || Number.MAX_VALUE,
+ gr, // google row
+ gc; // google column
if (googleSpreadsheetKey) {
jQuery.getJSON('https://spreadsheets.google.com/feeds/cells/' +
googleSpreadsheetKey + '/' + (options.googleSpreadsheetWorksheet || 'od6') +
'/public/values?alt=json-in-script&callback=?',
@@ -243,19 +259,32 @@
rowCount = Math.max(rowCount, cell.gs$cell.row);
}
// Set up arrays containing the column data
for (i = 0; i < colCount; i++) {
- columns[i] = new Array(rowCount);
+ if (i >= startColumn && i <= endColumn) {
+ // Create new columns with the length of either end-start or rowCount
+ columns[i - startColumn] = [];
+
+ // Setting the length to avoid jslint warning
+ columns[i - startColumn].length = Math.min(rowCount, endRow - startRow);
+ }
}
// Loop over the cells and assign the value to the right
// place in the column arrays
for (i = 0; i < cellCount; i++) {
cell = cells[i];
- columns[cell.gs$cell.col - 1][cell.gs$cell.row - 1] =
- cell.content.$t;
+ gr = cell.gs$cell.row - 1; // rows start at 1
+ gc = cell.gs$cell.col - 1; // columns start at 1
+
+ // If both row and col falls inside start and end
+ // set the transposed cell value in the newly created columns
+ if (gc >= startColumn && gc <= endColumn &&
+ gr >= startRow && gr <= endRow) {
+ columns[gc - startColumn][gr - startRow] = cell.content.$t;
+ }
}
self.dataFound();
});
}
},
@@ -277,11 +306,10 @@
/**
* Trim a string from whitespace
*/
trim: function (str) {
- //return typeof str === 'number' ? str : str.replace(/^\s+|\s+$/g, ''); // fails with spreadsheet
return typeof str === 'string' ? str.replace(/^\s+|\s+$/g, '') : str;
},
/**
* Parse numeric cells in to number types and date types in to true dates.
@@ -300,10 +328,11 @@
row = columns[col].length;
while (row--) {
val = columns[col][row];
floatVal = parseFloat(val);
trimVal = this.trim(val);
+
/*jslint eqeq: true*/
if (trimVal == floatVal) { // is numeric
/*jslint eqeq: false*/
columns[col][row] = floatVal;
@@ -320,11 +349,11 @@
if (col === 0 && typeof dateVal === 'number' && !isNaN(dateVal)) { // is date
columns[col][row] = dateVal;
columns[col].isDatetime = true;
} else { // string
- columns[col][row] = trimVal;
+ columns[col][row] = trimVal === '' ? null : trimVal;
}
}
}
}
@@ -348,11 +377,11 @@
key,
format,
match;
if (parseDate) {
- ret = parseDate;
+ ret = parseDate(val);
}
if (typeof val === 'string') {
for (key in this.dateFormats) {
format = this.dateFormats[key];
@@ -484,24 +513,20 @@
var chart = this;
if (userOptions && userOptions.data) {
Highcharts.data(Highcharts.extend(userOptions.data, {
complete: function (dataOptions) {
- var datasets = [];
- // Don't merge the data arrays themselves
- each(dataOptions.series, function (series, i) {
- datasets[i] = series.data;
- series.data = null;
- });
-
+ // Merge series configs
+ if (userOptions.series) {
+ each(userOptions.series, function (series, i) {
+ userOptions.series[i] = Highcharts.merge(series, dataOptions.series[i]);
+ });
+ }
+
// Do the merge
userOptions = Highcharts.merge(dataOptions, userOptions);
-
- // Re-insert the data
- each(datasets, function (data, i) {
- userOptions.series[i].data = data;
- });
+
proceed.call(chart, userOptions, callback);
}
}));
} else {
proceed.call(chart, userOptions, callback);