Data.js 76 KB

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