highcharts
Version:
JavaScript charting framework
1,298 lines (1,292 loc) • 80.1 kB
JavaScript
/* *
*
* Data module
*
* (c) 2012-2026 Highsoft AS
* Author: Torstein Hønsi
*
* Integration of this software requires a license.
* - For commercial use, see www.highcharts.com/license
* - For non-commercial, see www.highcharts.com/license-eula
*
*
* */
'use strict';
import Axis from '../Core/Axis/Axis.js';
import Chart from '../Core/Chart/Chart.js';
import { getOptions } from '../Core/Defaults.js';
import DataTableCore from '../Data/DataTableCore.js';
import G from '../Core/Globals.js';
const { doc } = G;
import { ajax } from '../Core/HttpUtilities.js';
import Point from '../Core/Series/Point.js';
import SeriesRegistry from '../Core/Series/SeriesRegistry.js';
const { seriesTypes } = SeriesRegistry;
import Time from '../Core/Time.js';
import { addEvent, defined, extend, fireEvent, internalClearTimeout, isNumber, merge, objectEach, splat } from '../Shared/Utilities.js';
import { error } from '../Core/Utilities.js';
/* *
*
* Functions
*
* */
/**
* Get the free column indexes.
*
* @param {number} numberOfColumns
* The number of columns.
*
* @param {Array<SeriesBuilder>} seriesBuilders
* The seriesBuilders.
*
* @return {Array<number>}
* The free indexes.
*
* @internal
*/
function getFreeIndexes(numberOfColumns, seriesBuilders) {
// Add all columns as free
const freeIndexes = new Array(numberOfColumns).fill(true), freeIndexValues = [];
// Loop all defined builders and remove their referenced columns
seriesBuilders.forEach((seriesBuilder) => {
seriesBuilder.getReferencedColumnIndexes().forEach((index) => {
freeIndexes[index] = false;
});
});
// Collect the values for the free indexes
freeIndexes.forEach((isFree, i) => {
if (isFree) {
freeIndexValues.push(i);
}
});
return freeIndexValues;
}
/**
* Checks if the data options has URL options.
*
* @internal
*
* @param {Highcharts.DataOptions} options
* The data options to check.
*
* @return {boolean}
* Returns true if any of the URL options is set.
*/
function hasURLOption(options) {
return !!(options.rowsURL || options.csvURL || options.columnsURL);
}
/* *
*
* Class
*
* */
/**
* The Data class
*
* @requires modules/data
*
* @class
* @name Highcharts.Data
*
* @param {Highcharts.DataOptions} dataOptions
*
* @param {Highcharts.Options} [chartOptions]
*
* @param {Highcharts.Chart} [chart]
*/
class Data {
/* *
*
* Static Properties
*
* */
/**
* Creates a data object to parse data for a chart.
*
* @function Highcharts.data
*/
static data(dataOptions, chartOptions = {}, chart) {
return new Data(dataOptions, chartOptions, chart);
}
/**
* Reorganize rows into columns.
*
* @function Highcharts.Data.rowsToColumns
*/
static rowsToColumns(rows) {
let row, rowsLength, col, colsLength, columns;
if (rows) {
columns = [];
rowsLength = rows.length;
for (row = 0; row < rowsLength; row++) {
colsLength = rows[row].length;
for (col = 0; col < colsLength; col++) {
if (!columns[col]) {
columns[col] = [];
}
columns[col][row] = rows[row][col];
}
}
}
return columns;
}
/* *
*
* Constructors
*
* */
constructor(dataOptions, chartOptions = {}, chart) {
/**
* A collection of two-dimensional arrays.
* @internal
*/
this.rowsToColumns = Data.rowsToColumns; // Backwards compatibility
/**
* A collection of available date formats, extendable from the outside to
* support custom date formats.
*
* @name Highcharts.Data#dateFormats
* @type {Highcharts.Dictionary<Highcharts.DataDateFormatObject>}
*/
this.dateFormats = {
'YYYY/mm/dd': {
regex: /^(\d{4})[\-\/\.](\d{1,2})[\-\/\.](\d{1,2})$/,
parser: function (match) {
return (match ?
Date.UTC(+match[1], +match[2] - 1, +match[3]) :
NaN);
}
},
'dd/mm/YYYY': {
regex: /^(\d{1,2})[\-\/\.](\d{1,2})[\-\/\.](\d{4})$/,
parser: function (match) {
return (match ?
Date.UTC(+match[3], +match[2] - 1, +match[1]) :
NaN);
},
alternative: 'mm/dd/YYYY' // Different format with the same regex
},
'mm/dd/YYYY': {
regex: /^(\d{1,2})[\-\/\.](\d{1,2})[\-\/\.](\d{4})$/,
parser: function (match) {
return (match ?
Date.UTC(+match[3], +match[1] - 1, +match[2]) :
NaN);
}
},
'dd/mm/YY': {
regex: /^(\d{1,2})[\-\/\.](\d{1,2})[\-\/\.](\d{2})$/,
parser: function (match) {
if (!match) {
return NaN;
}
const d = new Date();
let year = +match[3];
if (year > (d.getFullYear() - 2000)) {
year += 1900;
}
else {
year += 2000;
}
return Date.UTC(year, +match[2] - 1, +match[1]);
},
alternative: 'mm/dd/YY' // Different format with the same regex
},
'mm/dd/YY': {
regex: /^(\d{1,2})[\-\/\.](\d{1,2})[\-\/\.](\d{2})$/,
parser: function (match) {
return (match ?
Date.UTC(+match[3] + 2000, +match[1] - 1, +match[2]) :
NaN);
}
}
};
this.chart = chart;
this.chartOptions = chartOptions;
this.options = dataOptions;
this.rawColumns = [];
this.init(dataOptions, chartOptions, chart);
}
/* *
*
* Functions
*
* */
/**
* Initialize the Data object with the given options
*
* @internal
* @function Highcharts.Data#init
*/
init(dataOptions, chartOptions, chart) {
let decimalPoint = dataOptions.decimalPoint, hasData;
if (chartOptions) {
this.chartOptions = chartOptions;
}
if (chart) {
this.chart = chart;
}
if (decimalPoint !== '.' && decimalPoint !== ',') {
decimalPoint = void 0;
}
this.options = dataOptions;
this.columns = (dataOptions.columns ||
this.rowsToColumns(dataOptions.rows) ||
[]);
this.firstRowAsNames = dataOptions.firstRowAsNames ??
this.firstRowAsNames ?? true;
this.decimalRegex = (decimalPoint &&
new RegExp('^(-?[0-9]+)' + decimalPoint + '([0-9]+)$'));
// Always stop old polling when we have new options
if (this.liveDataTimeout !== void 0) {
internalClearTimeout(this.liveDataTimeout);
}
// This is a two-dimensional array holding the raw, trimmed string
// values with the same organization as the columns array. It makes it
// possible for example to revert from interpreted timestamps to
// string-based categories.
this.rawColumns = [];
// No need to parse or interpret anything
if (this.columns.length) {
this.dataFound();
hasData = !hasURLOption(dataOptions);
}
if (!hasData) {
// Fetch live data
hasData = this.fetchLiveData();
}
if (!hasData) {
// Parse a CSV string if options.csv is given. The parseCSV function
// returns a columns array, if it has no length, we have no data
hasData = Boolean(this.parseCSV().length);
}
if (!hasData) {
// Parse a HTML table if options.table is given
hasData = Boolean(this.parseTable().length);
}
if (!hasData) {
// Parse a Google Spreadsheet
hasData = this.parseGoogleSpreadsheet();
}
if (!hasData && dataOptions.afterComplete) {
dataOptions.afterComplete(this);
}
}
/**
* Get the column distribution. For example, a line series takes a single
* column for Y values. A range series takes two columns for low and high
* values respectively, and an OHLC series takes four columns.
*
* @function Highcharts.Data#getColumnDistribution
* @internal
*/
getColumnDistribution() {
const chartOptions = this.chartOptions, options = this.options, xColumns = [], getValueCount = function (type = 'line') {
return (seriesTypes[type].prototype.pointArrayMap || [0]).length;
}, getPointArrayMap = function (type = 'line') {
return seriesTypes[type].prototype.pointArrayMap;
}, globalType = chartOptions?.chart?.type, individualCounts = [], seriesBuilders = [],
// If no series mapping is defined, check if the series array is
// defined with types.
seriesMapping = (options?.seriesMapping ||
chartOptions?.series?.map(function () {
return { x: 0 };
}) ||
[]);
let seriesIndex = 0;
(chartOptions?.series || []).forEach((series) => {
individualCounts.push(getValueCount(series.type || globalType));
});
// Collect the x-column indexes from seriesMapping
seriesMapping.forEach((mapping) => {
xColumns.push(mapping.x || 0);
});
// If there are no defined series with x-columns, use the first column
// as x column
if (xColumns.length === 0) {
xColumns.push(0);
}
// Loop all seriesMappings and constructs SeriesBuilders from
// the mapping options.
seriesMapping.forEach((mapping) => {
const builder = new SeriesBuilder(), numberOfValueColumnsNeeded = individualCounts[seriesIndex] ||
getValueCount(globalType), seriesArr = chartOptions?.series ?? [], series = seriesArr[seriesIndex] ?? {}, defaultPointArrayMap = getPointArrayMap(series.type || globalType), pointArrayMap = defaultPointArrayMap ?? ['y'];
if (
// User-defined x.mapping
defined(mapping.x) ||
// All non cartesian don't need 'x'
series.isCartesian ||
// Except pie series:
!defaultPointArrayMap) {
// Add an x reader from the x property or from an undefined
// column if the property is not set. It will then be auto
// populated later.
builder.addColumnReader(mapping.x, 'x');
}
// Add all column mappings
objectEach(mapping, function (val, name) {
if (name !== 'x') {
builder.addColumnReader(val, name);
}
});
// Add missing columns
for (let i = 0; i < numberOfValueColumnsNeeded; i++) {
if (!builder.hasReader(pointArrayMap[i])) {
// Create and add a column reader for the next free column
// index
builder.addColumnReader(void 0, pointArrayMap[i]);
}
}
seriesBuilders.push(builder);
seriesIndex++;
});
let globalPointArrayMap = getPointArrayMap(globalType);
if (typeof globalPointArrayMap === 'undefined') {
globalPointArrayMap = ['y'];
}
this.valueCount = {
global: getValueCount(globalType),
xColumns: xColumns,
individual: individualCounts,
seriesBuilders: seriesBuilders,
globalPointArrayMap: globalPointArrayMap
};
}
/**
* When the data is parsed into columns, either by CSV, table, GS or direct
* input, continue with other operations.
*
* @internal
* @function Highcharts.Data#dataFound
*/
dataFound() {
if (this.options.switchRowsAndColumns) {
this.columns = this.rowsToColumns(this.columns);
}
// Interpret the info about series and columns
this.getColumnDistribution();
// Interpret the values into right types
this.parseTypes();
// Handle columns if a handleColumns callback is given
if (this.parsed() !== false) {
// Complete if a complete callback is given
this.complete();
}
}
/**
* Parse a CSV input string
*
* @function Highcharts.Data#parseCSV
*/
parseCSV(inOptions) {
const self = this, columns = this.columns = [], options = inOptions || this.options, startColumn = options.startColumn || 0, endColumn = options.endColumn || Number.MAX_VALUE, dataTypes = [],
// We count potential delimiters in the prepass, and use the
// result as the basis of half-intelligent guesses.
potDelimiters = {
',': 0,
';': 0,
'\t': 0
};
let csv = options.csv, startRow = options.startRow || 0, endRow = options.endRow || Number.MAX_VALUE, itemDelimiter, lines, rowIt = 0;
/*
This implementation is quite verbose. It will be shortened once
it's stable and passes all the test.
It's also not written with speed in mind, instead everything is
very segregated, and there a several redundant loops.
This is to make it easier to stabilize the code initially.
We do a pre-pass on the first 4 rows to make some intelligent
guesses on the set. Guessed delimiters are in this pass counted.
Auto detecting delimiters
- If we meet a quoted string, the next symbol afterwards
(that's not \s, \t) is the delimiter
- If we meet a date, the next symbol afterwards is the delimiter
Date formats
- If we meet a column with date formats, check all of them to
see if one of the potential months crossing 12. If it does,
we now know the format
It would make things easier to guess the delimiter before
doing the actual parsing.
General rules:
- Quoting is allowed, e.g: "Col 1",123,321
- Quoting is optional, e.g.: Col1,123,321
- Double quoting is escaping, e.g. "Col ""Hello world""",123
- Spaces are considered part of the data: Col1 ,123
- New line is always the row delimiter
- Potential column delimiters are , ; \t
- First row may optionally contain headers
- The last row may or may not have a row delimiter
- Comments are optionally supported, in which case the comment
must start at the first column, and the rest of the line will
be ignored
*/
/**
* Parse a single row.
* @internal
*/
function parseRow(columnStr, rowNumber, noAdd, callbacks) {
let i = 0, c = '', cl = '', cn = '', token = '', actualColumn = 0, column = 0;
/**
* Read a single character from the column string.
*
* @internal
*/
function read(j) {
c = columnStr[j];
cl = columnStr[j - 1];
cn = columnStr[j + 1];
}
/**
* Push a type to the dataTypes array.
*
* @internal
*/
function pushType(type) {
if (dataTypes.length < column + 1) {
dataTypes.push([type]);
}
if (dataTypes[column][dataTypes[column].length - 1] !== type) {
dataTypes[column].push(type);
}
}
/**
* Push a token to the columns array.
*
* @internal
*/
function push() {
if (startColumn > actualColumn || actualColumn > endColumn) {
// Skip this column, but increment the column count (#7272)
++actualColumn;
token = '';
return;
}
if (!options.columnTypes) {
if (!isNaN(parseFloat(token)) && isFinite(token)) {
token = parseFloat(token);
pushType('number');
}
else if (!isNaN(Date.parse(token))) {
token = token.replace(/\//g, '-');
pushType('date');
}
else {
pushType('string');
}
}
if (columns.length < column + 1) {
columns.push([]);
}
if (!noAdd) {
// Don't push - if there's a varying amount of columns
// for each row, pushing will skew everything down n slots
columns[column][rowNumber] = token;
}
token = '';
++column;
++actualColumn;
}
if (!columnStr.trim().length) {
return;
}
if (columnStr.trim()[0] === '#') {
return;
}
for (; i < columnStr.length; i++) {
read(i);
if (c === '"') {
read(++i);
while (i < columnStr.length) {
if (c === '"' && cl !== '"' && cn !== '"') {
break;
}
if (c !== '"' || (c === '"' && cl !== '"')) {
token += c;
}
read(++i);
}
// Perform "plugin" handling
}
else if (callbacks?.[c]) {
if (callbacks[c](c, token)) {
push();
}
// Delimiter - push current token
}
else if (c === itemDelimiter) {
push();
// Actual column data
}
else {
token += c;
}
}
push();
}
/**
* Attempt to guess the delimiter. We do a separate parse pass here
* because we need to count potential delimiters softly without making
* any assumptions.
* @internal
*/
function guessDelimiter(lines) {
let points = 0, commas = 0, guessed = false;
lines.some(function (columnStr, i) {
let inStr = false, c, cn, cl, token = '';
// We should be able to detect dateFormats within 13 rows
if (i > 13) {
return true;
}
for (let j = 0; j < columnStr.length; j++) {
c = columnStr[j];
cn = columnStr[j + 1];
cl = columnStr[j - 1];
if (c === '#') {
// Skip the rest of the line - it's a comment
return;
}
if (c === '"') {
if (inStr) {
if (cl !== '"' && cn !== '"') {
while (cn === ' ' && j < columnStr.length) {
cn = columnStr[++j];
}
// After parsing a string, the next non-blank
// should be a delimiter if the CSV is properly
// formed.
if (typeof potDelimiters[cn] !== 'undefined') {
potDelimiters[cn]++;
}
inStr = false;
}
}
else {
inStr = true;
}
}
else if (typeof potDelimiters[c] !== 'undefined') {
token = token.trim();
if (!isNaN(Date.parse(token))) {
potDelimiters[c]++;
}
else if (isNaN(token) ||
!isFinite(token)) {
potDelimiters[c]++;
}
token = '';
}
else {
token += c;
}
if (c === ',') {
commas++;
}
if (c === '.') {
points++;
}
}
});
// Count the potential delimiters.
// This could be improved by checking if the number of delimiters
// equals the number of columns - 1
if (potDelimiters[';'] > potDelimiters[',']) {
guessed = ';';
}
else if (potDelimiters[','] > potDelimiters[';']) {
guessed = ',';
}
else {
// No good guess could be made..
guessed = ',';
}
// Try to deduce the decimal point if it's not explicitly set.
// If both commas or points is > 0 there is likely an issue
if (!options.decimalPoint) {
if (points > commas) {
options.decimalPoint = '.';
}
else {
options.decimalPoint = ',';
}
// Apply a new decimal regex based on the presumed decimal sep.
self.decimalRegex = new RegExp('^(-?[0-9]+)' +
options.decimalPoint +
'([0-9]+)$');
}
return guessed;
}
/**
* Tries to guess the date format
* - Check if either month candidate exceeds 12
* - Check if year is missing (use current year)
* - Check if a shortened year format is used (e.g. 1/1/99)
* - If no guess can be made, the user must be prompted
* data is the data to deduce a format based on
* @internal
*/
function deduceDateFormat(data, limit) {
const format = 'YYYY/mm/dd', stable = [], max = [];
let thing, guessedFormat = [], calculatedFormat, i = 0, madeDeduction = false, j;
if (!limit || limit > data.length) {
limit = data.length;
}
for (; i < limit; i++) {
if (typeof data[i] !== 'undefined' &&
data[i]?.length) {
thing = data[i]
.trim()
.replace(/\//g, ' ')
.replace(/\-/g, ' ')
.replace(/\./g, ' ')
.split(' ');
guessedFormat = [
'',
'',
''
];
for (j = 0; j < thing.length; j++) {
if (j < guessedFormat.length) {
thing[j] = parseInt(thing[j], 10);
if (thing[j]) {
max[j] = (!max[j] || max[j] < thing[j]) ?
thing[j] :
max[j];
if (typeof stable[j] !== 'undefined') {
if (stable[j] !== thing[j]) {
stable[j] = false;
}
}
else {
stable[j] = thing[j];
}
if (thing[j] > 31) {
if (thing[j] < 100) {
guessedFormat[j] = 'YY';
}
else {
guessedFormat[j] = 'YYYY';
}
}
else if (thing[j] > 12 &&
thing[j] <= 31) {
guessedFormat[j] = 'dd';
madeDeduction = true;
}
else if (!guessedFormat[j].length) {
guessedFormat[j] = 'mm';
}
}
}
}
}
}
if (madeDeduction) {
// This handles a few edge cases with hard to guess dates
for (j = 0; j < stable.length; j++) {
if (stable[j] !== false) {
if (max[j] > 12 &&
guessedFormat[j] !== 'YY' &&
guessedFormat[j] !== 'YYYY') {
guessedFormat[j] = 'YY';
}
}
else if (max[j] > 12 && guessedFormat[j] === 'mm') {
guessedFormat[j] = 'dd';
}
}
// If the middle one is dd, and the last one is dd,
// the last should likely be year.
if (guessedFormat.length === 3 &&
guessedFormat[1] === 'dd' &&
guessedFormat[2] === 'dd') {
guessedFormat[2] = 'YY';
}
calculatedFormat = guessedFormat.join('/');
// If the calculated format is not valid, we need to present an
// error.
if (!self.dateFormats[calculatedFormat]) {
// This should emit an event instead
fireEvent(self, 'deduceDateFailed');
return format;
}
return calculatedFormat;
}
return format;
}
if (csv && options.beforeParse) {
csv = options.beforeParse.call(this, csv, this);
}
if (csv) {
lines = csv
.replace(/\r\n/g, '\n') // Unix
.replace(/\r/g, '\n') // Mac
.split(options.lineDelimiter || '\n');
if (!startRow || startRow < 0) {
startRow = 0;
}
if (!endRow || endRow >= lines.length) {
endRow = lines.length - 1;
}
if (options.itemDelimiter) {
itemDelimiter = options.itemDelimiter;
}
else {
itemDelimiter = guessDelimiter(lines);
}
let offset = 0;
for (rowIt = startRow; rowIt <= endRow; rowIt++) {
if (lines[rowIt][0] === '#') {
offset++;
}
else {
parseRow(lines[rowIt], rowIt - startRow - offset);
}
}
if ((!options.columnTypes || options.columnTypes.length === 0) &&
dataTypes.length &&
dataTypes[0].length &&
dataTypes[0][1] === 'date' &&
!options.dateFormat) {
options.dateFormat = deduceDateFormat(columns[0]);
}
/// lines.forEach(function (line, rowNo) {
// let trimmed = self.trim(line),
// isComment = trimmed.indexOf('#') === 0,
// isBlank = trimmed === '',
// items;
// if (
// rowNo >= startRow &&
// rowNo <= endRow &&
// !isComment && !isBlank
// ) {
// items = line.split(itemDelimiter);
// items.forEach(function (item, colNo) {
// if (colNo >= startColumn && colNo <= endColumn) {
// if (!columns[colNo - startColumn]) {
// columns[colNo - startColumn] = [];
// }
// columns[colNo - startColumn][activeRowNo] = item;
// }
// });
// activeRowNo += 1;
// }
// });
//
this.dataFound();
}
return columns;
}
/**
* Parse a HTML table
*
* @function Highcharts.Data#parseTable
*/
parseTable() {
const options = this.options, columns = this.columns || [], startRow = options.startRow || 0, endRow = options.endRow || Number.MAX_VALUE, startColumn = options.startColumn || 0, endColumn = options.endColumn || Number.MAX_VALUE;
if (options.table) {
let table = options.table;
if (typeof table === 'string') {
table = doc.getElementById(table);
}
[].forEach.call(table.getElementsByTagName('tr'), (tr, rowNo) => {
if (rowNo >= startRow && rowNo <= endRow) {
[].forEach.call(tr.children, (item, colNo) => {
const row = columns[colNo - startColumn];
let i = 1;
if ((item.tagName === 'TD' ||
item.tagName === 'TH') &&
colNo >= startColumn &&
colNo <= endColumn) {
if (!columns[colNo - startColumn]) {
columns[colNo - startColumn] = [];
}
columns[colNo - startColumn][rowNo - startRow] = item.innerHTML;
// Loop over all previous indices and make sure
// they are nulls, not undefined.
while (rowNo - startRow >= i &&
row[rowNo - startRow - i] === void 0) {
row[rowNo - startRow - i] = null;
i++;
}
}
});
}
});
this.dataFound(); // Continue
}
return columns;
}
/**
* Fetch or refetch live data
*
* @function Highcharts.Data#fetchLiveData
*
* @return {boolean}
* The URLs that were tried can be found in the options
*/
fetchLiveData() {
const data = this, chart = this.chart, options = this.options, maxRetries = 3, pollingEnabled = options.enablePolling, originalOptions = merge(options);
let currentRetries = 0, updateIntervalMs = (options.dataRefreshRate || 2) * 1000;
if (!hasURLOption(options)) {
return false;
}
// Do not allow polling more than once a second
updateIntervalMs = Math.max(updateIntervalMs, 1000);
delete options.csvURL;
delete options.rowsURL;
delete options.columnsURL;
/**
* Performs a data fetch with optional polling support. Attempts to load
* data from configured sources in the following order: `csvURL`,
* `rowsURL`, then `columnsURL`. On success, updates the chart with the
* received data.
*
* @param {boolean} initialFetch
* Whether this is the initial fetch. When `true`, clears any existing
* polling timeout and sets the active `liveDataURL` on the chart.
*
* @internal
*/
function performFetch(initialFetch) {
/**
* Helper function for doing the data fetch + polling.
* @internal
*/
function request(url, done, tp) {
if (!url ||
!/^(http|\/|\.\/|\.\.\/)/.test(url)) {
if (url) {
error(`Invalid URL: ${url}`, false, chart);
}
return false;
}
if (initialFetch) {
internalClearTimeout(data.liveDataTimeout);
chart.liveDataURL = url;
}
/**
* Schedules the next fetch if polling is enabled and the URL
* has not changed since the request was initiated.
* @internal
*/
function poll() {
// Poll
if (pollingEnabled && chart.liveDataURL === url) {
// We need to stop doing this if the URL has changed
data.liveDataTimeout =
setTimeout(performFetch, updateIntervalMs);
}
}
ajax({
url: url,
dataType: tp || 'json',
success: function (res) {
if (chart?.series) {
done(res);
}
poll();
},
error: function (xhr, e) {
if (++currentRetries < maxRetries) {
poll();
}
if (!chart.options) {
// If the chart is destroyed, ignore the error as
// a cancelled request.
return;
}
return error(`Request failed - ${xhr.status} \n` +
(typeof e === 'string' ? e : e.message), false, chart);
}
});
return true;
}
if (!request(originalOptions.csvURL, function (res) {
chart.update({ data: { csv: res } });
}, 'text')) {
if (!request(originalOptions.rowsURL, function (res) {
chart.update({
data: {
rows: res
}
});
})) {
request(originalOptions.columnsURL, function (res) {
chart.update({
data: {
columns: res
}
});
});
}
}
}
performFetch(true);
return hasURLOption(options);
}
/**
* Parse a Google spreadsheet.
*
* @function Highcharts.Data#parseGoogleSpreadsheet
*
* @return {boolean}
* Always returns false, because it is an intermediate fetch.
*/
parseGoogleSpreadsheet() {
const data = this, options = this.options, googleSpreadsheetKey = options.googleSpreadsheetKey, chart = this.chart, refreshRate = Math.max((options.dataRefreshRate || 2) * 1000, 4000);
/**
* Form the `values` field after range settings, unless the
* googleSpreadsheetRange option is set.
*/
const getRange = () => {
if (options.googleSpreadsheetRange) {
return options.googleSpreadsheetRange;
}
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const start = (alphabet.charAt(options.startColumn || 0) || 'A') +
((options.startRow || 0) + 1);
let end = alphabet.charAt(options.endColumn ?? -1) || 'ZZ';
if (defined(options.endRow)) {
end += options.endRow + 1;
}
return `${start}:${end}`;
};
/**
* Fetch the actual spreadsheet using XMLHttpRequest.
* @internal
*/
function fetchSheet(fn) {
const url = [
'https://sheets.googleapis.com/v4/spreadsheets',
googleSpreadsheetKey,
'values',
getRange(),
'?alt=json&' +
'majorDimension=COLUMNS&' +
'valueRenderOption=UNFORMATTED_VALUE&' +
'dateTimeRenderOption=FORMATTED_STRING&' +
'key=' + options.googleAPIKey
].join('/');
ajax({
url,
dataType: 'json',
success: function (json) {
fn(json);
if (options.enablePolling) {
data.liveDataTimeout = setTimeout(function () {
fetchSheet(fn);
}, refreshRate);
}
},
error: function (xhr, text) {
if (!chart.options) {
// If the chart is destroyed, ignore the error as
// a cancelled request.
return;
}
return error(`Request failed - ${xhr.status} \n` +
(typeof text === 'string' ? text : text.message), false, chart);
}
});
}
if (googleSpreadsheetKey) {
delete options.googleSpreadsheetKey;
fetchSheet(function (json) {
// Prepare the data from the spreadsheet
const columns = json.values;
if (!columns || columns.length === 0) {
return false;
}
// Find the maximum row count in order to extend shorter columns
const rowCount = columns.reduce((rowCount, column) => Math.max(rowCount, column.length), 0);
// Insert null for empty spreadsheet cells (#5298)
columns.forEach((column) => {
for (let i = 0; i < rowCount; i++) {
if (typeof column[i] === 'undefined') {
column[i] = null;
}
}
});
if (chart?.series) {
chart.update({
data: {
columns: columns
}
});
}
else { // #8245
data.columns = columns;
data.dataFound();
}
});
}
// This is an intermediate fetch, so always return false.
return false;
}
/**
* Trim a string from whitespaces.
*
* @function Highcharts.Data#trim
*
* @param {string} str
* String to trim
*
* @param {boolean} [inside=false]
* Remove all spaces between numbers.
*
* @return {string}
* Trimmed string
*/
trim(str, inside) {
if (typeof str === 'string') {
str = str.replace(/^\s+|\s+$/g, '');
// Clear white space inside the string, like thousands separators
if (inside && /[\d\s]+/.test(str)) {
str = str.replace(/\s/g, '');
}
if (this.decimalRegex) {
str = str.replace(this.decimalRegex, '$1.$2');
}
}
return str;
}
/**
* Parse numeric cells in to number types and date types in to true dates.
*
* @function Highcharts.Data#parseTypes
*/
parseTypes() {
const columns = this.columns || [];
let col = columns.length;
while (col--) {
this.parseColumn(columns[col], col);
}
}
/**
* Parse a single column. Set properties like .isDatetime and .isNumeric.
*
* @function Highcharts.Data#parseColumn
*
* @param {Array<Highcharts.DataValueType>} column
* Column to parse
*
* @param {number} col
* Column index
*/
parseColumn(column, col) {
const rawColumns = this.rawColumns, columns = this.columns = this.columns || [], firstRowAsNames = this.firstRowAsNames, isXColumn = this.valueCount?.xColumns.indexOf(col) !== -1, backup = [], chartOptions = this.chartOptions, columnTypes = this.options.columnTypes || [], columnType = columnTypes[col], forceCategory = (isXColumn &&
(chartOptions?.xAxis &&
splat(chartOptions.xAxis)[0].type === 'category')) || columnType === 'string', columnHasName = defined(column.name);
let row = column.length, val, floatVal, trimVal, trimInsideVal, dateVal, diff, descending;
if (!rawColumns[col]) {
rawColumns[col] = [];
}
while (row--) {
val = backup[row] || column[row];
trimVal = this.trim(val);
trimInsideVal = this.trim(val, true);
floatVal = parseFloat(trimInsideVal);
// Set it the first time
if (typeof rawColumns[col][row] === 'undefined') {
rawColumns[col][row] = trimVal;
}
// Disable number or date parsing by setting the X axis type to
// category
if (forceCategory ||
(row === 0 && firstRowAsNames && !columnHasName)) {
column[row] = '' + trimVal;
}
else if (+trimInsideVal === floatVal) { // Is numeric
column[row] = floatVal;
// If the number is greater than milliseconds in a year, assume
// datetime
if (floatVal > 365 * 24 * 3600 * 1000 &&
columnType !== 'float') {
column.isDatetime = true;
}
else {
column.isNumeric = true;
}
if (typeof column[row + 1] !== 'undefined') {
descending = floatVal > column[row + 1];
}
// String, continue to determine if it is a date string or really a
// string
}
else {
if (trimVal?.length) {
dateVal = this.parseDate(val);
}
// Only allow parsing of dates if this column is an x-column
if (isXColumn && isNumber(dateVal) && columnType !== 'float') {
backup[row] = val;
column[row] = dateVal;
column.isDatetime = true;
// Check if the dates are uniformly descending or ascending.
// If they are not, chances are that they are a different
// time format, so check for alternative.
if (typeof column[row + 1] !== 'undefined') {
diff = dateVal > column[row + 1];
if (diff !== descending &&
typeof descending !== 'undefined' &&
this.alternativeFormat) {
this.dateFormat = this.alternativeFormat;
row = column.length;
this.alternativeFormat =
this.dateFormats[this.dateFormat].alternative;
}
descending = diff;
}
}
else { // String
column[row] = trimVal === '' ? null : trimVal;
if (row !== 0 &&
(column.isDatetime ||
column.isNumeric)) {
column.mixed = true;
}
}
}
}
// If strings are intermixed with numbers or dates in a parsed column,
// it is an indication that parsing went wrong or the data was not
// intended to display as numbers or dates and parsing is too
// aggressive. Fall back to categories. Demonstrated in the
// highcharts/demo/column-drilldown sample.
if (isXColumn && column.mixed) {
columns[col] = rawColumns[col];
}
}
/**
* Parse a date and return it as a number. Overridable through
* `options.parseDate`.
*
* @function Highcharts.Data#parseDate
*/
parseDate(val) {
const parseDate = this.options.parseDate;
let ret, key, format, dateFormat = this.options.dateFormat || this.dateFormat, match;
if (parseDate) {
ret = parseDate(val);
}
else if (parseDate === false) {
ret = val;
}
else if (typeof val === 'string') {
// Auto-detect the date format the first time
if (!dateFormat) {
for (key in this.dateFormats) { // eslint-disable-line guard-for-in
format = this.dateFormats[key];
match = val.match(format.regex);
if (match) {
this.dateFormat = dateFormat = key;
this.alternativeFormat = format.alternative;
ret = format.parser(match);
break;
}
}
// Next time, use the one previously found
}
else {
format = this.dateFormats[dateFormat];
if (!format) {
// The selected format is invalid
format = this.dateFormats['YYYY/mm/dd'];
}
match = val.match(format.regex);
if (match) {
ret = format.parser(match);
}
}
// Fall back to Date.parse
if (!match) {
ret = new Time().parse(val);
}
}
return ret;
}
/**
* Get the parsed data in a form that we can apply directly to the
* `series.data` config. Array positions can be mapped using the
* `series.keys` option.
*
* @example
* const data = Highcharts.data({
* csv: document.getElementById('data').innerHTML
* }).getData();
*
* @function Highcharts.Data#getData
*
* @return {Array<Array<DataValueType>>|undefined} Data rows
*/
getData() {
if (this.columns) {
return this.rowsToColumns(this.columns)?.slice(1);
}
}
/**
* Return a DataTable with the parsed data
*
* @example
* const csv = await fetch(
* 'https://www.example.com/sample-data.csv'
* ).then(result => result.text());
* const dataTable = new Highcharts.Data({ csv }).getDataTable();
*
* @sample highcharts/data/getdatatable
*
* @function Highcharts.Data#getDataTable
*
* @since 13.0.0
* @return {Highcharts.DataTable} DataTable with the parsed data
*/
getDataTable() {
return new DataTableCore({
columns: Object.values(this.columns || [])
.reduce((dtColumns, dtColumn) => {
// To avoid shifting the original column, create a copy
const column = dtColumn.slice(), columnId = column.shift();
if (typeof columnId === 'string' ||
typeof columnId === 'number') {
dtColumns[columnId] = column;
}
return dtColumns;
}, {})
});
}
/**
* A hook for working directly on the parsed columns
*
* @function Highcharts.Data#parsed
*/
parsed() {
if (this.options.parsed) {
return this.options.parsed.call(this, this.columns, this);
}
}
/**
* If a complete callback function is provided in the options, interpret the
* columns into a Highcharts options object.
*
* The function requires that the context has the `valueCount` property set.
*
* @function Highcharts.Data#complete
* @internal
*/
complete() {
const columns = this.columns = this.columns || [], xColumns = [], options = this.options, allSeriesBuilders = [];
let type = 'linear', series, data, i, j, r, seriesIndex, chartOptions, builder, freeIndexes, typeCol, index;
xColumns.length = columns.length;
if (options.complete || options.afterComplete) {
// Get the names and shift the top row
if (this.firstRowAsNames) {
for (i = 0; i < columns.length; i++) {
const curCol = columns[i];
if (!defined(curCol.name)) {
curCol.name = (curCol.shift() ?? '').toString();
}
}
}
// Use the next columns for series
series = [];
freeIndexes = getFreeIndexes(columns?.length || 0, this.valueCount.seriesBuilders);
// Populate defi