boost-overrides.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* *
  2. *
  3. * Copyright (c) 2019-2019 Highsoft AS
  4. *
  5. * Boost module: stripped-down renderer for higher performance
  6. *
  7. * License: highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import H from '../../parts/Globals.js';
  14. import U from '../../parts/Utilities.js';
  15. var isNumber = U.isNumber, wrap = U.wrap;
  16. import '../../parts/Color.js';
  17. import '../../parts/Series.js';
  18. import '../../parts/Options.js';
  19. import '../../parts/Point.js';
  20. import '../../parts/Interaction.js';
  21. import butils from './boost-utils.js';
  22. import boostable from './boostables.js';
  23. import boostableMap from './boostable-map.js';
  24. var boostEnabled = butils.boostEnabled, shouldForceChartSeriesBoosting = butils.shouldForceChartSeriesBoosting, Chart = H.Chart, Series = H.Series, Point = H.Point, seriesTypes = H.seriesTypes, addEvent = H.addEvent, pick = H.pick, plotOptions = H.getOptions().plotOptions;
  25. /**
  26. * Returns true if the chart is in series boost mode.
  27. *
  28. * @function Highcharts.Chart#isChartSeriesBoosting
  29. *
  30. * @param {Highcharts.Chart} chart
  31. * the chart to check
  32. *
  33. * @return {boolean}
  34. * true if the chart is in series boost mode
  35. */
  36. Chart.prototype.isChartSeriesBoosting = function () {
  37. var isSeriesBoosting, threshold = pick(this.options.boost && this.options.boost.seriesThreshold, 50);
  38. isSeriesBoosting = threshold <= this.series.length ||
  39. shouldForceChartSeriesBoosting(this);
  40. return isSeriesBoosting;
  41. };
  42. /* eslint-disable valid-jsdoc */
  43. /**
  44. * Get the clip rectangle for a target, either a series or the chart. For the
  45. * chart, we need to consider the maximum extent of its Y axes, in case of
  46. * Highstock panes and navigator.
  47. *
  48. * @private
  49. * @function Highcharts.Chart#getBoostClipRect
  50. *
  51. * @param {Highcharts.Chart} target
  52. *
  53. * @return {Highcharts.BBoxObject}
  54. */
  55. Chart.prototype.getBoostClipRect = function (target) {
  56. var clipBox = {
  57. x: this.plotLeft,
  58. y: this.plotTop,
  59. width: this.plotWidth,
  60. height: this.plotHeight
  61. };
  62. if (target === this) {
  63. this.yAxis.forEach(function (yAxis) {
  64. clipBox.y = Math.min(yAxis.pos, clipBox.y);
  65. clipBox.height = Math.max(yAxis.pos - this.plotTop + yAxis.len, clipBox.height);
  66. }, this);
  67. }
  68. return clipBox;
  69. };
  70. /**
  71. * Return a full Point object based on the index.
  72. * The boost module uses stripped point objects for performance reasons.
  73. *
  74. * @function Highcharts.Series#getPoint
  75. *
  76. * @param {object|Highcharts.Point} boostPoint
  77. * A stripped-down point object
  78. *
  79. * @return {Highcharts.Point}
  80. * A Point object as per http://api.highcharts.com/highcharts#Point
  81. */
  82. Series.prototype.getPoint = function (boostPoint) {
  83. var point = boostPoint, xData = (this.xData || this.options.xData || this.processedXData ||
  84. false);
  85. if (boostPoint && !(boostPoint instanceof this.pointClass)) {
  86. point = (new this.pointClass()).init(// eslint-disable-line new-cap
  87. this, this.options.data[boostPoint.i], xData ? xData[boostPoint.i] : void 0);
  88. point.category = pick(this.xAxis.categories ?
  89. this.xAxis.categories[point.x] :
  90. point.x, // @todo simplify
  91. point.x);
  92. point.dist = boostPoint.dist;
  93. point.distX = boostPoint.distX;
  94. point.plotX = boostPoint.plotX;
  95. point.plotY = boostPoint.plotY;
  96. point.index = boostPoint.i;
  97. }
  98. return point;
  99. };
  100. /* eslint-disable no-invalid-this */
  101. // Return a point instance from the k-d-tree
  102. wrap(Series.prototype, 'searchPoint', function (proceed) {
  103. return this.getPoint(proceed.apply(this, [].slice.call(arguments, 1)));
  104. });
  105. // For inverted series, we need to swap X-Y values before running base methods
  106. wrap(Point.prototype, 'haloPath', function (proceed) {
  107. var halo, point = this, series = point.series, chart = series.chart, plotX = point.plotX, plotY = point.plotY, inverted = chart.inverted;
  108. if (series.isSeriesBoosting && inverted) {
  109. point.plotX = series.yAxis.len - plotY;
  110. point.plotY = series.xAxis.len - plotX;
  111. }
  112. halo = proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  113. if (series.isSeriesBoosting && inverted) {
  114. point.plotX = plotX;
  115. point.plotY = plotY;
  116. }
  117. return halo;
  118. });
  119. wrap(Series.prototype, 'markerAttribs', function (proceed, point) {
  120. var attribs, series = this, chart = series.chart, plotX = point.plotX, plotY = point.plotY, inverted = chart.inverted;
  121. if (series.isSeriesBoosting && inverted) {
  122. point.plotX = series.yAxis.len - plotY;
  123. point.plotY = series.xAxis.len - plotX;
  124. }
  125. attribs = proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  126. if (series.isSeriesBoosting && inverted) {
  127. point.plotX = plotX;
  128. point.plotY = plotY;
  129. }
  130. return attribs;
  131. });
  132. /*
  133. * Extend series.destroy to also remove the fake k-d-tree points (#5137).
  134. * Normally this is handled by Series.destroy that calls Point.destroy,
  135. * but the fake search points are not registered like that.
  136. */
  137. addEvent(Series, 'destroy', function () {
  138. var series = this, chart = series.chart;
  139. if (chart.markerGroup === series.markerGroup) {
  140. series.markerGroup = null;
  141. }
  142. if (chart.hoverPoints) {
  143. chart.hoverPoints = chart.hoverPoints.filter(function (point) {
  144. return point.series === series;
  145. });
  146. }
  147. if (chart.hoverPoint && chart.hoverPoint.series === series) {
  148. chart.hoverPoint = null;
  149. }
  150. });
  151. /*
  152. * Do not compute extremes when min and max are set.
  153. * If we use this in the core, we can add the hook
  154. * to hasExtremes to the methods directly.
  155. */
  156. wrap(Series.prototype, 'getExtremes', function (proceed) {
  157. if (!this.isSeriesBoosting || (!this.hasExtremes || !this.hasExtremes())) {
  158. return proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  159. }
  160. });
  161. /*
  162. * Override a bunch of methods the same way. If the number of points is
  163. * below the threshold, run the original method. If not, check for a
  164. * canvas version or do nothing.
  165. *
  166. * Note that we're not overriding any of these for heatmaps.
  167. */
  168. [
  169. 'translate',
  170. 'generatePoints',
  171. 'drawTracker',
  172. 'drawPoints',
  173. 'render'
  174. ].forEach(function (method) {
  175. /**
  176. * @private
  177. */
  178. function branch(proceed) {
  179. var letItPass = this.options.stacking &&
  180. (method === 'translate' || method === 'generatePoints');
  181. if (!this.isSeriesBoosting ||
  182. letItPass ||
  183. !boostEnabled(this.chart) ||
  184. this.type === 'heatmap' ||
  185. this.type === 'treemap' ||
  186. !boostableMap[this.type] ||
  187. this.options.boostThreshold === 0) {
  188. proceed.call(this);
  189. // If a canvas version of the method exists, like renderCanvas(), run
  190. }
  191. else if (this[method + 'Canvas']) {
  192. this[method + 'Canvas']();
  193. }
  194. }
  195. wrap(Series.prototype, method, branch);
  196. // A special case for some types - their translate method is already wrapped
  197. if (method === 'translate') {
  198. [
  199. 'column',
  200. 'bar',
  201. 'arearange',
  202. 'columnrange',
  203. 'heatmap',
  204. 'treemap'
  205. ].forEach(function (type) {
  206. if (seriesTypes[type]) {
  207. wrap(seriesTypes[type].prototype, method, branch);
  208. }
  209. });
  210. }
  211. });
  212. // If the series is a heatmap or treemap, or if the series is not boosting
  213. // do the default behaviour. Otherwise, process if the series has no extremes.
  214. wrap(Series.prototype, 'processData', function (proceed) {
  215. var series = this, dataToMeasure = this.options.data, firstPoint;
  216. /**
  217. * Used twice in this function, first on this.options.data, the second
  218. * time it runs the check again after processedXData is built.
  219. * @private
  220. * @todo Check what happens with data grouping
  221. */
  222. function getSeriesBoosting(data) {
  223. return series.chart.isChartSeriesBoosting() || ((data ? data.length : 0) >=
  224. (series.options.boostThreshold || Number.MAX_VALUE));
  225. }
  226. if (boostEnabled(this.chart) && boostableMap[this.type]) {
  227. // If there are no extremes given in the options, we also need to
  228. // process the data to read the data extremes. If this is a heatmap, do
  229. // default behaviour.
  230. if (!getSeriesBoosting(dataToMeasure) || // First pass with options.data
  231. this.type === 'heatmap' ||
  232. this.type === 'treemap' ||
  233. this.options.stacking || // processedYData for the stack (#7481)
  234. !this.hasExtremes ||
  235. !this.hasExtremes(true)) {
  236. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  237. dataToMeasure = this.processedXData;
  238. }
  239. // Set the isBoosting flag, second pass with processedXData to see if we
  240. // have zoomed.
  241. this.isSeriesBoosting = getSeriesBoosting(dataToMeasure);
  242. // Enter or exit boost mode
  243. if (this.isSeriesBoosting) {
  244. // Force turbo-mode:
  245. firstPoint = this.getFirstValidPoint(this.options.data);
  246. if (!isNumber(firstPoint) && !H.isArray(firstPoint)) {
  247. H.error(12, false, this.chart);
  248. }
  249. this.enterBoost();
  250. }
  251. else if (this.exitBoost) {
  252. this.exitBoost();
  253. }
  254. // The series type is not boostable
  255. }
  256. else {
  257. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  258. }
  259. });
  260. addEvent(Series, 'hide', function () {
  261. if (this.canvas && this.renderTarget) {
  262. if (this.ogl) {
  263. this.ogl.clear();
  264. }
  265. this.boostClear();
  266. }
  267. });
  268. /**
  269. * Enter boost mode and apply boost-specific properties.
  270. *
  271. * @function Highcharts.Series#enterBoost
  272. */
  273. Series.prototype.enterBoost = function () {
  274. this.alteredByBoost = [];
  275. // Save the original values, including whether it was an own property or
  276. // inherited from the prototype.
  277. ['allowDG', 'directTouch', 'stickyTracking'].forEach(function (prop) {
  278. this.alteredByBoost.push({
  279. prop: prop,
  280. val: this[prop],
  281. own: Object.hasOwnProperty.call(this, prop)
  282. });
  283. }, this);
  284. this.allowDG = false;
  285. this.directTouch = false;
  286. this.stickyTracking = true;
  287. // Once we've been in boost mode, we don't want animation when returning to
  288. // vanilla mode.
  289. this.animate = null;
  290. // Hide series label if any
  291. if (this.labelBySeries) {
  292. this.labelBySeries = this.labelBySeries.destroy();
  293. }
  294. };
  295. /**
  296. * Exit from boost mode and restore non-boost properties.
  297. *
  298. * @function Highcharts.Series#exitBoost
  299. */
  300. Series.prototype.exitBoost = function () {
  301. // Reset instance properties and/or delete instance properties and go back
  302. // to prototype
  303. (this.alteredByBoost || []).forEach(function (setting) {
  304. if (setting.own) {
  305. this[setting.prop] = setting.val;
  306. }
  307. else {
  308. // Revert to prototype
  309. delete this[setting.prop];
  310. }
  311. }, this);
  312. // Clear previous run
  313. if (this.boostClear) {
  314. this.boostClear();
  315. }
  316. };
  317. /**
  318. * @private
  319. * @function Highcharts.Series#hasExtremes
  320. *
  321. * @param {boolean} checkX
  322. *
  323. * @return {boolean}
  324. */
  325. Series.prototype.hasExtremes = function (checkX) {
  326. var options = this.options, data = options.data, xAxis = this.xAxis && this.xAxis.options, yAxis = this.yAxis && this.yAxis.options, colorAxis = this.colorAxis && this.colorAxis.options;
  327. return data.length > (options.boostThreshold || Number.MAX_VALUE) &&
  328. // Defined yAxis extremes
  329. isNumber(yAxis.min) &&
  330. isNumber(yAxis.max) &&
  331. // Defined (and required) xAxis extremes
  332. (!checkX ||
  333. (isNumber(xAxis.min) && isNumber(xAxis.max))) &&
  334. // Defined (e.g. heatmap) colorAxis extremes
  335. (!colorAxis ||
  336. (isNumber(colorAxis.min) && isNumber(colorAxis.max)));
  337. };
  338. /**
  339. * If implemented in the core, parts of this can probably be
  340. * shared with other similar methods in Highcharts.
  341. *
  342. * @function Highcharts.Series#destroyGraphics
  343. */
  344. Series.prototype.destroyGraphics = function () {
  345. var series = this, points = this.points, point, i;
  346. if (points) {
  347. for (i = 0; i < points.length; i = i + 1) {
  348. point = points[i];
  349. if (point && point.destroyElements) {
  350. point.destroyElements(); // #7557
  351. }
  352. }
  353. }
  354. ['graph', 'area', 'tracker'].forEach(function (prop) {
  355. if (series[prop]) {
  356. series[prop] = series[prop].destroy();
  357. }
  358. });
  359. };
  360. // Set default options
  361. boostable.forEach(function (type) {
  362. if (plotOptions[type]) {
  363. plotOptions[type].boostThreshold = 5000;
  364. plotOptions[type].boostData = [];
  365. seriesTypes[type].prototype.fillOpacity = true;
  366. }
  367. });