boost-canvas.src.js 26 KB

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