boost-canvas.src.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /**
  2. * @license Highcharts JS v8.2.0 (2020-08-20)
  3. *
  4. * Boost module
  5. *
  6. * (c) 2010-2019 Highsoft AS
  7. * Author: Torstein Honsi
  8. *
  9. * License: www.highcharts.com/license
  10. */
  11. 'use strict';
  12. (function (factory) {
  13. if (typeof module === 'object' && module.exports) {
  14. factory['default'] = factory;
  15. module.exports = factory;
  16. } else if (typeof define === 'function' && define.amd) {
  17. define('highcharts/modules/boost-canvas', ['highcharts'], function (Highcharts) {
  18. factory(Highcharts);
  19. factory.Highcharts = Highcharts;
  20. return factory;
  21. });
  22. } else {
  23. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  24. }
  25. }(function (Highcharts) {
  26. var _modules = Highcharts ? Highcharts._modules : {};
  27. function _registerModule(obj, path, args, fn) {
  28. if (!obj.hasOwnProperty(path)) {
  29. obj[path] = fn.apply(null, args);
  30. }
  31. }
  32. _registerModule(_modules, 'Extensions/BoostCanvas.js', [_modules['Core/Chart/Chart.js'], _modules['Core/Globals.js'], _modules['Core/Color.js'], _modules['Core/Utilities.js']], function (Chart, H, Color, U) {
  33. /* *
  34. *
  35. * License: www.highcharts.com/license
  36. * Author: Torstein Honsi, Christer Vasseng
  37. *
  38. * This module serves as a fallback for the Boost module in IE9 and IE10. Newer
  39. * browsers support WebGL which is faster.
  40. *
  41. * It is recommended to include this module in conditional comments targeting
  42. * IE9 and IE10.
  43. *
  44. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  45. *
  46. * */
  47. var color = Color.parse;
  48. var addEvent = U.addEvent,
  49. extend = U.extend,
  50. fireEvent = U.fireEvent,
  51. isNumber = U.isNumber,
  52. merge = U.merge,
  53. pick = U.pick,
  54. wrap = U.wrap;
  55. var win = H.win,
  56. doc = win.document,
  57. noop = function () { },
  58. Series = H.Series,
  59. seriesTypes = H.seriesTypes,
  60. CHUNK_SIZE = 50000,
  61. destroyLoadingDiv;
  62. /* eslint-disable no-invalid-this, valid-jsdoc */
  63. /**
  64. * Initialize the canvas boost.
  65. *
  66. * @function Highcharts.initCanvasBoost
  67. */
  68. H.initCanvasBoost = function () {
  69. if (H.seriesTypes.heatmap) {
  70. wrap(H.seriesTypes.heatmap.prototype, 'drawPoints', function () {
  71. var chart = this.chart,
  72. ctx = this.getContext(),
  73. inverted = this.chart.inverted,
  74. xAxis = this.xAxis,
  75. yAxis = this.yAxis;
  76. if (ctx) {
  77. // draw the columns
  78. this.points.forEach(function (point) {
  79. var plotY = point.plotY,
  80. shapeArgs,
  81. pointAttr;
  82. if (typeof plotY !== 'undefined' &&
  83. !isNaN(plotY) &&
  84. point.y !== null) {
  85. shapeArgs = point.shapeArgs;
  86. if (!chart.styledMode) {
  87. pointAttr = point.series.pointAttribs(point);
  88. }
  89. else {
  90. pointAttr = point.series.colorAttribs(point);
  91. }
  92. ctx.fillStyle = pointAttr.fill;
  93. if (inverted) {
  94. ctx.fillRect(yAxis.len - shapeArgs.y + xAxis.left, xAxis.len - shapeArgs.x + yAxis.top, -shapeArgs.height, -shapeArgs.width);
  95. }
  96. else {
  97. ctx.fillRect(shapeArgs.x + xAxis.left, shapeArgs.y + yAxis.top, shapeArgs.width, shapeArgs.height);
  98. }
  99. }
  100. });
  101. this.canvasToSVG();
  102. }
  103. else {
  104. this.chart.showLoading('Your browser doesn\'t support HTML5 canvas, <br>' +
  105. 'please use a modern browser');
  106. // Uncomment this to provide low-level (slow) support in oldIE.
  107. // It will cause script errors on charts with more than a few
  108. // thousand points.
  109. // arguments[0].call(this);
  110. }
  111. });
  112. }
  113. extend(Series.prototype, {
  114. /**
  115. * Create a hidden canvas to draw the graph on. The contents is later
  116. * copied over to an SVG image element.
  117. *
  118. * @private
  119. * @function Highcharts.Series#getContext
  120. */
  121. getContext: function () {
  122. var chart = this.chart,
  123. width = chart.chartWidth,
  124. height = chart.chartHeight,
  125. targetGroup = chart.seriesGroup || this.group,
  126. target = this,
  127. ctx,
  128. swapXY = function (proceed,
  129. x,
  130. y,
  131. a,
  132. b,
  133. c,
  134. d) {
  135. proceed.call(this,
  136. y,
  137. x,
  138. a,
  139. b,
  140. c,
  141. d);
  142. };
  143. if (chart.isChartSeriesBoosting()) {
  144. target = chart;
  145. targetGroup = chart.seriesGroup;
  146. }
  147. ctx = target.ctx;
  148. if (!target.canvas) {
  149. target.canvas = doc.createElement('canvas');
  150. target.renderTarget = chart.renderer
  151. .image('', 0, 0, width, height)
  152. .addClass('highcharts-boost-canvas')
  153. .add(targetGroup);
  154. target.ctx = ctx = target.canvas.getContext('2d');
  155. if (chart.inverted) {
  156. ['moveTo', 'lineTo', 'rect', 'arc'].forEach(function (fn) {
  157. wrap(ctx, fn, swapXY);
  158. });
  159. }
  160. target.boostCopy = function () {
  161. target.renderTarget.attr({
  162. href: target.canvas.toDataURL('image/png')
  163. });
  164. };
  165. target.boostClear = function () {
  166. ctx.clearRect(0, 0, target.canvas.width, target.canvas.height);
  167. if (target === this) {
  168. target.renderTarget.attr({ href: '' });
  169. }
  170. };
  171. target.boostClipRect = chart.renderer.clipRect();
  172. target.renderTarget.clip(target.boostClipRect);
  173. }
  174. else if (!(target instanceof H.Chart)) {
  175. // ctx.clearRect(0, 0, width, height);
  176. }
  177. if (target.canvas.width !== width) {
  178. target.canvas.width = width;
  179. }
  180. if (target.canvas.height !== height) {
  181. target.canvas.height = height;
  182. }
  183. target.renderTarget.attr({
  184. x: 0,
  185. y: 0,
  186. width: width,
  187. height: height,
  188. style: 'pointer-events: none',
  189. href: ''
  190. });
  191. target.boostClipRect.attr(chart.getBoostClipRect(target));
  192. return ctx;
  193. },
  194. /**
  195. * Draw the canvas image inside an SVG image
  196. *
  197. * @private
  198. * @function Highcharts.Series#canvasToSVG
  199. */
  200. canvasToSVG: function () {
  201. if (!this.chart.isChartSeriesBoosting()) {
  202. if (this.boostCopy || this.chart.boostCopy) {
  203. (this.boostCopy || this.chart.boostCopy)();
  204. }
  205. }
  206. else {
  207. if (this.boostClear) {
  208. this.boostClear();
  209. }
  210. }
  211. },
  212. cvsLineTo: function (ctx, clientX, plotY) {
  213. ctx.lineTo(clientX, plotY);
  214. },
  215. renderCanvas: function () {
  216. var series = this, options = series.options, chart = series.chart, xAxis = this.xAxis, yAxis = this.yAxis, activeBoostSettings = chart.options.boost || {}, boostSettings = {
  217. timeRendering: activeBoostSettings.timeRendering || false,
  218. timeSeriesProcessing: activeBoostSettings.timeSeriesProcessing || false,
  219. timeSetup: activeBoostSettings.timeSetup || false
  220. }, ctx, c = 0, xData = series.processedXData, yData = series.processedYData, rawData = options.data, xExtremes = xAxis.getExtremes(), xMin = xExtremes.min, xMax = xExtremes.max, yExtremes = yAxis.getExtremes(), yMin = yExtremes.min, yMax = yExtremes.max, pointTaken = {}, lastClientX, sampling = !!series.sampling, points, r = options.marker && options.marker.radius, cvsDrawPoint = this.cvsDrawPoint, cvsLineTo = options.lineWidth ? this.cvsLineTo : void 0, cvsMarker = (r && r <= 1 ?
  221. this.cvsMarkerSquare :
  222. this.cvsMarkerCircle), strokeBatch = this.cvsStrokeBatch || 1000, enableMouseTracking = options.enableMouseTracking !== false, lastPoint, threshold = options.threshold, yBottom = yAxis.getThreshold(threshold), hasThreshold = isNumber(threshold), translatedThreshold = yBottom, doFill = this.fill, isRange = (series.pointArrayMap &&
  223. series.pointArrayMap.join(',') === 'low,high'), isStacked = !!options.stacking, cropStart = series.cropStart || 0, loadingOptions = chart.options.loading, requireSorting = series.requireSorting, wasNull, connectNulls = options.connectNulls, useRaw = !xData, minVal, maxVal, minI, maxI, index, sdata = (isStacked ?
  224. series.data :
  225. (xData || rawData)), fillColor = (series.fillOpacity ?
  226. new Color(series.color).setOpacity(pick(options.fillOpacity, 0.75)).get() :
  227. series.color),
  228. //
  229. stroke = function () {
  230. if (doFill) {
  231. ctx.fillStyle = fillColor;
  232. ctx.fill();
  233. }
  234. else {
  235. ctx.strokeStyle = series.color;
  236. ctx.lineWidth = options.lineWidth;
  237. ctx.stroke();
  238. }
  239. },
  240. //
  241. drawPoint = function (clientX, plotY, yBottom, i) {
  242. if (c === 0) {
  243. ctx.beginPath();
  244. if (cvsLineTo) {
  245. ctx.lineJoin = 'round';
  246. }
  247. }
  248. if (chart.scroller &&
  249. series.options.className ===
  250. 'highcharts-navigator-series') {
  251. plotY += chart.scroller.top;
  252. if (yBottom) {
  253. yBottom += chart.scroller.top;
  254. }
  255. }
  256. else {
  257. plotY += chart.plotTop;
  258. }
  259. clientX += chart.plotLeft;
  260. if (wasNull) {
  261. ctx.moveTo(clientX, plotY);
  262. }
  263. else {
  264. if (cvsDrawPoint) {
  265. cvsDrawPoint(ctx, clientX, plotY, yBottom, lastPoint);
  266. }
  267. else if (cvsLineTo) {
  268. cvsLineTo(ctx, clientX, plotY);
  269. }
  270. else if (cvsMarker) {
  271. cvsMarker.call(series, ctx, clientX, plotY, r, i);
  272. }
  273. }
  274. // We need to stroke the line for every 1000 pixels. It will
  275. // crash the browser memory use if we stroke too
  276. // infrequently.
  277. c = c + 1;
  278. if (c === strokeBatch) {
  279. stroke();
  280. c = 0;
  281. }
  282. // Area charts need to keep track of the last point
  283. lastPoint = {
  284. clientX: clientX,
  285. plotY: plotY,
  286. yBottom: yBottom
  287. };
  288. },
  289. //
  290. compareX = options.findNearestPointBy === 'x',
  291. //
  292. xDataFull = (this.xData ||
  293. this.options.xData ||
  294. this.processedXData ||
  295. false),
  296. //
  297. addKDPoint = function (clientX, plotY, i) {
  298. // Shaves off about 60ms compared to repeated concatenation
  299. index = compareX ? clientX : clientX + ',' + plotY;
  300. // The k-d tree requires series points.
  301. // Reduce the amount of points, since the time to build the
  302. // tree increases exponentially.
  303. if (enableMouseTracking && !pointTaken[index]) {
  304. pointTaken[index] = true;
  305. if (chart.inverted) {
  306. clientX = xAxis.len - clientX;
  307. plotY = yAxis.len - plotY;
  308. }
  309. points.push({
  310. x: xDataFull ?
  311. xDataFull[cropStart + i] :
  312. false,
  313. clientX: clientX,
  314. plotX: clientX,
  315. plotY: plotY,
  316. i: cropStart + i
  317. });
  318. }
  319. };
  320. if (this.renderTarget) {
  321. this.renderTarget.attr({ 'href': '' });
  322. }
  323. // If we are zooming out from SVG mode, destroy the graphics
  324. if (this.points || this.graph) {
  325. this.destroyGraphics();
  326. }
  327. // The group
  328. series.plotGroup('group', 'series', series.visible ? 'visible' : 'hidden', options.zIndex, chart.seriesGroup);
  329. series.markerGroup = series.group;
  330. addEvent(series, 'destroy', function () {
  331. // Prevent destroy twice
  332. series.markerGroup = null;
  333. });
  334. points = this.points = [];
  335. ctx = this.getContext();
  336. series.buildKDTree = noop; // Do not start building while drawing
  337. if (this.boostClear) {
  338. this.boostClear();
  339. }
  340. // if (this.canvas) {
  341. // ctx.clearRect(
  342. // 0,
  343. // 0,
  344. // this.canvas.width,
  345. // this.canvas.height
  346. // );
  347. // }
  348. if (!this.visible) {
  349. return;
  350. }
  351. // Display a loading indicator
  352. if (rawData.length > 99999) {
  353. chart.options.loading = merge(loadingOptions, {
  354. labelStyle: {
  355. backgroundColor: color('#ffffff').setOpacity(0.75).get(),
  356. padding: '1em',
  357. borderRadius: '0.5em'
  358. },
  359. style: {
  360. backgroundColor: 'none',
  361. opacity: 1
  362. }
  363. });
  364. U.clearTimeout(destroyLoadingDiv);
  365. chart.showLoading('Drawing...');
  366. chart.options.loading = loadingOptions; // reset
  367. }
  368. if (boostSettings.timeRendering) {
  369. console.time('canvas rendering'); // eslint-disable-line no-console
  370. }
  371. // Loop over the points
  372. H.eachAsync(sdata, function (d, i) {
  373. var x,
  374. y,
  375. clientX,
  376. plotY,
  377. isNull,
  378. low,
  379. isNextInside = false,
  380. isPrevInside = false,
  381. nx = false,
  382. px = false,
  383. chartDestroyed = typeof chart.index === 'undefined',
  384. isYInside = true;
  385. if (!chartDestroyed) {
  386. if (useRaw) {
  387. x = d[0];
  388. y = d[1];
  389. if (sdata[i + 1]) {
  390. nx = sdata[i + 1][0];
  391. }
  392. if (sdata[i - 1]) {
  393. px = sdata[i - 1][0];
  394. }
  395. }
  396. else {
  397. x = d;
  398. y = yData[i];
  399. if (sdata[i + 1]) {
  400. nx = sdata[i + 1];
  401. }
  402. if (sdata[i - 1]) {
  403. px = sdata[i - 1];
  404. }
  405. }
  406. if (nx && nx >= xMin && nx <= xMax) {
  407. isNextInside = true;
  408. }
  409. if (px && px >= xMin && px <= xMax) {
  410. isPrevInside = true;
  411. }
  412. // Resolve low and high for range series
  413. if (isRange) {
  414. if (useRaw) {
  415. y = d.slice(1, 3);
  416. }
  417. low = y[0];
  418. y = y[1];
  419. }
  420. else if (isStacked) {
  421. x = d.x;
  422. y = d.stackY;
  423. low = y - d.y;
  424. }
  425. isNull = y === null;
  426. // Optimize for scatter zooming
  427. if (!requireSorting) {
  428. isYInside = y >= yMin && y <= yMax;
  429. }
  430. if (!isNull &&
  431. ((x >= xMin && x <= xMax && isYInside) ||
  432. (isNextInside || isPrevInside))) {
  433. clientX = Math.round(xAxis.toPixels(x, true));
  434. if (sampling) {
  435. if (typeof minI === 'undefined' ||
  436. clientX === lastClientX) {
  437. if (!isRange) {
  438. low = y;
  439. }
  440. if (typeof maxI === 'undefined' || y > maxVal) {
  441. maxVal = y;
  442. maxI = i;
  443. }
  444. if (typeof minI === 'undefined' ||
  445. low < minVal) {
  446. minVal = low;
  447. minI = i;
  448. }
  449. }
  450. // Add points and reset
  451. if (clientX !== lastClientX) {
  452. // maxI also a number:
  453. if (typeof minI !== 'undefined') {
  454. plotY = yAxis.toPixels(maxVal, true);
  455. yBottom = yAxis.toPixels(minVal, true);
  456. drawPoint(clientX, hasThreshold ?
  457. Math.min(plotY, translatedThreshold) : plotY, hasThreshold ?
  458. Math.max(yBottom, translatedThreshold) : yBottom, i);
  459. addKDPoint(clientX, plotY, maxI);
  460. if (yBottom !== plotY) {
  461. addKDPoint(clientX, yBottom, minI);
  462. }
  463. }
  464. minI = maxI = void 0;
  465. lastClientX = clientX;
  466. }
  467. }
  468. else {
  469. plotY = Math.round(yAxis.toPixels(y, true));
  470. drawPoint(clientX, plotY, yBottom, i);
  471. addKDPoint(clientX, plotY, i);
  472. }
  473. }
  474. wasNull = isNull && !connectNulls;
  475. if (i % CHUNK_SIZE === 0) {
  476. if (series.boostCopy || series.chart.boostCopy) {
  477. (series.boostCopy || series.chart.boostCopy)();
  478. }
  479. }
  480. }
  481. return !chartDestroyed;
  482. }, function () {
  483. var loadingDiv = chart.loadingDiv,
  484. loadingShown = chart.loadingShown;
  485. stroke();
  486. // if (series.boostCopy || series.chart.boostCopy) {
  487. // (series.boostCopy || series.chart.boostCopy)();
  488. // }
  489. series.canvasToSVG();
  490. if (boostSettings.timeRendering) {
  491. console.timeEnd('canvas rendering'); // eslint-disable-line no-console
  492. }
  493. fireEvent(series, 'renderedCanvas');
  494. // Do not use chart.hideLoading, as it runs JS animation and
  495. // will be blocked by buildKDTree. CSS animation looks good, but
  496. // then it must be deleted in timeout. If we add the module to
  497. // core, change hideLoading so we can skip this block.
  498. if (loadingShown) {
  499. extend(loadingDiv.style, {
  500. transition: 'opacity 250ms',
  501. opacity: 0
  502. });
  503. chart.loadingShown = false;
  504. destroyLoadingDiv = setTimeout(function () {
  505. if (loadingDiv.parentNode) { // In exporting it is falsy
  506. loadingDiv.parentNode.removeChild(loadingDiv);
  507. }
  508. chart.loadingDiv = chart.loadingSpan = null;
  509. }, 250);
  510. }
  511. // Go back to prototype, ready to build
  512. delete series.buildKDTree;
  513. series.buildKDTree();
  514. // Don't do async on export, the exportChart, getSVGForExport and
  515. // getSVG methods are not chained for it.
  516. }, chart.renderer.forExport ? Number.MAX_VALUE : void 0);
  517. }
  518. });
  519. seriesTypes.scatter.prototype.cvsMarkerCircle = function (ctx, clientX, plotY, r) {
  520. ctx.moveTo(clientX, plotY);
  521. ctx.arc(clientX, plotY, r, 0, 2 * Math.PI, false);
  522. };
  523. // Rect is twice as fast as arc, should be used for small markers
  524. seriesTypes.scatter.prototype.cvsMarkerSquare = function (ctx, clientX, plotY, r) {
  525. ctx.rect(clientX - r, plotY - r, r * 2, r * 2);
  526. };
  527. seriesTypes.scatter.prototype.fill = true;
  528. if (seriesTypes.bubble) {
  529. seriesTypes.bubble.prototype.cvsMarkerCircle = function (ctx, clientX, plotY, r, i) {
  530. ctx.moveTo(clientX, plotY);
  531. ctx.arc(clientX, plotY, this.radii && this.radii[i], 0, 2 * Math.PI, false);
  532. };
  533. seriesTypes.bubble.prototype.cvsStrokeBatch = 1;
  534. }
  535. extend(seriesTypes.area.prototype, {
  536. cvsDrawPoint: function (ctx, clientX, plotY, yBottom, lastPoint) {
  537. if (lastPoint && clientX !== lastPoint.clientX) {
  538. ctx.moveTo(lastPoint.clientX, lastPoint.yBottom);
  539. ctx.lineTo(lastPoint.clientX, lastPoint.plotY);
  540. ctx.lineTo(clientX, plotY);
  541. ctx.lineTo(clientX, yBottom);
  542. }
  543. },
  544. fill: true,
  545. fillOpacity: true,
  546. sampling: true
  547. });
  548. extend(seriesTypes.column.prototype, {
  549. cvsDrawPoint: function (ctx, clientX, plotY, yBottom) {
  550. ctx.rect(clientX - 1, plotY, 1, yBottom - plotY);
  551. },
  552. fill: true,
  553. sampling: true
  554. });
  555. Chart.prototype.callbacks.push(function (chart) {
  556. /**
  557. * @private
  558. */
  559. function canvasToSVG() {
  560. if (chart.boostCopy) {
  561. chart.boostCopy();
  562. }
  563. }
  564. /**
  565. * @private
  566. */
  567. function clear() {
  568. if (chart.renderTarget) {
  569. chart.renderTarget.attr({ href: '' });
  570. }
  571. if (chart.canvas) {
  572. chart.canvas.getContext('2d').clearRect(0, 0, chart.canvas.width, chart.canvas.height);
  573. }
  574. }
  575. addEvent(chart, 'predraw', clear);
  576. addEvent(chart, 'render', canvasToSVG);
  577. });
  578. };
  579. });
  580. _registerModule(_modules, 'masters/modules/boost-canvas.src.js', [], function () {
  581. });
  582. }));