exporting-image.src.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Client side exporting module
  5. *
  6. * (c) 2015-2019 Torstein Honsi / Oystein Moseng
  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('highcharts/modules/offline-exporting', ['highcharts', 'highcharts/modules/exporting'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Extensions/DownloadURL.js', [_modules['Core/Globals.js']], function (Highcharts) {
  32. /* *
  33. *
  34. * (c) 2015-2020 Oystein Moseng
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * Mixin for downloading content in the browser
  41. *
  42. * */
  43. var win = Highcharts.win,
  44. nav = win.navigator,
  45. doc = win.document,
  46. domurl = win.URL || win.webkitURL || win,
  47. isEdgeBrowser = /Edge\/\d+/.test(nav.userAgent);
  48. /**
  49. * Convert base64 dataURL to Blob if supported, otherwise returns undefined.
  50. * @private
  51. * @function Highcharts.dataURLtoBlob
  52. * @param {string} dataURL
  53. * URL to convert
  54. * @return {string|undefined}
  55. * Blob
  56. */
  57. var dataURLtoBlob = Highcharts.dataURLtoBlob = function (dataURL) {
  58. var parts = dataURL
  59. .replace(/filename=.*;/, '')
  60. .match(/data:([^;]*)(;base64)?,([0-9A-Za-z+/]+)/);
  61. if (parts &&
  62. parts.length > 3 &&
  63. win.atob &&
  64. win.ArrayBuffer &&
  65. win.Uint8Array &&
  66. win.Blob &&
  67. domurl.createObjectURL) {
  68. // Try to convert data URL to Blob
  69. var binStr = win.atob(parts[3]),
  70. buf = new win.ArrayBuffer(binStr.length),
  71. binary = new win.Uint8Array(buf);
  72. for (var i = 0; i < binary.length; ++i) {
  73. binary[i] = binStr.charCodeAt(i);
  74. }
  75. var blob = new win.Blob([binary], { 'type': parts[1] });
  76. return domurl.createObjectURL(blob);
  77. }
  78. };
  79. /**
  80. * Download a data URL in the browser. Can also take a blob as first param.
  81. *
  82. * @private
  83. * @function Highcharts.downloadURL
  84. * @param {string|global.URL} dataURL
  85. * The dataURL/Blob to download
  86. * @param {string} filename
  87. * The name of the resulting file (w/extension)
  88. * @return {void}
  89. */
  90. var downloadURL = Highcharts.downloadURL = function (dataURL,
  91. filename) {
  92. var a = doc.createElement('a'),
  93. windowRef;
  94. // IE specific blob implementation
  95. // Don't use for normal dataURLs
  96. if (typeof dataURL !== 'string' &&
  97. !(dataURL instanceof String) &&
  98. nav.msSaveOrOpenBlob) {
  99. nav.msSaveOrOpenBlob(dataURL, filename);
  100. return;
  101. }
  102. dataURL = "" + dataURL;
  103. // Some browsers have limitations for data URL lengths. Try to convert to
  104. // Blob or fall back. Edge always needs that blob.
  105. if (isEdgeBrowser || dataURL.length > 2000000) {
  106. dataURL = dataURLtoBlob(dataURL) || '';
  107. if (!dataURL) {
  108. throw new Error('Failed to convert to blob');
  109. }
  110. }
  111. // Try HTML5 download attr if supported
  112. if (typeof a.download !== 'undefined') {
  113. a.href = dataURL;
  114. a.download = filename; // HTML5 download attribute
  115. doc.body.appendChild(a);
  116. a.click();
  117. doc.body.removeChild(a);
  118. }
  119. else {
  120. // No download attr, just opening data URI
  121. try {
  122. windowRef = win.open(dataURL, 'chart');
  123. if (typeof windowRef === 'undefined' || windowRef === null) {
  124. throw new Error('Failed to open window');
  125. }
  126. }
  127. catch (e) {
  128. // window.open failed, trying location.href
  129. win.location.href = dataURL;
  130. }
  131. }
  132. };
  133. var exports = {
  134. dataURLtoBlob: dataURLtoBlob,
  135. downloadURL: downloadURL
  136. };
  137. return exports;
  138. });
  139. _registerModule(_modules, 'Extensions/OfflineExporting.js', [_modules['Core/Chart/Chart.js'], _modules['Core/Globals.js'], _modules['Core/Renderer/SVG/SVGRenderer.js'], _modules['Core/Utilities.js'], _modules['Extensions/DownloadURL.js']], function (Chart, H, SVGRenderer, U, DownloadURL) {
  140. /* *
  141. *
  142. * Client side exporting module
  143. *
  144. * (c) 2015 Torstein Honsi / Oystein Moseng
  145. *
  146. * License: www.highcharts.com/license
  147. *
  148. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  149. *
  150. * */
  151. var win = H.win,
  152. doc = H.doc;
  153. var addEvent = U.addEvent,
  154. error = U.error,
  155. extend = U.extend,
  156. getOptions = U.getOptions,
  157. merge = U.merge;
  158. var downloadURL = DownloadURL.downloadURL;
  159. var domurl = win.URL || win.webkitURL || win,
  160. nav = win.navigator,
  161. isMSBrowser = /Edge\/|Trident\/|MSIE /.test(nav.userAgent),
  162. // Milliseconds to defer image load event handlers to offset IE bug
  163. loadEventDeferDelay = isMSBrowser ? 150 : 0;
  164. var imageData;
  165. // Dummy object so we can reuse our canvas-tools.js without errors
  166. H.CanVGRenderer = {};
  167. /* eslint-disable valid-jsdoc */
  168. /**
  169. * Downloads a script and executes a callback when done.
  170. *
  171. * @private
  172. * @function getScript
  173. * @param {string} scriptLocation
  174. * @param {Function} callback
  175. * @return {void}
  176. */
  177. function getScript(scriptLocation, callback) {
  178. var head = doc.getElementsByTagName('head')[0], script = doc.createElement('script');
  179. script.type = 'text/javascript';
  180. script.src = scriptLocation;
  181. script.onload = callback;
  182. script.onerror = function () {
  183. error('Error loading script ' + scriptLocation);
  184. };
  185. head.appendChild(script);
  186. }
  187. /**
  188. * Get blob URL from SVG code. Falls back to normal data URI.
  189. *
  190. * @private
  191. * @function Highcharts.svgToDataURL
  192. * @param {string} svg
  193. * @return {string}
  194. */
  195. function svgToDataUrl(svg) {
  196. // Webkit and not chrome
  197. var webKit = (nav.userAgent.indexOf('WebKit') > -1 &&
  198. nav.userAgent.indexOf('Chrome') < 0);
  199. try {
  200. // Safari requires data URI since it doesn't allow navigation to blob
  201. // URLs. Firefox has an issue with Blobs and internal references,
  202. // leading to gradients not working using Blobs (#4550)
  203. if (!webKit && nav.userAgent.toLowerCase().indexOf('firefox') < 0) {
  204. return domurl.createObjectURL(new win.Blob([svg], {
  205. type: 'image/svg+xml;charset-utf-16'
  206. }));
  207. }
  208. }
  209. catch (e) {
  210. // Ignore
  211. }
  212. return 'data:image/svg+xml;charset=UTF-8,' + encodeURIComponent(svg);
  213. }
  214. /**
  215. * Get data:URL from image URL. Pass in callbacks to handle results.
  216. *
  217. * @private
  218. * @function Highcharts.imageToDataUrl
  219. *
  220. * @param {string} imageURL
  221. *
  222. * @param {string} imageType
  223. *
  224. * @param {*} callbackArgs
  225. * callbackArgs is used only by callbacks.
  226. *
  227. * @param {number} scale
  228. *
  229. * @param {Function} successCallback
  230. * Receives four arguments: imageURL, imageType, callbackArgs, and scale.
  231. *
  232. * @param {Function} taintedCallback
  233. * Receives four arguments: imageURL, imageType, callbackArgs, and scale.
  234. *
  235. * @param {Function} noCanvasSupportCallback
  236. * Receives four arguments: imageURL, imageType, callbackArgs, and scale.
  237. *
  238. * @param {Function} failedLoadCallback
  239. * Receives four arguments: imageURL, imageType, callbackArgs, and scale.
  240. *
  241. * @param {Function} [finallyCallback]
  242. * finallyCallback is always called at the end of the process. All
  243. * callbacks receive four arguments: imageURL, imageType, callbackArgs,
  244. * and scale.
  245. *
  246. * @return {void}
  247. */
  248. function imageToDataUrl(imageURL, imageType, callbackArgs, scale) {
  249. var img = new win.Image();
  250. img.onload = loadHandler;
  251. //img.onerror = errorHandler;
  252. img.src = imageURL;
  253. var canvas = doc.createElement('canvas'), ctx = canvas.getContext && canvas.getContext('2d'), dataURL;
  254. canvas.height = img.height * scale;
  255. canvas.width = img.width * scale;
  256. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  257. dataURL = canvas.toDataURL(imageType);
  258. imageData = dataURL;
  259. debugger;
  260. return dataURL;
  261. }
  262. Chart.prototype.getImageData = function ( exportingOptions, chartOptions) {
  263. var chart = this,
  264. options = merge(chart.options.exporting,
  265. exportingOptions),
  266. fallbackToExportServer = function (err) {
  267. if (options.fallbackToExportServer === false) {
  268. if (options.error) {
  269. options.error(options,
  270. err);
  271. }
  272. else {
  273. error(28, true); // Fallback disabled
  274. }
  275. }
  276. else {
  277. chart.exportChart(options);
  278. }
  279. },
  280. // Return true if the SVG contains images with external data. With the
  281. // boost module there are `image` elements with encoded PNGs, these are
  282. // supported by svg2pdf and should pass (#10243).
  283. hasExternalImages = function () {
  284. return [].some.call(chart.container.getElementsByTagName('image'), function (image) {
  285. var href = image.getAttribute('href');
  286. return href !== '' && href.indexOf('data:') !== 0;
  287. });
  288. };
  289. // If we are on IE and in styled mode, add a whitelist to the renderer for
  290. // inline styles that we want to pass through. There are so many styles by
  291. // default in IE that we don't want to blacklist them all.
  292. if (isMSBrowser && chart.styledMode) {
  293. SVGRenderer.prototype.inlineWhitelist = [
  294. /^blockSize/,
  295. /^border/,
  296. /^caretColor/,
  297. /^color/,
  298. /^columnRule/,
  299. /^columnRuleColor/,
  300. /^cssFloat/,
  301. /^cursor/,
  302. /^fill$/,
  303. /^fillOpacity/,
  304. /^font/,
  305. /^inlineSize/,
  306. /^length/,
  307. /^lineHeight/,
  308. /^opacity/,
  309. /^outline/,
  310. /^parentRule/,
  311. /^rx$/,
  312. /^ry$/,
  313. /^stroke/,
  314. /^textAlign/,
  315. /^textAnchor/,
  316. /^textDecoration/,
  317. /^transform/,
  318. /^vectorEffect/,
  319. /^visibility/,
  320. /^x$/,
  321. /^y$/
  322. ];
  323. }
  324. // Always fall back on:
  325. // - MS browsers: Embedded images JPEG/PNG, or any PDF
  326. // - Embedded images and PDF
  327. if ((isMSBrowser &&
  328. (options.type === 'application/pdf' ||
  329. chart.container.getElementsByTagName('image').length &&
  330. options.type !== 'image/svg+xml')) || (options.type === 'application/pdf' &&
  331. hasExternalImages())) {
  332. fallbackToExportServer('Image type not supported for this chart/browser.');
  333. return imageData;
  334. }
  335. var svg = chart.getSVG();
  336. //console.log(svg);
  337. //var svgUrl = svgToDataUrl(svg);
  338. //var imageType = options.type || 'image/png', scale = options.scale || 1;
  339. var imageUrl = 'data:image/svg+xml;charset=utf-8;base64,' + window.btoa(unescape(encodeURIComponent(svg)));
  340. return imageUrl;
  341. };
  342. // Extend the default options to use the local exporter logic
  343. merge(true, getOptions().exporting, {
  344. libURL: 'https://code.highcharts.com/8.2.2/lib/',
  345. // When offline-exporting is loaded, redefine the menu item definitions
  346. // related to download.
  347. menuItemDefinitions: {
  348. downloadPNG: {
  349. textKey: 'downloadPNG',
  350. onclick: function () {
  351. this.exportChartLocal();
  352. }
  353. },
  354. downloadJPEG: {
  355. textKey: 'downloadJPEG',
  356. onclick: function () {
  357. this.exportChartLocal({
  358. type: 'image/jpeg'
  359. });
  360. }
  361. },
  362. downloadSVG: {
  363. textKey: 'downloadSVG',
  364. onclick: function () {
  365. this.exportChartLocal({
  366. type: 'image/svg+xml'
  367. });
  368. }
  369. },
  370. downloadPDF: {
  371. textKey: 'downloadPDF',
  372. onclick: function () {
  373. this.exportChartLocal({
  374. type: 'application/pdf'
  375. });
  376. }
  377. }
  378. }
  379. });
  380. // Compatibility
  381. });
  382. _registerModule(_modules, 'masters/modules/offline-exporting.src.js', [], function () {
  383. });
  384. }));