BoostOverrides.js 13 KB

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