data.src.js 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  1. /**
  2. * @license Highcharts JS v8.2.0 (2020-08-20)
  3. *
  4. * Data module
  5. *
  6. * (c) 2012-2019 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/data', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Extensions/Ajax.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * (c) 2010-2017 Christer Vasseng, Torstein Honsi
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var merge = U.merge,
  42. objectEach = U.objectEach;
  43. /**
  44. * @interface Highcharts.AjaxSettingsObject
  45. */ /**
  46. * The payload to send.
  47. *
  48. * @name Highcharts.AjaxSettingsObject#data
  49. * @type {string|Highcharts.Dictionary<any>}
  50. */ /**
  51. * The data type expected.
  52. * @name Highcharts.AjaxSettingsObject#dataType
  53. * @type {"json"|"xml"|"text"|"octet"}
  54. */ /**
  55. * Function to call on error.
  56. * @name Highcharts.AjaxSettingsObject#error
  57. * @type {Function}
  58. */ /**
  59. * The headers; keyed on header name.
  60. * @name Highcharts.AjaxSettingsObject#headers
  61. * @type {Highcharts.Dictionary<string>}
  62. */ /**
  63. * Function to call on success.
  64. * @name Highcharts.AjaxSettingsObject#success
  65. * @type {Function}
  66. */ /**
  67. * The HTTP method to use. For example GET or POST.
  68. * @name Highcharts.AjaxSettingsObject#type
  69. * @type {string}
  70. */ /**
  71. * The URL to call.
  72. * @name Highcharts.AjaxSettingsObject#url
  73. * @type {string}
  74. */
  75. /**
  76. * Perform an Ajax call.
  77. *
  78. * @function Highcharts.ajax
  79. *
  80. * @param {Partial<Highcharts.AjaxSettingsObject>} attr
  81. * The Ajax settings to use.
  82. *
  83. * @return {false|undefined}
  84. * Returns false, if error occured.
  85. */
  86. H.ajax = function (attr) {
  87. var options = merge(true, {
  88. url: false,
  89. type: 'get',
  90. dataType: 'json',
  91. success: false,
  92. error: false,
  93. data: false,
  94. headers: {}
  95. },
  96. attr),
  97. headers = {
  98. json: 'application/json',
  99. xml: 'application/xml',
  100. text: 'text/plain',
  101. octet: 'application/octet-stream'
  102. },
  103. r = new XMLHttpRequest();
  104. /**
  105. * @private
  106. * @param {XMLHttpRequest} xhr - Internal request object.
  107. * @param {string|Error} err - Occured error.
  108. * @return {void}
  109. */
  110. function handleError(xhr, err) {
  111. if (options.error) {
  112. options.error(xhr, err);
  113. }
  114. else {
  115. // @todo Maybe emit a highcharts error event here
  116. }
  117. }
  118. if (!options.url) {
  119. return false;
  120. }
  121. r.open(options.type.toUpperCase(), options.url, true);
  122. if (!options.headers['Content-Type']) {
  123. r.setRequestHeader('Content-Type', headers[options.dataType] || headers.text);
  124. }
  125. objectEach(options.headers, function (val, key) {
  126. r.setRequestHeader(key, val);
  127. });
  128. // @todo lacking timeout handling
  129. r.onreadystatechange = function () {
  130. var res;
  131. if (r.readyState === 4) {
  132. if (r.status === 200) {
  133. res = r.responseText;
  134. if (options.dataType === 'json') {
  135. try {
  136. res = JSON.parse(res);
  137. }
  138. catch (e) {
  139. return handleError(r, e);
  140. }
  141. }
  142. return options.success && options.success(res);
  143. }
  144. handleError(r, r.responseText);
  145. }
  146. };
  147. try {
  148. options.data = JSON.stringify(options.data);
  149. }
  150. catch (e) {
  151. // empty
  152. }
  153. r.send(options.data || true);
  154. };
  155. /**
  156. * Get a JSON resource over XHR, also supporting CORS without preflight.
  157. *
  158. * @function Highcharts.getJSON
  159. * @param {string} url
  160. * The URL to load.
  161. * @param {Function} success
  162. * The success callback. For error handling, use the `Highcharts.ajax`
  163. * function instead.
  164. * @return {void}
  165. */
  166. H.getJSON = function (url, success) {
  167. H.ajax({
  168. url: url,
  169. success: success,
  170. dataType: 'json',
  171. headers: {
  172. // Override the Content-Type to avoid preflight problems with CORS
  173. // in the Highcharts demos
  174. 'Content-Type': 'text/plain'
  175. }
  176. });
  177. };
  178. var exports = {
  179. ajax: H.ajax,
  180. getJSON: H.getJSON
  181. };
  182. return exports;
  183. });
  184. _registerModule(_modules, 'Extensions/Data.js', [_modules['Core/Chart/Chart.js'], _modules['Core/Globals.js'], _modules['Core/Series/Point.js'], _modules['Core/Utilities.js'], _modules['Extensions/Ajax.js']], function (Chart, H, Point, U, Ajax) {
  185. /* *
  186. *
  187. * Data module
  188. *
  189. * (c) 2012-2020 Torstein Honsi
  190. *
  191. * License: www.highcharts.com/license
  192. *
  193. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  194. *
  195. * */
  196. var addEvent = U.addEvent,
  197. defined = U.defined,
  198. extend = U.extend,
  199. fireEvent = U.fireEvent,
  200. isNumber = U.isNumber,
  201. merge = U.merge,
  202. objectEach = U.objectEach,
  203. pick = U.pick,
  204. splat = U.splat;
  205. /**
  206. * Callback function to modify the CSV before parsing it by the data module.
  207. *
  208. * @callback Highcharts.DataBeforeParseCallbackFunction
  209. *
  210. * @param {string} csv
  211. * The CSV to modify.
  212. *
  213. * @return {string}
  214. * The CSV to parse.
  215. */
  216. /**
  217. * Callback function that gets called after parsing data.
  218. *
  219. * @callback Highcharts.DataCompleteCallbackFunction
  220. *
  221. * @param {Highcharts.Options} chartOptions
  222. * The chart options that were used.
  223. */
  224. /**
  225. * Callback function that returns the correspondig Date object to a match.
  226. *
  227. * @callback Highcharts.DataDateFormatCallbackFunction
  228. *
  229. * @param {Array<number>} match
  230. *
  231. * @return {number}
  232. */
  233. /**
  234. * Structure for alternative date formats to parse.
  235. *
  236. * @interface Highcharts.DataDateFormatObject
  237. */ /**
  238. * @name Highcharts.DataDateFormatObject#alternative
  239. * @type {string|undefined}
  240. */ /**
  241. * @name Highcharts.DataDateFormatObject#parser
  242. * @type {Highcharts.DataDateFormatCallbackFunction}
  243. */ /**
  244. * @name Highcharts.DataDateFormatObject#regex
  245. * @type {global.RegExp}
  246. */
  247. /**
  248. * Possible types for a data item in a column or row.
  249. *
  250. * @typedef {number|string|null} Highcharts.DataValueType
  251. */
  252. /**
  253. * Callback function to parse string representations of dates into
  254. * JavaScript timestamps (milliseconds since 1.1.1970).
  255. *
  256. * @callback Highcharts.DataParseDateCallbackFunction
  257. *
  258. * @param {string} dateValue
  259. *
  260. * @return {number}
  261. * Timestamp (milliseconds since 1.1.1970) as integer for Date class.
  262. */
  263. /**
  264. * Callback function to access the parsed columns, the two-dimentional
  265. * input data array directly, before they are interpreted into series
  266. * data and categories.
  267. *
  268. * @callback Highcharts.DataParsedCallbackFunction
  269. *
  270. * @param {Array<Array<*>>} columns
  271. * The parsed columns by the data module.
  272. *
  273. * @return {boolean|undefined}
  274. * Return `false` to stop completion, or call `this.complete()` to
  275. * continue async.
  276. */
  277. var ajax = Ajax.ajax;
  278. // Utilities
  279. var win = H.win,
  280. doc = win.document;
  281. /**
  282. * The Data module provides a simplified interface for adding data to
  283. * a chart from sources like CVS, HTML tables or grid views. See also
  284. * the [tutorial article on the Data module](
  285. * https://www.highcharts.com/docs/working-with-data/data-module).
  286. *
  287. * It requires the `modules/data.js` file to be loaded.
  288. *
  289. * Please note that the default way of adding data in Highcharts, without
  290. * the need of a module, is through the [series._type_.data](#series.line.data)
  291. * option.
  292. *
  293. * @sample {highcharts} highcharts/demo/column-parsed/
  294. * HTML table
  295. * @sample {highcharts} highcharts/data/csv/
  296. * CSV
  297. *
  298. * @since 4.0
  299. * @requires modules/data
  300. * @apioption data
  301. */
  302. /**
  303. * A callback function to modify the CSV before parsing it. Return the modified
  304. * string.
  305. *
  306. * @sample {highcharts} highcharts/demo/line-ajax/
  307. * Modify CSV before parse
  308. *
  309. * @type {Highcharts.DataBeforeParseCallbackFunction}
  310. * @since 6.1
  311. * @apioption data.beforeParse
  312. */
  313. /**
  314. * A two-dimensional array representing the input data on tabular form.
  315. * This input can be used when the data is already parsed, for example
  316. * from a grid view component. Each cell can be a string or number.
  317. * If not switchRowsAndColumns is set, the columns are interpreted as
  318. * series.
  319. *
  320. * @see [data.rows](#data.rows)
  321. *
  322. * @sample {highcharts} highcharts/data/columns/
  323. * Columns
  324. *
  325. * @type {Array<Array<Highcharts.DataValueType>>}
  326. * @since 4.0
  327. * @apioption data.columns
  328. */
  329. /**
  330. * The callback that is evaluated when the data is finished loading,
  331. * optionally from an external source, and parsed. The first argument
  332. * passed is a finished chart options object, containing the series.
  333. * These options can be extended with additional options and passed
  334. * directly to the chart constructor.
  335. *
  336. * @see [data.parsed](#data.parsed)
  337. *
  338. * @sample {highcharts} highcharts/data/complete/
  339. * Modify data on complete
  340. *
  341. * @type {Highcharts.DataCompleteCallbackFunction}
  342. * @since 4.0
  343. * @apioption data.complete
  344. */
  345. /**
  346. * A comma delimited string to be parsed. Related options are [startRow](
  347. * #data.startRow), [endRow](#data.endRow), [startColumn](#data.startColumn)
  348. * and [endColumn](#data.endColumn) to delimit what part of the table
  349. * is used. The [lineDelimiter](#data.lineDelimiter) and [itemDelimiter](
  350. * #data.itemDelimiter) options define the CSV delimiter formats.
  351. *
  352. * The built-in CSV parser doesn't support all flavours of CSV, so in
  353. * some cases it may be necessary to use an external CSV parser. See
  354. * [this example](https://jsfiddle.net/highcharts/u59176h4/) of parsing
  355. * CSV through the MIT licensed [Papa Parse](http://papaparse.com/)
  356. * library.
  357. *
  358. * @sample {highcharts} highcharts/data/csv/
  359. * Data from CSV
  360. *
  361. * @type {string}
  362. * @since 4.0
  363. * @apioption data.csv
  364. */
  365. /**
  366. * Which of the predefined date formats in Date.prototype.dateFormats
  367. * to use to parse date values. Defaults to a best guess based on what
  368. * format gives valid and ordered dates. Valid options include: `YYYY/mm/dd`,
  369. * `dd/mm/YYYY`, `mm/dd/YYYY`, `dd/mm/YY`, `mm/dd/YY`.
  370. *
  371. * @see [data.parseDate](#data.parseDate)
  372. *
  373. * @sample {highcharts} highcharts/data/dateformat-auto/
  374. * Best guess date format
  375. *
  376. * @type {string}
  377. * @since 4.0
  378. * @validvalue ["YYYY/mm/dd", "dd/mm/YYYY", "mm/dd/YYYY", "dd/mm/YYYY",
  379. * "dd/mm/YY", "mm/dd/YY"]
  380. * @apioption data.dateFormat
  381. */
  382. /**
  383. * The decimal point used for parsing numbers in the CSV.
  384. *
  385. * If both this and data.delimiter is set to `undefined`, the parser will
  386. * attempt to deduce the decimal point automatically.
  387. *
  388. * @sample {highcharts} highcharts/data/delimiters/
  389. * Comma as decimal point
  390. *
  391. * @type {string}
  392. * @default .
  393. * @since 4.1.0
  394. * @apioption data.decimalPoint
  395. */
  396. /**
  397. * In tabular input data, the last column (indexed by 0) to use. Defaults
  398. * to the last column containing data.
  399. *
  400. * @sample {highcharts} highcharts/data/start-end/
  401. * Limited data
  402. *
  403. * @type {number}
  404. * @since 4.0
  405. * @apioption data.endColumn
  406. */
  407. /**
  408. * In tabular input data, the last row (indexed by 0) to use. Defaults
  409. * to the last row containing data.
  410. *
  411. * @sample {highcharts} highcharts/data/start-end/
  412. * Limited data
  413. *
  414. * @type {number}
  415. * @since 4.0.4
  416. * @apioption data.endRow
  417. */
  418. /**
  419. * Whether to use the first row in the data set as series names.
  420. *
  421. * @sample {highcharts} highcharts/data/start-end/
  422. * Don't get series names from the CSV
  423. * @sample {highstock} highcharts/data/start-end/
  424. * Don't get series names from the CSV
  425. *
  426. * @type {boolean}
  427. * @default true
  428. * @since 4.1.0
  429. * @product highcharts highstock gantt
  430. * @apioption data.firstRowAsNames
  431. */
  432. /**
  433. * The key for a Google Spreadsheet to load. See [general information
  434. * on GS](https://developers.google.com/gdata/samples/spreadsheet_sample).
  435. *
  436. * @sample {highcharts} highcharts/data/google-spreadsheet/
  437. * Load a Google Spreadsheet
  438. *
  439. * @type {string}
  440. * @since 4.0
  441. * @apioption data.googleSpreadsheetKey
  442. */
  443. /**
  444. * The Google Spreadsheet worksheet to use in combination with
  445. * [googleSpreadsheetKey](#data.googleSpreadsheetKey). The available id's from
  446. * your sheet can be read from `https://spreadsheets.google.com/feeds/worksheets/{key}/public/basic`.
  447. *
  448. * @sample {highcharts} highcharts/data/google-spreadsheet/
  449. * Load a Google Spreadsheet
  450. *
  451. * @type {string}
  452. * @since 4.0
  453. * @apioption data.googleSpreadsheetWorksheet
  454. */
  455. /**
  456. * Item or cell delimiter for parsing CSV. Defaults to the tab character
  457. * `\t` if a tab character is found in the CSV string, if not it defaults
  458. * to `,`.
  459. *
  460. * If this is set to false or undefined, the parser will attempt to deduce
  461. * the delimiter automatically.
  462. *
  463. * @sample {highcharts} highcharts/data/delimiters/
  464. * Delimiters
  465. *
  466. * @type {string}
  467. * @since 4.0
  468. * @apioption data.itemDelimiter
  469. */
  470. /**
  471. * Line delimiter for parsing CSV.
  472. *
  473. * @sample {highcharts} highcharts/data/delimiters/
  474. * Delimiters
  475. *
  476. * @type {string}
  477. * @default \n
  478. * @since 4.0
  479. * @apioption data.lineDelimiter
  480. */
  481. /**
  482. * A callback function to access the parsed columns, the two-dimentional
  483. * input data array directly, before they are interpreted into series
  484. * data and categories. Return `false` to stop completion, or call
  485. * `this.complete()` to continue async.
  486. *
  487. * @see [data.complete](#data.complete)
  488. *
  489. * @sample {highcharts} highcharts/data/parsed/
  490. * Modify data after parse
  491. *
  492. * @type {Highcharts.DataParsedCallbackFunction}
  493. * @since 4.0
  494. * @apioption data.parsed
  495. */
  496. /**
  497. * A callback function to parse string representations of dates into
  498. * JavaScript timestamps. Should return an integer timestamp on success.
  499. *
  500. * @see [dateFormat](#data.dateFormat)
  501. *
  502. * @type {Highcharts.DataParseDateCallbackFunction}
  503. * @since 4.0
  504. * @apioption data.parseDate
  505. */
  506. /**
  507. * The same as the columns input option, but defining rows intead of
  508. * columns.
  509. *
  510. * @see [data.columns](#data.columns)
  511. *
  512. * @sample {highcharts} highcharts/data/rows/
  513. * Data in rows
  514. *
  515. * @type {Array<Array<Highcharts.DataValueType>>}
  516. * @since 4.0
  517. * @apioption data.rows
  518. */
  519. /**
  520. * An array containing dictionaries for each series. A dictionary exists of
  521. * Point property names as the key and the CSV column index as the value.
  522. *
  523. * @sample {highcharts} highcharts/data/seriesmapping-label/
  524. * Label from data set
  525. *
  526. * @type {Array<Highcharts.Dictionary<number>>}
  527. * @since 4.0.4
  528. * @apioption data.seriesMapping
  529. */
  530. /**
  531. * In tabular input data, the first column (indexed by 0) to use.
  532. *
  533. * @sample {highcharts} highcharts/data/start-end/
  534. * Limited data
  535. *
  536. * @type {number}
  537. * @default 0
  538. * @since 4.0
  539. * @apioption data.startColumn
  540. */
  541. /**
  542. * In tabular input data, the first row (indexed by 0) to use.
  543. *
  544. * @sample {highcharts} highcharts/data/start-end/
  545. * Limited data
  546. *
  547. * @type {number}
  548. * @default 0
  549. * @since 4.0
  550. * @apioption data.startRow
  551. */
  552. /**
  553. * Switch rows and columns of the input data, so that `this.columns`
  554. * effectively becomes the rows of the data set, and the rows are interpreted
  555. * as series.
  556. *
  557. * @sample {highcharts} highcharts/data/switchrowsandcolumns/
  558. * Switch rows and columns
  559. *
  560. * @type {boolean}
  561. * @default false
  562. * @since 4.0
  563. * @apioption data.switchRowsAndColumns
  564. */
  565. /**
  566. * An HTML table or the id of such to be parsed as input data. Related
  567. * options are `startRow`, `endRow`, `startColumn` and `endColumn` to
  568. * delimit what part of the table is used.
  569. *
  570. * @sample {highcharts} highcharts/demo/column-parsed/
  571. * Parsed table
  572. *
  573. * @type {string|global.HTMLElement}
  574. * @since 4.0
  575. * @apioption data.table
  576. */
  577. /**
  578. * An URL to a remote CSV dataset. Will be fetched when the chart is created
  579. * using Ajax.
  580. *
  581. * @sample highcharts/data/livedata-columns
  582. * Categorized bar chart with CSV and live polling
  583. * @sample highcharts/data/livedata-csv
  584. * Time based line chart with CSV and live polling
  585. *
  586. * @type {string}
  587. * @apioption data.csvURL
  588. */
  589. /**
  590. * A URL to a remote JSON dataset, structured as a row array.
  591. * Will be fetched when the chart is created using Ajax.
  592. *
  593. * @sample highcharts/data/livedata-rows
  594. * Rows with live polling
  595. *
  596. * @type {string}
  597. * @apioption data.rowsURL
  598. */
  599. /**
  600. * A URL to a remote JSON dataset, structured as a column array.
  601. * Will be fetched when the chart is created using Ajax.
  602. *
  603. * @sample highcharts/data/livedata-columns
  604. * Columns with live polling
  605. *
  606. * @type {string}
  607. * @apioption data.columnsURL
  608. */
  609. /**
  610. * Sets the refresh rate for data polling when importing remote dataset by
  611. * setting [data.csvURL](data.csvURL), [data.rowsURL](data.rowsURL),
  612. * [data.columnsURL](data.columnsURL), or
  613. * [data.googleSpreadsheetKey](data.googleSpreadsheetKey).
  614. *
  615. * Note that polling must be enabled by setting
  616. * [data.enablePolling](data.enablePolling) to true.
  617. *
  618. * The value is the number of seconds between pollings.
  619. * It cannot be set to less than 1 second.
  620. *
  621. * @sample highcharts/demo/live-data
  622. * Live data with user set refresh rate
  623. *
  624. * @default 1
  625. * @type {number}
  626. * @apioption data.dataRefreshRate
  627. */
  628. /**
  629. * Enables automatic refetching of remote datasets every _n_ seconds (defined by
  630. * setting [data.dataRefreshRate](data.dataRefreshRate)).
  631. *
  632. * Only works when either [data.csvURL](data.csvURL),
  633. * [data.rowsURL](data.rowsURL), [data.columnsURL](data.columnsURL), or
  634. * [data.googleSpreadsheetKey](data.googleSpreadsheetKey).
  635. *
  636. * @sample highcharts/demo/live-data
  637. * Live data
  638. * @sample highcharts/data/livedata-columns
  639. * Categorized bar chart with CSV and live polling
  640. *
  641. * @type {boolean}
  642. * @default false
  643. * @apioption data.enablePolling
  644. */
  645. /* eslint-disable valid-jsdoc */
  646. /**
  647. * The Data class
  648. *
  649. * @requires module:modules/data
  650. *
  651. * @class
  652. * @name Highcharts.Data
  653. *
  654. * @param {Highcharts.DataOptions} dataOptions
  655. *
  656. * @param {Highcharts.Options} [chartOptions]
  657. *
  658. * @param {Highcharts.Chart} [chart]
  659. */
  660. var Data = /** @class */ (function () {
  661. function Data(dataOptions, chartOptions, chart) {
  662. this.chart = void 0;
  663. this.chartOptions = void 0;
  664. this.firstRowAsNames = void 0;
  665. this.rawColumns = void 0;
  666. this.options = void 0;
  667. /**
  668. * A collection of available date formats, extendable from the outside to
  669. * support custom date formats.
  670. *
  671. * @name Highcharts.Data#dateFormats
  672. * @type {Highcharts.Dictionary<Highcharts.DataDateFormatObject>}
  673. */
  674. this.dateFormats = {
  675. 'YYYY/mm/dd': {
  676. regex: /^([0-9]{4})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{1,2})$/,
  677. parser: function (match) {
  678. return (match ?
  679. Date.UTC(+match[1], match[2] - 1, +match[3]) :
  680. NaN);
  681. }
  682. },
  683. 'dd/mm/YYYY': {
  684. regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{4})$/,
  685. parser: function (match) {
  686. return (match ?
  687. Date.UTC(+match[3], match[2] - 1, +match[1]) :
  688. NaN);
  689. },
  690. alternative: 'mm/dd/YYYY' // different format with the same regex
  691. },
  692. 'mm/dd/YYYY': {
  693. regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{4})$/,
  694. parser: function (match) {
  695. return (match ?
  696. Date.UTC(+match[3], match[1] - 1, +match[2]) :
  697. NaN);
  698. }
  699. },
  700. 'dd/mm/YY': {
  701. regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{2})$/,
  702. parser: function (match) {
  703. if (!match) {
  704. return NaN;
  705. }
  706. var year = +match[3],
  707. d = new Date();
  708. if (year > (d.getFullYear() - 2000)) {
  709. year += 1900;
  710. }
  711. else {
  712. year += 2000;
  713. }
  714. return Date.UTC(year, match[2] - 1, +match[1]);
  715. },
  716. alternative: 'mm/dd/YY' // different format with the same regex
  717. },
  718. 'mm/dd/YY': {
  719. regex: /^([0-9]{1,2})[\-\/\.]([0-9]{1,2})[\-\/\.]([0-9]{2})$/,
  720. parser: function (match) {
  721. return (match ?
  722. Date.UTC(+match[3] + 2000, match[1] - 1, +match[2]) :
  723. NaN);
  724. }
  725. }
  726. };
  727. this.init(dataOptions, chartOptions, chart);
  728. }
  729. /**
  730. * Initialize the Data object with the given options
  731. *
  732. * @private
  733. * @function Highcharts.Data#init
  734. * @param {Highcharts.DataOptions} options
  735. * @param {Highcharts.Options} [chartOptions]
  736. * @param {Highcharts.Chart} [chart]
  737. */
  738. Data.prototype.init = function (options, chartOptions, chart) {
  739. var decimalPoint = options.decimalPoint,
  740. hasData;
  741. if (chartOptions) {
  742. this.chartOptions = chartOptions;
  743. }
  744. if (chart) {
  745. this.chart = chart;
  746. }
  747. if (decimalPoint !== '.' && decimalPoint !== ',') {
  748. decimalPoint = void 0;
  749. }
  750. this.options = options;
  751. this.columns = (options.columns ||
  752. this.rowsToColumns(options.rows) ||
  753. []);
  754. this.firstRowAsNames = pick(options.firstRowAsNames, this.firstRowAsNames, true);
  755. this.decimalRegex = (decimalPoint &&
  756. new RegExp('^(-?[0-9]+)' + decimalPoint + '([0-9]+)$'));
  757. // This is a two-dimensional array holding the raw, trimmed string
  758. // values with the same organisation as the columns array. It makes it
  759. // possible for example to revert from interpreted timestamps to
  760. // string-based categories.
  761. this.rawColumns = [];
  762. // No need to parse or interpret anything
  763. if (this.columns.length) {
  764. this.dataFound();
  765. hasData = true;
  766. }
  767. if (this.hasURLOption(options)) {
  768. clearTimeout(this.liveDataTimeout);
  769. hasData = false;
  770. }
  771. if (!hasData) {
  772. // Fetch live data
  773. hasData = this.fetchLiveData();
  774. }
  775. if (!hasData) {
  776. // Parse a CSV string if options.csv is given. The parseCSV function
  777. // returns a columns array, if it has no length, we have no data
  778. hasData = Boolean(this.parseCSV().length);
  779. }
  780. if (!hasData) {
  781. // Parse a HTML table if options.table is given
  782. hasData = Boolean(this.parseTable().length);
  783. }
  784. if (!hasData) {
  785. // Parse a Google Spreadsheet
  786. hasData = this.parseGoogleSpreadsheet();
  787. }
  788. if (!hasData && options.afterComplete) {
  789. options.afterComplete();
  790. }
  791. };
  792. Data.prototype.hasURLOption = function (options) {
  793. return Boolean(options &&
  794. (options.rowsURL || options.csvURL || options.columnsURL));
  795. };
  796. /**
  797. * Get the column distribution. For example, a line series takes a single
  798. * column for Y values. A range series takes two columns for low and high
  799. * values respectively, and an OHLC series takes four columns.
  800. *
  801. * @function Highcharts.Data#getColumnDistribution
  802. */
  803. Data.prototype.getColumnDistribution = function () {
  804. var chartOptions = this.chartOptions,
  805. options = this.options,
  806. xColumns = [],
  807. getValueCount = function (type) {
  808. return (H.seriesTypes[type || 'line'].prototype
  809. .pointArrayMap ||
  810. [0]).length;
  811. }, getPointArrayMap = function (type) {
  812. return H.seriesTypes[type || 'line']
  813. .prototype.pointArrayMap;
  814. }, globalType = (chartOptions &&
  815. chartOptions.chart &&
  816. chartOptions.chart.type), individualCounts = [], seriesBuilders = [], seriesIndex = 0,
  817. // If no series mapping is defined, check if the series array is
  818. // defined with types.
  819. seriesMapping = ((options && options.seriesMapping) ||
  820. (chartOptions &&
  821. chartOptions.series &&
  822. chartOptions.series.map(function () {
  823. return { x: 0 };
  824. })) ||
  825. []), i;
  826. ((chartOptions && chartOptions.series) || []).forEach(function (series) {
  827. individualCounts.push(getValueCount(series.type || globalType));
  828. });
  829. // Collect the x-column indexes from seriesMapping
  830. seriesMapping.forEach(function (mapping) {
  831. xColumns.push(mapping.x || 0);
  832. });
  833. // If there are no defined series with x-columns, use the first column
  834. // as x column
  835. if (xColumns.length === 0) {
  836. xColumns.push(0);
  837. }
  838. // Loop all seriesMappings and constructs SeriesBuilders from
  839. // the mapping options.
  840. seriesMapping.forEach(function (mapping) {
  841. var builder = new SeriesBuilder(),
  842. numberOfValueColumnsNeeded = individualCounts[seriesIndex] ||
  843. getValueCount(globalType),
  844. seriesArr = (chartOptions && chartOptions.series) || [],
  845. series = seriesArr[seriesIndex] || {},
  846. defaultPointArrayMap = getPointArrayMap(series.type || globalType),
  847. pointArrayMap = defaultPointArrayMap || ['y'];
  848. if (
  849. // User-defined x.mapping
  850. defined(mapping.x) ||
  851. // All non cartesian don't need 'x'
  852. series.isCartesian ||
  853. // Except pie series:
  854. !defaultPointArrayMap) {
  855. // Add an x reader from the x property or from an undefined
  856. // column if the property is not set. It will then be auto
  857. // populated later.
  858. builder.addColumnReader(mapping.x, 'x');
  859. }
  860. // Add all column mappings
  861. objectEach(mapping, function (val, name) {
  862. if (name !== 'x') {
  863. builder.addColumnReader(val, name);
  864. }
  865. });
  866. // Add missing columns
  867. for (i = 0; i < numberOfValueColumnsNeeded; i++) {
  868. if (!builder.hasReader(pointArrayMap[i])) {
  869. // Create and add a column reader for the next free column
  870. // index
  871. builder.addColumnReader(void 0, pointArrayMap[i]);
  872. }
  873. }
  874. seriesBuilders.push(builder);
  875. seriesIndex++;
  876. });
  877. var globalPointArrayMap = getPointArrayMap(globalType);
  878. if (typeof globalPointArrayMap === 'undefined') {
  879. globalPointArrayMap = ['y'];
  880. }
  881. this.valueCount = {
  882. global: getValueCount(globalType),
  883. xColumns: xColumns,
  884. individual: individualCounts,
  885. seriesBuilders: seriesBuilders,
  886. globalPointArrayMap: globalPointArrayMap
  887. };
  888. };
  889. /**
  890. * When the data is parsed into columns, either by CSV, table, GS or direct
  891. * input, continue with other operations.
  892. *
  893. * @private
  894. * @function Highcharts.Data#dataFound
  895. */
  896. Data.prototype.dataFound = function () {
  897. if (this.options.switchRowsAndColumns) {
  898. this.columns = this.rowsToColumns(this.columns);
  899. }
  900. // Interpret the info about series and columns
  901. this.getColumnDistribution();
  902. // Interpret the values into right types
  903. this.parseTypes();
  904. // Handle columns if a handleColumns callback is given
  905. if (this.parsed() !== false) {
  906. // Complete if a complete callback is given
  907. this.complete();
  908. }
  909. };
  910. /**
  911. * Parse a CSV input string
  912. *
  913. * @function Highcharts.Data#parseCSV
  914. *
  915. * @param {Highcharts.DataOptions} [inOptions]
  916. *
  917. * @return {Array<Array<Highcharts.DataValueType>>}
  918. */
  919. Data.prototype.parseCSV = function (inOptions) {
  920. var self = this, options = inOptions || this.options, csv = options.csv, columns, startRow = (typeof options.startRow !== 'undefined' && options.startRow ?
  921. options.startRow :
  922. 0), endRow = options.endRow || Number.MAX_VALUE, startColumn = (typeof options.startColumn !== 'undefined' &&
  923. options.startColumn) ? options.startColumn : 0, endColumn = options.endColumn || Number.MAX_VALUE, itemDelimiter, lines, rowIt = 0,
  924. // activeRowNo = 0,
  925. dataTypes = [],
  926. // We count potential delimiters in the prepass, and use the
  927. // result as the basis of half-intelligent guesses.
  928. potDelimiters = {
  929. ',': 0,
  930. ';': 0,
  931. '\t': 0
  932. };
  933. columns = this.columns = [];
  934. /*
  935. This implementation is quite verbose. It will be shortened once
  936. it's stable and passes all the test.
  937. It's also not written with speed in mind, instead everything is
  938. very seggregated, and there a several redundant loops.
  939. This is to make it easier to stabilize the code initially.
  940. We do a pre-pass on the first 4 rows to make some intelligent
  941. guesses on the set. Guessed delimiters are in this pass counted.
  942. Auto detecting delimiters
  943. - If we meet a quoted string, the next symbol afterwards
  944. (that's not \s, \t) is the delimiter
  945. - If we meet a date, the next symbol afterwards is the delimiter
  946. Date formats
  947. - If we meet a column with date formats, check all of them to
  948. see if one of the potential months crossing 12. If it does,
  949. we now know the format
  950. It would make things easier to guess the delimiter before
  951. doing the actual parsing.
  952. General rules:
  953. - Quoting is allowed, e.g: "Col 1",123,321
  954. - Quoting is optional, e.g.: Col1,123,321
  955. - Doubble quoting is escaping, e.g. "Col ""Hello world""",123
  956. - Spaces are considered part of the data: Col1 ,123
  957. - New line is always the row delimiter
  958. - Potential column delimiters are , ; \t
  959. - First row may optionally contain headers
  960. - The last row may or may not have a row delimiter
  961. - Comments are optionally supported, in which case the comment
  962. must start at the first column, and the rest of the line will
  963. be ignored
  964. */
  965. /**
  966. * Parse a single row.
  967. * @private
  968. */
  969. function parseRow(columnStr, rowNumber, noAdd, callbacks) {
  970. var i = 0, c = '', cl = '', cn = '', token = '', actualColumn = 0, column = 0;
  971. /**
  972. * @private
  973. */
  974. function read(j) {
  975. c = columnStr[j];
  976. cl = columnStr[j - 1];
  977. cn = columnStr[j + 1];
  978. }
  979. /**
  980. * @private
  981. */
  982. function pushType(type) {
  983. if (dataTypes.length < column + 1) {
  984. dataTypes.push([type]);
  985. }
  986. if (dataTypes[column][dataTypes[column].length - 1] !== type) {
  987. dataTypes[column].push(type);
  988. }
  989. }
  990. /**
  991. * @private
  992. */
  993. function push() {
  994. if (startColumn > actualColumn || actualColumn > endColumn) {
  995. // Skip this column, but increment the column count (#7272)
  996. ++actualColumn;
  997. token = '';
  998. return;
  999. }
  1000. if (!isNaN(parseFloat(token)) && isFinite(token)) {
  1001. token = parseFloat(token);
  1002. pushType('number');
  1003. }
  1004. else if (!isNaN(Date.parse(token))) {
  1005. token = token.replace(/\//g, '-');
  1006. pushType('date');
  1007. }
  1008. else {
  1009. pushType('string');
  1010. }
  1011. if (columns.length < column + 1) {
  1012. columns.push([]);
  1013. }
  1014. if (!noAdd) {
  1015. // Don't push - if there's a varrying amount of columns
  1016. // for each row, pushing will skew everything down n slots
  1017. columns[column][rowNumber] = token;
  1018. }
  1019. token = '';
  1020. ++column;
  1021. ++actualColumn;
  1022. }
  1023. if (!columnStr.trim().length) {
  1024. return;
  1025. }
  1026. if (columnStr.trim()[0] === '#') {
  1027. return;
  1028. }
  1029. for (; i < columnStr.length; i++) {
  1030. read(i);
  1031. // Quoted string
  1032. if (c === '#') {
  1033. // The rest of the row is a comment
  1034. push();
  1035. return;
  1036. }
  1037. if (c === '"') {
  1038. read(++i);
  1039. while (i < columnStr.length) {
  1040. if (c === '"' && cl !== '"' && cn !== '"') {
  1041. break;
  1042. }
  1043. if (c !== '"' || (c === '"' && cl !== '"')) {
  1044. token += c;
  1045. }
  1046. read(++i);
  1047. }
  1048. // Perform "plugin" handling
  1049. }
  1050. else if (callbacks && callbacks[c]) {
  1051. if (callbacks[c](c, token)) {
  1052. push();
  1053. }
  1054. // Delimiter - push current token
  1055. }
  1056. else if (c === itemDelimiter) {
  1057. push();
  1058. // Actual column data
  1059. }
  1060. else {
  1061. token += c;
  1062. }
  1063. }
  1064. push();
  1065. }
  1066. /**
  1067. * Attempt to guess the delimiter. We do a separate parse pass here
  1068. * because we need to count potential delimiters softly without making
  1069. * any assumptions.
  1070. * @private
  1071. */
  1072. function guessDelimiter(lines) {
  1073. var points = 0,
  1074. commas = 0,
  1075. guessed = false;
  1076. lines.some(function (columnStr, i) {
  1077. var inStr = false,
  1078. c,
  1079. cn,
  1080. cl,
  1081. token = '';
  1082. // We should be able to detect dateformats within 13 rows
  1083. if (i > 13) {
  1084. return true;
  1085. }
  1086. for (var j = 0; j < columnStr.length; j++) {
  1087. c = columnStr[j];
  1088. cn = columnStr[j + 1];
  1089. cl = columnStr[j - 1];
  1090. if (c === '#') {
  1091. // Skip the rest of the line - it's a comment
  1092. return;
  1093. }
  1094. if (c === '"') {
  1095. if (inStr) {
  1096. if (cl !== '"' && cn !== '"') {
  1097. while (cn === ' ' && j < columnStr.length) {
  1098. cn = columnStr[++j];
  1099. }
  1100. // After parsing a string, the next non-blank
  1101. // should be a delimiter if the CSV is properly
  1102. // formed.
  1103. if (typeof potDelimiters[cn] !== 'undefined') {
  1104. potDelimiters[cn]++;
  1105. }
  1106. inStr = false;
  1107. }
  1108. }
  1109. else {
  1110. inStr = true;
  1111. }
  1112. }
  1113. else if (typeof potDelimiters[c] !== 'undefined') {
  1114. token = token.trim();
  1115. if (!isNaN(Date.parse(token))) {
  1116. potDelimiters[c]++;
  1117. }
  1118. else if (isNaN(token) ||
  1119. !isFinite(token)) {
  1120. potDelimiters[c]++;
  1121. }
  1122. token = '';
  1123. }
  1124. else {
  1125. token += c;
  1126. }
  1127. if (c === ',') {
  1128. commas++;
  1129. }
  1130. if (c === '.') {
  1131. points++;
  1132. }
  1133. }
  1134. });
  1135. // Count the potential delimiters.
  1136. // This could be improved by checking if the number of delimiters
  1137. // equals the number of columns - 1
  1138. if (potDelimiters[';'] > potDelimiters[',']) {
  1139. guessed = ';';
  1140. }
  1141. else if (potDelimiters[','] > potDelimiters[';']) {
  1142. guessed = ',';
  1143. }
  1144. else {
  1145. // No good guess could be made..
  1146. guessed = ',';
  1147. }
  1148. // Try to deduce the decimal point if it's not explicitly set.
  1149. // If both commas or points is > 0 there is likely an issue
  1150. if (!options.decimalPoint) {
  1151. if (points > commas) {
  1152. options.decimalPoint = '.';
  1153. }
  1154. else {
  1155. options.decimalPoint = ',';
  1156. }
  1157. // Apply a new decimal regex based on the presumed decimal sep.
  1158. self.decimalRegex = new RegExp('^(-?[0-9]+)' +
  1159. options.decimalPoint +
  1160. '([0-9]+)$');
  1161. }
  1162. return guessed;
  1163. }
  1164. /**
  1165. * Tries to guess the date format
  1166. * - Check if either month candidate exceeds 12
  1167. * - Check if year is missing (use current year)
  1168. * - Check if a shortened year format is used (e.g. 1/1/99)
  1169. * - If no guess can be made, the user must be prompted
  1170. * data is the data to deduce a format based on
  1171. * @private
  1172. */
  1173. function deduceDateFormat(data, limit) {
  1174. var format = 'YYYY/mm/dd',
  1175. thing,
  1176. guessedFormat = [],
  1177. calculatedFormat,
  1178. i = 0,
  1179. madeDeduction = false,
  1180. // candidates = {},
  1181. stable = [],
  1182. max = [],
  1183. j;
  1184. if (!limit || limit > data.length) {
  1185. limit = data.length;
  1186. }
  1187. for (; i < limit; i++) {
  1188. if (typeof data[i] !== 'undefined' &&
  1189. data[i] && data[i].length) {
  1190. thing = data[i]
  1191. .trim()
  1192. .replace(/\//g, ' ')
  1193. .replace(/\-/g, ' ')
  1194. .replace(/\./g, ' ')
  1195. .split(' ');
  1196. guessedFormat = [
  1197. '',
  1198. '',
  1199. ''
  1200. ];
  1201. for (j = 0; j < thing.length; j++) {
  1202. if (j < guessedFormat.length) {
  1203. thing[j] = parseInt(thing[j], 10);
  1204. if (thing[j]) {
  1205. max[j] = (!max[j] || max[j] < thing[j]) ?
  1206. thing[j] :
  1207. max[j];
  1208. if (typeof stable[j] !== 'undefined') {
  1209. if (stable[j] !== thing[j]) {
  1210. stable[j] = false;
  1211. }
  1212. }
  1213. else {
  1214. stable[j] = thing[j];
  1215. }
  1216. if (thing[j] > 31) {
  1217. if (thing[j] < 100) {
  1218. guessedFormat[j] = 'YY';
  1219. }
  1220. else {
  1221. guessedFormat[j] = 'YYYY';
  1222. }
  1223. // madeDeduction = true;
  1224. }
  1225. else if (thing[j] > 12 &&
  1226. thing[j] <= 31) {
  1227. guessedFormat[j] = 'dd';
  1228. madeDeduction = true;
  1229. }
  1230. else if (!guessedFormat[j].length) {
  1231. guessedFormat[j] = 'mm';
  1232. }
  1233. }
  1234. }
  1235. }
  1236. }
  1237. }
  1238. if (madeDeduction) {
  1239. // This handles a few edge cases with hard to guess dates
  1240. for (j = 0; j < stable.length; j++) {
  1241. if (stable[j] !== false) {
  1242. if (max[j] > 12 &&
  1243. guessedFormat[j] !== 'YY' &&
  1244. guessedFormat[j] !== 'YYYY') {
  1245. guessedFormat[j] = 'YY';
  1246. }
  1247. }
  1248. else if (max[j] > 12 && guessedFormat[j] === 'mm') {
  1249. guessedFormat[j] = 'dd';
  1250. }
  1251. }
  1252. // If the middle one is dd, and the last one is dd,
  1253. // the last should likely be year.
  1254. if (guessedFormat.length === 3 &&
  1255. guessedFormat[1] === 'dd' &&
  1256. guessedFormat[2] === 'dd') {
  1257. guessedFormat[2] = 'YY';
  1258. }
  1259. calculatedFormat = guessedFormat.join('/');
  1260. // If the caculated format is not valid, we need to present an
  1261. // error.
  1262. if (!(options.dateFormats || self.dateFormats)[calculatedFormat]) {
  1263. // This should emit an event instead
  1264. fireEvent('deduceDateFailed');
  1265. return format;
  1266. }
  1267. return calculatedFormat;
  1268. }
  1269. return format;
  1270. }
  1271. /**
  1272. * @todo
  1273. * Figure out the best axis types for the data
  1274. * - If the first column is a number, we're good
  1275. * - If the first column is a date, set to date/time
  1276. * - If the first column is a string, set to categories
  1277. * @private
  1278. */
  1279. function deduceAxisTypes() {
  1280. }
  1281. if (csv && options.beforeParse) {
  1282. csv = options.beforeParse.call(this, csv);
  1283. }
  1284. if (csv) {
  1285. lines = csv
  1286. .replace(/\r\n/g, '\n') // Unix
  1287. .replace(/\r/g, '\n') // Mac
  1288. .split(options.lineDelimiter || '\n');
  1289. if (!startRow || startRow < 0) {
  1290. startRow = 0;
  1291. }
  1292. if (!endRow || endRow >= lines.length) {
  1293. endRow = lines.length - 1;
  1294. }
  1295. if (options.itemDelimiter) {
  1296. itemDelimiter = options.itemDelimiter;
  1297. }
  1298. else {
  1299. itemDelimiter = null;
  1300. itemDelimiter = guessDelimiter(lines);
  1301. }
  1302. var offset = 0;
  1303. for (rowIt = startRow; rowIt <= endRow; rowIt++) {
  1304. if (lines[rowIt][0] === '#') {
  1305. offset++;
  1306. }
  1307. else {
  1308. parseRow(lines[rowIt], rowIt - startRow - offset);
  1309. }
  1310. }
  1311. // //Make sure that there's header columns for everything
  1312. // columns.forEach(function (col) {
  1313. // });
  1314. deduceAxisTypes();
  1315. if ((!options.columnTypes || options.columnTypes.length === 0) &&
  1316. dataTypes.length &&
  1317. dataTypes[0].length &&
  1318. dataTypes[0][1] === 'date' &&
  1319. !options.dateFormat) {
  1320. options.dateFormat = deduceDateFormat(columns[0]);
  1321. }
  1322. // lines.forEach(function (line, rowNo) {
  1323. // var trimmed = self.trim(line),
  1324. // isComment = trimmed.indexOf('#') === 0,
  1325. // isBlank = trimmed === '',
  1326. // items;
  1327. // if (
  1328. // rowNo >= startRow &&
  1329. // rowNo <= endRow &&
  1330. // !isComment && !isBlank
  1331. // ) {
  1332. // items = line.split(itemDelimiter);
  1333. // items.forEach(function (item, colNo) {
  1334. // if (colNo >= startColumn && colNo <= endColumn) {
  1335. // if (!columns[colNo - startColumn]) {
  1336. // columns[colNo - startColumn] = [];
  1337. // }
  1338. // columns[colNo - startColumn][activeRowNo] = item;
  1339. // }
  1340. // });
  1341. // activeRowNo += 1;
  1342. // }
  1343. // });
  1344. //
  1345. this.dataFound();
  1346. }
  1347. return columns;
  1348. };
  1349. /**
  1350. * Parse a HTML table
  1351. *
  1352. * @function Highcharts.Data#parseTable
  1353. *
  1354. * @return {Array<Array<Highcharts.DataValueType>>}
  1355. */
  1356. Data.prototype.parseTable = function () {
  1357. var options = this.options,
  1358. table = options.table,
  1359. columns = this.columns || [],
  1360. startRow = options.startRow || 0,
  1361. endRow = options.endRow || Number.MAX_VALUE,
  1362. startColumn = options.startColumn || 0,
  1363. endColumn = options.endColumn || Number.MAX_VALUE;
  1364. if (table) {
  1365. if (typeof table === 'string') {
  1366. table = doc.getElementById(table);
  1367. }
  1368. [].forEach.call(table.getElementsByTagName('tr'), function (tr, rowNo) {
  1369. if (rowNo >= startRow && rowNo <= endRow) {
  1370. [].forEach.call(tr.children, function (item, colNo) {
  1371. var row = columns[colNo - startColumn];
  1372. var i = 1;
  1373. if ((item.tagName === 'TD' ||
  1374. item.tagName === 'TH') &&
  1375. colNo >= startColumn &&
  1376. colNo <= endColumn) {
  1377. if (!columns[colNo - startColumn]) {
  1378. columns[colNo - startColumn] = [];
  1379. }
  1380. columns[colNo - startColumn][rowNo - startRow] = item.innerHTML;
  1381. // Loop over all previous indices and make sure
  1382. // they are nulls, not undefined.
  1383. while (rowNo - startRow >= i &&
  1384. row[rowNo - startRow - i] === void 0) {
  1385. row[rowNo - startRow - i] = null;
  1386. i++;
  1387. }
  1388. }
  1389. });
  1390. }
  1391. });
  1392. this.dataFound(); // continue
  1393. }
  1394. return columns;
  1395. };
  1396. /**
  1397. * Fetch or refetch live data
  1398. *
  1399. * @function Highcharts.Data#fetchLiveData
  1400. *
  1401. * @return {boolean}
  1402. * The URLs that were tried can be found in the options
  1403. */
  1404. Data.prototype.fetchLiveData = function () {
  1405. var data = this,
  1406. chart = this.chart,
  1407. options = this.options,
  1408. maxRetries = 3,
  1409. currentRetries = 0,
  1410. pollingEnabled = options.enablePolling,
  1411. updateIntervalMs = (options.dataRefreshRate || 2) * 1000,
  1412. originalOptions = merge(options);
  1413. if (!this.hasURLOption(options)) {
  1414. return false;
  1415. }
  1416. // Do not allow polling more than once a second
  1417. if (updateIntervalMs < 1000) {
  1418. updateIntervalMs = 1000;
  1419. }
  1420. delete options.csvURL;
  1421. delete options.rowsURL;
  1422. delete options.columnsURL;
  1423. /**
  1424. * @private
  1425. */
  1426. function performFetch(initialFetch) {
  1427. /**
  1428. * Helper function for doing the data fetch + polling.
  1429. * @private
  1430. */
  1431. function request(url, done, tp) {
  1432. if (!url || url.indexOf('http') !== 0) {
  1433. if (url && options.error) {
  1434. options.error('Invalid URL');
  1435. }
  1436. return false;
  1437. }
  1438. if (initialFetch) {
  1439. clearTimeout(data.liveDataTimeout);
  1440. chart.liveDataURL = url;
  1441. }
  1442. /**
  1443. * @private
  1444. */
  1445. function poll() {
  1446. // Poll
  1447. if (pollingEnabled && chart.liveDataURL === url) {
  1448. // We need to stop doing this if the URL has changed
  1449. data.liveDataTimeout =
  1450. setTimeout(performFetch, updateIntervalMs);
  1451. }
  1452. }
  1453. ajax({
  1454. url: url,
  1455. dataType: tp || 'json',
  1456. success: function (res) {
  1457. if (chart && chart.series) {
  1458. done(res);
  1459. }
  1460. poll();
  1461. },
  1462. error: function (xhr, text) {
  1463. if (++currentRetries < maxRetries) {
  1464. poll();
  1465. }
  1466. return options.error && options.error(text, xhr);
  1467. }
  1468. });
  1469. return true;
  1470. }
  1471. if (!request(originalOptions.csvURL, function (res) {
  1472. chart.update({
  1473. data: {
  1474. csv: res
  1475. }
  1476. });
  1477. }, 'text')) {
  1478. if (!request(originalOptions.rowsURL, function (res) {
  1479. chart.update({
  1480. data: {
  1481. rows: res
  1482. }
  1483. });
  1484. })) {
  1485. request(originalOptions.columnsURL, function (res) {
  1486. chart.update({
  1487. data: {
  1488. columns: res
  1489. }
  1490. });
  1491. });
  1492. }
  1493. }
  1494. }
  1495. performFetch(true);
  1496. return this.hasURLOption(options);
  1497. };
  1498. /**
  1499. * Parse a Google spreadsheet.
  1500. *
  1501. * @function Highcharts.Data#parseGoogleSpreadsheet
  1502. *
  1503. * @return {boolean}
  1504. * Always returns false, because it is an intermediate fetch.
  1505. */
  1506. Data.prototype.parseGoogleSpreadsheet = function () {
  1507. var data = this,
  1508. options = this.options,
  1509. googleSpreadsheetKey = options.googleSpreadsheetKey,
  1510. chart = this.chart,
  1511. // use sheet 1 as the default rather than od6
  1512. // as the latter sometimes cause issues (it looks like it can
  1513. // be renamed in some cases, ref. a fogbugz case).
  1514. worksheet = options.googleSpreadsheetWorksheet || 1,
  1515. startRow = options.startRow || 0,
  1516. endRow = options.endRow || Number.MAX_VALUE,
  1517. startColumn = options.startColumn || 0,
  1518. endColumn = options.endColumn || Number.MAX_VALUE,
  1519. refreshRate = (options.dataRefreshRate || 2) * 1000;
  1520. if (refreshRate < 4000) {
  1521. refreshRate = 4000;
  1522. }
  1523. /**
  1524. * Fetch the actual spreadsheet using XMLHttpRequest.
  1525. * @private
  1526. */
  1527. function fetchSheet(fn) {
  1528. var url = [
  1529. 'https://spreadsheets.google.com/feeds/cells',
  1530. googleSpreadsheetKey,
  1531. worksheet,
  1532. 'public/values?alt=json'
  1533. ].join('/');
  1534. ajax({
  1535. url: url,
  1536. dataType: 'json',
  1537. success: function (json) {
  1538. fn(json);
  1539. if (options.enablePolling) {
  1540. setTimeout(function () {
  1541. fetchSheet(fn);
  1542. }, (options.dataRefreshRate || 2) * 1000);
  1543. }
  1544. },
  1545. error: function (xhr, text) {
  1546. return options.error && options.error(text, xhr);
  1547. }
  1548. });
  1549. }
  1550. if (googleSpreadsheetKey) {
  1551. delete options.googleSpreadsheetKey;
  1552. fetchSheet(function (json) {
  1553. // Prepare the data from the spreadsheat
  1554. var columns = [],
  1555. cells = json.feed.entry,
  1556. cell,
  1557. cellCount = (cells || []).length,
  1558. colCount = 0,
  1559. rowCount = 0,
  1560. val,
  1561. gr,
  1562. gc,
  1563. cellInner,
  1564. i;
  1565. if (!cells || cells.length === 0) {
  1566. return false;
  1567. }
  1568. // First, find the total number of columns and rows that
  1569. // are actually filled with data
  1570. for (i = 0; i < cellCount; i++) {
  1571. cell = cells[i];
  1572. colCount = Math.max(colCount, cell.gs$cell.col);
  1573. rowCount = Math.max(rowCount, cell.gs$cell.row);
  1574. }
  1575. // Set up arrays containing the column data
  1576. for (i = 0; i < colCount; i++) {
  1577. if (i >= startColumn && i <= endColumn) {
  1578. // Create new columns with the length of either
  1579. // end-start or rowCount
  1580. columns[i - startColumn] = [];
  1581. }
  1582. }
  1583. // Loop over the cells and assign the value to the right
  1584. // place in the column arrays
  1585. for (i = 0; i < cellCount; i++) {
  1586. cell = cells[i];
  1587. gr = cell.gs$cell.row - 1; // rows start at 1
  1588. gc = cell.gs$cell.col - 1; // columns start at 1
  1589. // If both row and col falls inside start and end set the
  1590. // transposed cell value in the newly created columns
  1591. if (gc >= startColumn && gc <= endColumn &&
  1592. gr >= startRow && gr <= endRow) {
  1593. cellInner = cell.gs$cell || cell.content;
  1594. val = null;
  1595. if (cellInner.numericValue) {
  1596. if (cellInner.$t.indexOf('/') >= 0 ||
  1597. cellInner.$t.indexOf('-') >= 0) {
  1598. // This is a date - for future reference.
  1599. val = cellInner.$t;
  1600. }
  1601. else if (cellInner.$t.indexOf('%') > 0) {
  1602. // Percentage
  1603. val = parseFloat(cellInner.numericValue) * 100;
  1604. }
  1605. else {
  1606. val = parseFloat(cellInner.numericValue);
  1607. }
  1608. }
  1609. else if (cellInner.$t && cellInner.$t.length) {
  1610. val = cellInner.$t;
  1611. }
  1612. columns[gc - startColumn][gr - startRow] = val;
  1613. }
  1614. }
  1615. // Insert null for empty spreadsheet cells (#5298)
  1616. columns.forEach(function (column) {
  1617. for (i = 0; i < column.length; i++) {
  1618. if (typeof column[i] === 'undefined') {
  1619. column[i] = null;
  1620. }
  1621. }
  1622. });
  1623. if (chart && chart.series) {
  1624. chart.update({
  1625. data: {
  1626. columns: columns
  1627. }
  1628. });
  1629. }
  1630. else { // #8245
  1631. data.columns = columns;
  1632. data.dataFound();
  1633. }
  1634. });
  1635. }
  1636. // This is an intermediate fetch, so always return false.
  1637. return false;
  1638. };
  1639. /**
  1640. * Trim a string from whitespaces.
  1641. *
  1642. * @function Highcharts.Data#trim
  1643. *
  1644. * @param {string} str
  1645. * String to trim
  1646. *
  1647. * @param {boolean} [inside=false]
  1648. * Remove all spaces between numbers.
  1649. *
  1650. * @return {string}
  1651. * Trimed string
  1652. */
  1653. Data.prototype.trim = function (str, inside) {
  1654. if (typeof str === 'string') {
  1655. str = str.replace(/^\s+|\s+$/g, '');
  1656. // Clear white space insdie the string, like thousands separators
  1657. if (inside && /^[0-9\s]+$/.test(str)) {
  1658. str = str.replace(/\s/g, '');
  1659. }
  1660. if (this.decimalRegex) {
  1661. str = str.replace(this.decimalRegex, '$1.$2');
  1662. }
  1663. }
  1664. return str;
  1665. };
  1666. /**
  1667. * Parse numeric cells in to number types and date types in to true dates.
  1668. *
  1669. * @function Highcharts.Data#parseTypes
  1670. */
  1671. Data.prototype.parseTypes = function () {
  1672. var columns = this.columns,
  1673. col = columns.length;
  1674. while (col--) {
  1675. this.parseColumn(columns[col], col);
  1676. }
  1677. };
  1678. /**
  1679. * Parse a single column. Set properties like .isDatetime and .isNumeric.
  1680. *
  1681. * @function Highcharts.Data#parseColumn
  1682. *
  1683. * @param {Array<Highcharts.DataValueType>} column
  1684. * Column to parse
  1685. *
  1686. * @param {number} col
  1687. * Column index
  1688. */
  1689. Data.prototype.parseColumn = function (column, col) {
  1690. var rawColumns = this.rawColumns,
  1691. columns = this.columns,
  1692. row = column.length,
  1693. val,
  1694. floatVal,
  1695. trimVal,
  1696. trimInsideVal,
  1697. firstRowAsNames = this.firstRowAsNames,
  1698. isXColumn = this.valueCount.xColumns.indexOf(col) !== -1,
  1699. dateVal,
  1700. backup = [],
  1701. diff,
  1702. chartOptions = this.chartOptions,
  1703. descending,
  1704. columnTypes = this.options.columnTypes || [],
  1705. columnType = columnTypes[col],
  1706. forceCategory = isXColumn && ((chartOptions &&
  1707. chartOptions.xAxis &&
  1708. splat(chartOptions.xAxis)[0].type === 'category') || columnType === 'string');
  1709. if (!rawColumns[col]) {
  1710. rawColumns[col] = [];
  1711. }
  1712. while (row--) {
  1713. val = backup[row] || column[row];
  1714. trimVal = this.trim(val);
  1715. trimInsideVal = this.trim(val, true);
  1716. floatVal = parseFloat(trimInsideVal);
  1717. // Set it the first time
  1718. if (typeof rawColumns[col][row] === 'undefined') {
  1719. rawColumns[col][row] = trimVal;
  1720. }
  1721. // Disable number or date parsing by setting the X axis type to
  1722. // category
  1723. if (forceCategory || (row === 0 && firstRowAsNames)) {
  1724. column[row] = '' + trimVal;
  1725. }
  1726. else if (+trimInsideVal === floatVal) { // is numeric
  1727. column[row] = floatVal;
  1728. // If the number is greater than milliseconds in a year, assume
  1729. // datetime
  1730. if (floatVal > 365 * 24 * 3600 * 1000 &&
  1731. columnType !== 'float') {
  1732. column.isDatetime = true;
  1733. }
  1734. else {
  1735. column.isNumeric = true;
  1736. }
  1737. if (typeof column[row + 1] !== 'undefined') {
  1738. descending = floatVal > column[row + 1];
  1739. }
  1740. // String, continue to determine if it is a date string or really a
  1741. // string
  1742. }
  1743. else {
  1744. if (trimVal && trimVal.length) {
  1745. dateVal = this.parseDate(val);
  1746. }
  1747. // Only allow parsing of dates if this column is an x-column
  1748. if (isXColumn && isNumber(dateVal) && columnType !== 'float') {
  1749. backup[row] = val;
  1750. column[row] = dateVal;
  1751. column.isDatetime = true;
  1752. // Check if the dates are uniformly descending or ascending.
  1753. // If they are not, chances are that they are a different
  1754. // time format, so check for alternative.
  1755. if (typeof column[row + 1] !== 'undefined') {
  1756. diff = dateVal > column[row + 1];
  1757. if (diff !== descending &&
  1758. typeof descending !== 'undefined') {
  1759. if (this.alternativeFormat) {
  1760. this.dateFormat = this.alternativeFormat;
  1761. row = column.length;
  1762. this.alternativeFormat =
  1763. this.dateFormats[this.dateFormat]
  1764. .alternative;
  1765. }
  1766. else {
  1767. column.unsorted = true;
  1768. }
  1769. }
  1770. descending = diff;
  1771. }
  1772. }
  1773. else { // string
  1774. column[row] = trimVal === '' ? null : trimVal;
  1775. if (row !== 0 &&
  1776. (column.isDatetime ||
  1777. column.isNumeric)) {
  1778. column.mixed = true;
  1779. }
  1780. }
  1781. }
  1782. }
  1783. // If strings are intermixed with numbers or dates in a parsed column,
  1784. // it is an indication that parsing went wrong or the data was not
  1785. // intended to display as numbers or dates and parsing is too
  1786. // aggressive. Fall back to categories. Demonstrated in the
  1787. // highcharts/demo/column-drilldown sample.
  1788. if (isXColumn && column.mixed) {
  1789. columns[col] = rawColumns[col];
  1790. }
  1791. // If the 0 column is date or number and descending, reverse all
  1792. // columns.
  1793. if (isXColumn && descending && this.options.sort) {
  1794. for (col = 0; col < columns.length; col++) {
  1795. columns[col].reverse();
  1796. if (firstRowAsNames) {
  1797. columns[col].unshift(columns[col].pop());
  1798. }
  1799. }
  1800. }
  1801. };
  1802. /**
  1803. * Parse a date and return it as a number. Overridable through
  1804. * `options.parseDate`.
  1805. *
  1806. * @function Highcharts.Data#parseDate
  1807. *
  1808. * @param {string} val
  1809. *
  1810. * @return {number}
  1811. */
  1812. Data.prototype.parseDate = function (val) {
  1813. var parseDate = this.options.parseDate,
  1814. ret,
  1815. key,
  1816. format,
  1817. dateFormat = this.options.dateFormat || this.dateFormat,
  1818. match;
  1819. if (parseDate) {
  1820. ret = parseDate(val);
  1821. }
  1822. else if (typeof val === 'string') {
  1823. // Auto-detect the date format the first time
  1824. if (!dateFormat) {
  1825. for (key in this.dateFormats) { // eslint-disable-line guard-for-in
  1826. format = this.dateFormats[key];
  1827. match = val.match(format.regex);
  1828. if (match) {
  1829. this.dateFormat = dateFormat = key;
  1830. this.alternativeFormat = format.alternative;
  1831. ret = format.parser(match);
  1832. break;
  1833. }
  1834. }
  1835. // Next time, use the one previously found
  1836. }
  1837. else {
  1838. format = this.dateFormats[dateFormat];
  1839. if (!format) {
  1840. // The selected format is invalid
  1841. format = this.dateFormats['YYYY/mm/dd'];
  1842. }
  1843. match = val.match(format.regex);
  1844. if (match) {
  1845. ret = format.parser(match);
  1846. }
  1847. }
  1848. // Fall back to Date.parse
  1849. if (!match) {
  1850. match = Date.parse(val);
  1851. // External tools like Date.js and MooTools extend Date object
  1852. // and returns a date.
  1853. if (typeof match === 'object' &&
  1854. match !== null &&
  1855. match.getTime) {
  1856. ret = (match.getTime() -
  1857. match.getTimezoneOffset() *
  1858. 60000);
  1859. // Timestamp
  1860. }
  1861. else if (isNumber(match)) {
  1862. ret = match - (new Date(match)).getTimezoneOffset() * 60000;
  1863. }
  1864. }
  1865. }
  1866. return ret;
  1867. };
  1868. /**
  1869. * Reorganize rows into columns.
  1870. *
  1871. * @function Highcharts.Data#rowsToColumns
  1872. *
  1873. * @param {Array<Array<Highcharts.DataValueType>>} rows
  1874. *
  1875. * @return {Array<Array<Highcharts.DataValueType>>|undefined}
  1876. */
  1877. Data.prototype.rowsToColumns = function (rows) {
  1878. var row,
  1879. rowsLength,
  1880. col,
  1881. colsLength,
  1882. columns;
  1883. if (rows) {
  1884. columns = [];
  1885. rowsLength = rows.length;
  1886. for (row = 0; row < rowsLength; row++) {
  1887. colsLength = rows[row].length;
  1888. for (col = 0; col < colsLength; col++) {
  1889. if (!columns[col]) {
  1890. columns[col] = [];
  1891. }
  1892. columns[col][row] = rows[row][col];
  1893. }
  1894. }
  1895. }
  1896. return columns;
  1897. };
  1898. /**
  1899. * Get the parsed data in a form that we can apply directly to the
  1900. * `series.data` config. Array positions can be mapped using the
  1901. * `series.keys` option.
  1902. *
  1903. * @example
  1904. * const data = Highcharts.data({
  1905. * csv: document.getElementById('data').innerHTML
  1906. * }).getData();
  1907. *
  1908. * @function Highcharts.Data#getData
  1909. *
  1910. * @return {Array<Array<(number|string)>>|undefined} Data rows
  1911. */
  1912. Data.prototype.getData = function () {
  1913. if (this.columns) {
  1914. return this.rowsToColumns(this.columns).slice(1);
  1915. }
  1916. };
  1917. /**
  1918. * A hook for working directly on the parsed columns
  1919. *
  1920. * @function Highcharts.Data#parsed
  1921. *
  1922. * @return {boolean|undefined}
  1923. */
  1924. Data.prototype.parsed = function () {
  1925. if (this.options.parsed) {
  1926. return this.options.parsed.call(this, this.columns);
  1927. }
  1928. };
  1929. /**
  1930. * @private
  1931. * @function Highcharts.Data#getFreeIndexes
  1932. */
  1933. Data.prototype.getFreeIndexes = function (numberOfColumns, seriesBuilders) {
  1934. var s,
  1935. i,
  1936. freeIndexes = [],
  1937. freeIndexValues = [],
  1938. referencedIndexes;
  1939. // Add all columns as free
  1940. for (i = 0; i < numberOfColumns; i = i + 1) {
  1941. freeIndexes.push(true);
  1942. }
  1943. // Loop all defined builders and remove their referenced columns
  1944. for (s = 0; s < seriesBuilders.length; s = s + 1) {
  1945. referencedIndexes = seriesBuilders[s].getReferencedColumnIndexes();
  1946. for (i = 0; i < referencedIndexes.length; i = i + 1) {
  1947. freeIndexes[referencedIndexes[i]] = false;
  1948. }
  1949. }
  1950. // Collect the values for the free indexes
  1951. for (i = 0; i < freeIndexes.length; i = i + 1) {
  1952. if (freeIndexes[i]) {
  1953. freeIndexValues.push(i);
  1954. }
  1955. }
  1956. return freeIndexValues;
  1957. };
  1958. /**
  1959. * If a complete callback function is provided in the options, interpret the
  1960. * columns into a Highcharts options object.
  1961. *
  1962. * @function Highcharts.Data#complete
  1963. */
  1964. Data.prototype.complete = function () {
  1965. var columns = this.columns,
  1966. xColumns = [],
  1967. type,
  1968. options = this.options,
  1969. series,
  1970. data,
  1971. i,
  1972. j,
  1973. r,
  1974. seriesIndex,
  1975. chartOptions,
  1976. allSeriesBuilders = [],
  1977. builder,
  1978. freeIndexes,
  1979. typeCol,
  1980. index;
  1981. xColumns.length = columns.length;
  1982. if (options.complete || options.afterComplete) {
  1983. // Get the names and shift the top row
  1984. if (this.firstRowAsNames) {
  1985. for (i = 0; i < columns.length; i++) {
  1986. columns[i].name = columns[i].shift();
  1987. }
  1988. }
  1989. // Use the next columns for series
  1990. series = [];
  1991. freeIndexes = this.getFreeIndexes(columns.length, this.valueCount.seriesBuilders);
  1992. // Populate defined series
  1993. for (seriesIndex = 0; seriesIndex < this.valueCount.seriesBuilders.length; seriesIndex++) {
  1994. builder = this.valueCount.seriesBuilders[seriesIndex];
  1995. // If the builder can be populated with remaining columns, then
  1996. // add it to allBuilders
  1997. if (builder.populateColumns(freeIndexes)) {
  1998. allSeriesBuilders.push(builder);
  1999. }
  2000. }
  2001. // Populate dynamic series
  2002. while (freeIndexes.length > 0) {
  2003. builder = new SeriesBuilder();
  2004. builder.addColumnReader(0, 'x');
  2005. // Mark index as used (not free)
  2006. index = freeIndexes.indexOf(0);
  2007. if (index !== -1) {
  2008. freeIndexes.splice(index, 1);
  2009. }
  2010. for (i = 0; i < this.valueCount.global; i++) {
  2011. // Create and add a column reader for the next free column
  2012. // index
  2013. builder.addColumnReader(void 0, this.valueCount.globalPointArrayMap[i]);
  2014. }
  2015. // If the builder can be populated with remaining columns, then
  2016. // add it to allBuilders
  2017. if (builder.populateColumns(freeIndexes)) {
  2018. allSeriesBuilders.push(builder);
  2019. }
  2020. }
  2021. // Get the data-type from the first series x column
  2022. if (allSeriesBuilders.length > 0 &&
  2023. allSeriesBuilders[0].readers.length > 0) {
  2024. typeCol = columns[allSeriesBuilders[0].readers[0].columnIndex];
  2025. if (typeof typeCol !== 'undefined') {
  2026. if (typeCol.isDatetime) {
  2027. type = 'datetime';
  2028. }
  2029. else if (!typeCol.isNumeric) {
  2030. type = 'category';
  2031. }
  2032. }
  2033. }
  2034. // Axis type is category, then the "x" column should be called
  2035. // "name"
  2036. if (type === 'category') {
  2037. for (seriesIndex = 0; seriesIndex < allSeriesBuilders.length; seriesIndex++) {
  2038. builder = allSeriesBuilders[seriesIndex];
  2039. for (r = 0; r < builder.readers.length; r++) {
  2040. if (builder.readers[r].configName === 'x') {
  2041. builder.readers[r].configName = 'name';
  2042. }
  2043. }
  2044. }
  2045. }
  2046. // Read data for all builders
  2047. for (seriesIndex = 0; seriesIndex < allSeriesBuilders.length; seriesIndex++) {
  2048. builder = allSeriesBuilders[seriesIndex];
  2049. // Iterate down the cells of each column and add data to the
  2050. // series
  2051. data = [];
  2052. for (j = 0; j < columns[0].length; j++) {
  2053. data[j] = builder.read(columns, j);
  2054. }
  2055. // Add the series
  2056. series[seriesIndex] = {
  2057. data: data
  2058. };
  2059. if (builder.name) {
  2060. series[seriesIndex].name = builder.name;
  2061. }
  2062. if (type === 'category') {
  2063. series[seriesIndex].turboThreshold = 0;
  2064. }
  2065. }
  2066. // Do the callback
  2067. chartOptions = {
  2068. series: series
  2069. };
  2070. if (type) {
  2071. chartOptions.xAxis = {
  2072. type: type
  2073. };
  2074. if (type === 'category') {
  2075. chartOptions.xAxis.uniqueNames = false;
  2076. }
  2077. }
  2078. if (options.complete) {
  2079. options.complete(chartOptions);
  2080. }
  2081. // The afterComplete hook is used internally to avoid conflict with
  2082. // the externally available complete option.
  2083. if (options.afterComplete) {
  2084. options.afterComplete(chartOptions);
  2085. }
  2086. }
  2087. };
  2088. /**
  2089. * Updates the chart with new data options.
  2090. *
  2091. * @function Highcharts.Data#update
  2092. *
  2093. * @param {Highcharts.DataOptions} options
  2094. *
  2095. * @param {boolean} [redraw=true]
  2096. */
  2097. Data.prototype.update = function (options, redraw) {
  2098. var chart = this.chart;
  2099. if (options) {
  2100. // Set the complete handler
  2101. options.afterComplete = function (dataOptions) {
  2102. // Avoid setting axis options unless the type changes. Running
  2103. // Axis.update will cause the whole structure to be destroyed
  2104. // and rebuilt, and animation is lost.
  2105. if (dataOptions) {
  2106. if (dataOptions.xAxis &&
  2107. chart.xAxis[0] &&
  2108. dataOptions.xAxis.type ===
  2109. chart.xAxis[0].options.type) {
  2110. delete dataOptions.xAxis;
  2111. }
  2112. // @todo looks not right:
  2113. chart.update(dataOptions, redraw, true);
  2114. }
  2115. };
  2116. // Apply it
  2117. merge(true, chart.options.data, options);
  2118. this.init(chart.options.data);
  2119. }
  2120. };
  2121. return Data;
  2122. }());
  2123. // Register the Data prototype and data function on Highcharts
  2124. // Highcharts.Data = Data as any;
  2125. /**
  2126. * Creates a data object to parse data for a chart.
  2127. *
  2128. * @function Highcharts.data
  2129. *
  2130. * @param {Highcharts.DataOptions} dataOptions
  2131. *
  2132. * @param {Highcharts.Options} [chartOptions]
  2133. *
  2134. * @param {Highcharts.Chart} [chart]
  2135. *
  2136. * @return {Highcharts.Data}
  2137. */
  2138. H.data = function (dataOptions, chartOptions, chart) {
  2139. return new H.Data(dataOptions, chartOptions, chart);
  2140. };
  2141. // Extend Chart.init so that the Chart constructor accepts a new configuration
  2142. // option group, data.
  2143. addEvent(Chart, 'init', function (e) {
  2144. var chart = this, // eslint-disable-line no-invalid-this
  2145. userOptions = (e.args[0] || {}),
  2146. callback = e.args[1];
  2147. if (userOptions && userOptions.data && !chart.hasDataDef) {
  2148. chart.hasDataDef = true;
  2149. /**
  2150. * The data parser for this chart.
  2151. *
  2152. * @name Highcharts.Chart#data
  2153. * @type {Highcharts.Data|undefined}
  2154. */
  2155. chart.data = new H.Data(extend(userOptions.data, {
  2156. afterComplete: function (dataOptions) {
  2157. var i,
  2158. series;
  2159. // Merge series configs
  2160. if (Object.hasOwnProperty.call(userOptions, 'series')) {
  2161. if (typeof userOptions.series === 'object') {
  2162. i = Math.max(userOptions.series.length, dataOptions && dataOptions.series ?
  2163. dataOptions.series.length :
  2164. 0);
  2165. while (i--) {
  2166. series = userOptions.series[i] || {};
  2167. userOptions.series[i] = merge(series, dataOptions && dataOptions.series ?
  2168. dataOptions.series[i] :
  2169. {});
  2170. }
  2171. }
  2172. else { // Allow merging in dataOptions.series (#2856)
  2173. delete userOptions.series;
  2174. }
  2175. }
  2176. // Do the merge
  2177. userOptions = merge(dataOptions, userOptions);
  2178. // Run chart.init again
  2179. chart.init(userOptions, callback);
  2180. }
  2181. }), userOptions, chart);
  2182. e.preventDefault();
  2183. }
  2184. });
  2185. /**
  2186. * Creates a new SeriesBuilder. A SeriesBuilder consists of a number
  2187. * of ColumnReaders that reads columns and give them a name.
  2188. * Ex: A series builder can be constructed to read column 3 as 'x' and
  2189. * column 7 and 8 as 'y1' and 'y2'.
  2190. * The output would then be points/rows of the form {x: 11, y1: 22, y2: 33}
  2191. *
  2192. * The name of the builder is taken from the second column. In the above
  2193. * example it would be the column with index 7.
  2194. *
  2195. * @private
  2196. * @class
  2197. * @name SeriesBuilder
  2198. */
  2199. var SeriesBuilder = /** @class */ (function () {
  2200. function SeriesBuilder() {
  2201. /* eslint-disable no-invalid-this */
  2202. this.readers = [];
  2203. this.pointIsArray = true;
  2204. /* eslint-enable no-invalid-this */
  2205. this.name = void 0;
  2206. }
  2207. /**
  2208. * Populates readers with column indexes. A reader can be added without
  2209. * a specific index and for those readers the index is taken sequentially
  2210. * from the free columns (this is handled by the ColumnCursor instance).
  2211. *
  2212. * @function SeriesBuilder#populateColumns
  2213. *
  2214. * @param {Array<number>} freeIndexes
  2215. *
  2216. * @returns {boolean}
  2217. */
  2218. SeriesBuilder.prototype.populateColumns = function (freeIndexes) {
  2219. var builder = this,
  2220. enoughColumns = true;
  2221. // Loop each reader and give it an index if its missing.
  2222. // The freeIndexes.shift() will return undefined if there
  2223. // are no more columns.
  2224. builder.readers.forEach(function (reader) {
  2225. if (typeof reader.columnIndex === 'undefined') {
  2226. reader.columnIndex = freeIndexes.shift();
  2227. }
  2228. });
  2229. // Now, all readers should have columns mapped. If not
  2230. // then return false to signal that this series should
  2231. // not be added.
  2232. builder.readers.forEach(function (reader) {
  2233. if (typeof reader.columnIndex === 'undefined') {
  2234. enoughColumns = false;
  2235. }
  2236. });
  2237. return enoughColumns;
  2238. };
  2239. /**
  2240. * Reads a row from the dataset and returns a point or array depending
  2241. * on the names of the readers.
  2242. *
  2243. * @function SeriesBuilder#read<T>
  2244. *
  2245. * @param {Array<Array<T>>} columns
  2246. *
  2247. * @param {number} rowIndex
  2248. *
  2249. * @returns {Array<T>|Highcharts.Dictionary<T>}
  2250. */
  2251. SeriesBuilder.prototype.read = function (columns, rowIndex) {
  2252. var builder = this,
  2253. pointIsArray = builder.pointIsArray,
  2254. point = pointIsArray ? [] : {},
  2255. columnIndexes;
  2256. // Loop each reader and ask it to read its value.
  2257. // Then, build an array or point based on the readers names.
  2258. builder.readers.forEach(function (reader) {
  2259. var value = columns[reader.columnIndex][rowIndex];
  2260. if (pointIsArray) {
  2261. point.push(value);
  2262. }
  2263. else {
  2264. if (reader.configName.indexOf('.') > 0) {
  2265. // Handle nested property names
  2266. Point.prototype.setNestedProperty(point, value, reader.configName);
  2267. }
  2268. else {
  2269. point[reader.configName] = value;
  2270. }
  2271. }
  2272. });
  2273. // The name comes from the first column (excluding the x column)
  2274. if (typeof this.name === 'undefined' && builder.readers.length >= 2) {
  2275. columnIndexes = builder.getReferencedColumnIndexes();
  2276. if (columnIndexes.length >= 2) {
  2277. // remove the first one (x col)
  2278. columnIndexes.shift();
  2279. // Sort the remaining
  2280. columnIndexes.sort(function (a, b) {
  2281. return a - b;
  2282. });
  2283. // Now use the lowest index as name column
  2284. this.name = columns[columnIndexes.shift()].name;
  2285. }
  2286. }
  2287. return point;
  2288. };
  2289. /**
  2290. * Creates and adds ColumnReader from the given columnIndex and configName.
  2291. * ColumnIndex can be undefined and in that case the reader will be given
  2292. * an index when columns are populated.
  2293. *
  2294. * @function SeriesBuilder#addColumnReader
  2295. *
  2296. * @param {number} columnIndex
  2297. *
  2298. * @param {string} configName
  2299. */
  2300. SeriesBuilder.prototype.addColumnReader = function (columnIndex, configName) {
  2301. this.readers.push({
  2302. columnIndex: columnIndex,
  2303. configName: configName
  2304. });
  2305. if (!(configName === 'x' ||
  2306. configName === 'y' ||
  2307. typeof configName === 'undefined')) {
  2308. this.pointIsArray = false;
  2309. }
  2310. };
  2311. /**
  2312. * Returns an array of column indexes that the builder will use when
  2313. * reading data.
  2314. *
  2315. * @function SeriesBuilder#getReferencedColumnIndexes
  2316. *
  2317. * @returns {Array<number>}
  2318. */
  2319. SeriesBuilder.prototype.getReferencedColumnIndexes = function () {
  2320. var i,
  2321. referencedColumnIndexes = [],
  2322. columnReader;
  2323. for (i = 0; i < this.readers.length; i = i + 1) {
  2324. columnReader = this.readers[i];
  2325. if (typeof columnReader.columnIndex !== 'undefined') {
  2326. referencedColumnIndexes.push(columnReader.columnIndex);
  2327. }
  2328. }
  2329. return referencedColumnIndexes;
  2330. };
  2331. /**
  2332. * Returns true if the builder has a reader for the given configName.
  2333. *
  2334. * @function SeriesBuider#hasReader
  2335. *
  2336. * @param {string} configName
  2337. *
  2338. * @returns {boolean|undefined}
  2339. */
  2340. SeriesBuilder.prototype.hasReader = function (configName) {
  2341. var i,
  2342. columnReader;
  2343. for (i = 0; i < this.readers.length; i = i + 1) {
  2344. columnReader = this.readers[i];
  2345. if (columnReader.configName === configName) {
  2346. return true;
  2347. }
  2348. }
  2349. // Else return undefined
  2350. };
  2351. return SeriesBuilder;
  2352. }());
  2353. H.Data = Data;
  2354. return H.Data;
  2355. });
  2356. _registerModule(_modules, 'masters/modules/data.src.js', [], function () {
  2357. });
  2358. }));