data.src.js 74 KB

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