exporting.src.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /**
  2. * Exporting module
  3. *
  4. * (c) 2010-2019 Torstein Honsi
  5. *
  6. * License: www.highcharts.com/license
  7. */
  8. /**
  9. * Gets fired after a chart is printed through the context menu item or the
  10. * Chart.print method.
  11. *
  12. * @callback Highcharts.ExportingAfterPrintCallbackFunction
  13. *
  14. * @param {Highcharts.Chart} chart
  15. * The chart on which the event occured.
  16. *
  17. * @param {global.Event} event
  18. * The event that occured.
  19. */
  20. /**
  21. * Gets fired before a chart is printed through the context menu item or the
  22. * Chart.print method.
  23. *
  24. * @callback Highcharts.ExportingBeforePrintCallbackFunction
  25. *
  26. * @param {Highcharts.Chart} chart
  27. * The chart on which the event occured.
  28. *
  29. * @param {global.Event} event
  30. * The event that occured.
  31. */
  32. /**
  33. * Function to call if the offline-exporting module fails to export a chart on
  34. * the client side.
  35. *
  36. * @callback Highcharts.ExportingErrorCallbackFunction
  37. *
  38. * @param {Highcharts.ExportingOptions} options
  39. * The exporting options.
  40. *
  41. * @param {global.Error} err
  42. * The error from the module.
  43. */
  44. /**
  45. * Definition for a menu item in the context menu.
  46. *
  47. * @interface Highcharts.ExportingMenuObject
  48. *//**
  49. * The text for the menu item.
  50. *
  51. * @name Highcharts.ExportingMenuObject#text
  52. * @type {string|undefined}
  53. *//**
  54. * If internationalization is required, the key to a language string.
  55. *
  56. * @name Highcharts.ExportingMenuObject#textKey
  57. * @type {string|undefined}
  58. *//**
  59. * The click handler for the menu item.
  60. *
  61. * @name Highcharts.ExportingMenuObject#onclick
  62. * @type {Function|undefined}
  63. *//**
  64. * Indicates a separator line instead of an item.
  65. *
  66. * @name Highcharts.ExportingMenuObject#separator
  67. * @type {boolean|undefined}
  68. */
  69. 'use strict';
  70. import H from '../parts/Globals.js';
  71. import '../parts/Utilities.js';
  72. import '../parts/Options.js';
  73. import '../parts/Chart.js';
  74. import chartNavigationMixin from '../mixins/navigation.js';
  75. // create shortcuts
  76. var defaultOptions = H.defaultOptions,
  77. doc = H.doc,
  78. Chart = H.Chart,
  79. addEvent = H.addEvent,
  80. removeEvent = H.removeEvent,
  81. fireEvent = H.fireEvent,
  82. createElement = H.createElement,
  83. discardElement = H.discardElement,
  84. css = H.css,
  85. merge = H.merge,
  86. pick = H.pick,
  87. objectEach = H.objectEach,
  88. extend = H.extend,
  89. isTouchDevice = H.isTouchDevice,
  90. win = H.win,
  91. userAgent = win.navigator.userAgent,
  92. SVGRenderer = H.SVGRenderer,
  93. symbols = H.Renderer.prototype.symbols,
  94. isMSBrowser = /Edge\/|Trident\/|MSIE /.test(userAgent),
  95. isFirefoxBrowser = /firefox/i.test(userAgent);
  96. // Add language
  97. extend(defaultOptions.lang
  98. /**
  99. * @optionparent lang
  100. */
  101. , {
  102. /**
  103. * Exporting module only. The text for the menu item to print the chart.
  104. *
  105. * @since 3.0.1
  106. */
  107. printChart: 'Print chart',
  108. /**
  109. * Exporting module only. The text for the PNG download menu item.
  110. *
  111. * @since 2.0
  112. */
  113. downloadPNG: 'Download PNG image',
  114. /**
  115. * Exporting module only. The text for the JPEG download menu item.
  116. *
  117. * @since 2.0
  118. */
  119. downloadJPEG: 'Download JPEG image',
  120. /**
  121. * Exporting module only. The text for the PDF download menu item.
  122. *
  123. * @since 2.0
  124. */
  125. downloadPDF: 'Download PDF document',
  126. /**
  127. * Exporting module only. The text for the SVG download menu item.
  128. *
  129. * @since 2.0
  130. */
  131. downloadSVG: 'Download SVG vector image',
  132. /**
  133. * Exporting module menu. The tooltip title for the context menu holding
  134. * print and export menu items.
  135. *
  136. * @since 3.0
  137. */
  138. contextButtonTitle: 'Chart context menu'
  139. });
  140. if (!defaultOptions.navigation) {
  141. // Buttons and menus are collected in a separate config option set called
  142. // 'navigation'. This can be extended later to add control buttons like
  143. // zoom and pan right click menus.
  144. /**
  145. * A collection of options for buttons and menus appearing in the exporting
  146. * module.
  147. *
  148. * @optionparent navigation
  149. */
  150. defaultOptions.navigation = {};
  151. }
  152. merge(true, defaultOptions.navigation, {
  153. buttonOptions: {
  154. theme: {},
  155. /**
  156. * Whether to enable buttons.
  157. *
  158. * @sample highcharts/navigation/buttonoptions-enabled/
  159. * Exporting module loaded but buttons disabled
  160. *
  161. * @type {boolean}
  162. * @default true
  163. * @since 2.0
  164. * @apioption navigation.buttonOptions.enabled
  165. */
  166. /**
  167. * The pixel size of the symbol on the button.
  168. *
  169. * @sample highcharts/navigation/buttonoptions-height/
  170. * Bigger buttons
  171. *
  172. * @since 2.0
  173. */
  174. symbolSize: 14,
  175. /**
  176. * The x position of the center of the symbol inside the button.
  177. *
  178. * @sample highcharts/navigation/buttonoptions-height/
  179. * Bigger buttons
  180. *
  181. * @since 2.0
  182. */
  183. symbolX: 12.5,
  184. /**
  185. * The y position of the center of the symbol inside the button.
  186. *
  187. * @sample highcharts/navigation/buttonoptions-height/
  188. * Bigger buttons
  189. *
  190. * @since 2.0
  191. */
  192. symbolY: 10.5,
  193. /**
  194. * Alignment for the buttons.
  195. *
  196. * @sample highcharts/navigation/buttonoptions-align/
  197. * Center aligned
  198. *
  199. * @type {Highcharts.AlignType}
  200. * @since 2.0
  201. */
  202. align: 'right',
  203. /**
  204. * The pixel spacing between buttons.
  205. *
  206. * @since 2.0
  207. */
  208. buttonSpacing: 3,
  209. /**
  210. * Pixel height of the buttons.
  211. *
  212. * @sample highcharts/navigation/buttonoptions-height/
  213. * Bigger buttons
  214. *
  215. * @since 2.0
  216. */
  217. height: 22,
  218. /**
  219. * A text string to add to the individual button.
  220. *
  221. * @sample highcharts/exporting/buttons-text/
  222. * Full text button
  223. * @sample highcharts/exporting/buttons-text-symbol/
  224. * Combined symbol and text
  225. *
  226. * @type {string}
  227. * @default null
  228. * @since 3.0
  229. * @apioption navigation.buttonOptions.text
  230. */
  231. /**
  232. * The vertical offset of the button's position relative to its
  233. * `verticalAlign`.
  234. *
  235. * @sample highcharts/navigation/buttonoptions-verticalalign/
  236. * Buttons at lower right
  237. *
  238. * @type {number}
  239. * @default 0
  240. * @since 2.0
  241. * @apioption navigation.buttonOptions.y
  242. */
  243. /**
  244. * The vertical alignment of the buttons. Can be one of "top", "middle"
  245. * or "bottom".
  246. *
  247. * @sample highcharts/navigation/buttonoptions-verticalalign/
  248. * Buttons at lower right
  249. *
  250. * @type {Highcharts.VerticalAlignType}
  251. * @since 2.0
  252. */
  253. verticalAlign: 'top',
  254. /**
  255. * The pixel width of the button.
  256. *
  257. * @sample highcharts/navigation/buttonoptions-height/
  258. * Bigger buttons
  259. *
  260. * @since 2.0
  261. */
  262. width: 24
  263. }
  264. });
  265. // Presentational attributes
  266. merge(true, defaultOptions.navigation
  267. /**
  268. * A collection of options for buttons and menus appearing in the exporting
  269. * module.
  270. *
  271. * @optionparent navigation
  272. */
  273. , {
  274. /**
  275. * CSS styles for the popup menu appearing by default when the export
  276. * icon is clicked. This menu is rendered in HTML.
  277. *
  278. * @see In styled mode, the menu is styled with the `.highcharts-menu`
  279. * class.
  280. *
  281. * @sample highcharts/navigation/menustyle/
  282. * Light gray menu background
  283. *
  284. * @type {Highcharts.CSSObject}
  285. * @default {"border": "1px solid #999999", "background": "#ffffff", "padding": "5px 0"}
  286. * @since 2.0
  287. */
  288. menuStyle: {
  289. /** @ignore-option */
  290. border: '1px solid #999999',
  291. /** @ignore-option */
  292. background: '#ffffff',
  293. /** @ignore-option */
  294. padding: '5px 0'
  295. },
  296. /**
  297. * CSS styles for the individual items within the popup menu appearing
  298. * by default when the export icon is clicked. The menu items are
  299. * rendered in HTML. Font size defaults to `11px` on desktop and `14px`
  300. * on touch devices.
  301. *
  302. * @see In styled mode, the menu items are styled with the
  303. * `.highcharts-menu-item` class.
  304. *
  305. * @sample {highcharts} highcharts/navigation/menuitemstyle/
  306. * Add a grey stripe to the left
  307. *
  308. * @type {Highcharts.CSSObject}
  309. * @default {"padding": "0.5em 1em", "color": "#333333", "background": "none", "fontSize": "11px/14px", "transition": "background 250ms, color 250ms"}
  310. * @since 2.0
  311. */
  312. menuItemStyle: {
  313. /** @ignore-option */
  314. padding: '0.5em 1em',
  315. /** @ignore-option */
  316. color: '#333333',
  317. /** @ignore-option */
  318. background: 'none',
  319. /** @ignore-option */
  320. fontSize: isTouchDevice ? '14px' : '11px',
  321. /** @ignore-option */
  322. transition: 'background 250ms, color 250ms'
  323. },
  324. /**
  325. * CSS styles for the hover state of the individual items within the
  326. * popup menu appearing by default when the export icon is clicked. The
  327. * menu items are rendered in HTML.
  328. *
  329. * @see In styled mode, the menu items are styled with the
  330. * `.highcharts-menu-item` class.
  331. *
  332. * @sample highcharts/navigation/menuitemhoverstyle/
  333. * Bold text on hover
  334. *
  335. * @type {Highcharts.CSSObject}
  336. * @default {"background": "#335cad", "color": "#ffffff"}
  337. * @since 2.0
  338. */
  339. menuItemHoverStyle: {
  340. /** @ignore-option */
  341. background: '#335cad',
  342. /** @ignore-option */
  343. color: '#ffffff'
  344. },
  345. /**
  346. * A collection of options for buttons appearing in the exporting
  347. * module.
  348. *
  349. * In styled mode, the buttons are styled with the
  350. * `.highcharts-contextbutton` and `.highcharts-button-symbol` classes.
  351. */
  352. buttonOptions: {
  353. /**
  354. * Fill color for the symbol within the button.
  355. *
  356. * @sample highcharts/navigation/buttonoptions-symbolfill/
  357. * Blue symbol stroke for one of the buttons
  358. *
  359. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  360. * @since 2.0
  361. */
  362. symbolFill: '#666666',
  363. /**
  364. * The color of the symbol's stroke or line.
  365. *
  366. * @sample highcharts/navigation/buttonoptions-symbolstroke/
  367. * Blue symbol stroke
  368. *
  369. * @type {Highcharts.ColorString}
  370. * @since 2.0
  371. */
  372. symbolStroke: '#666666',
  373. /**
  374. * The pixel stroke width of the symbol on the button.
  375. *
  376. * @sample highcharts/navigation/buttonoptions-height/
  377. * Bigger buttons
  378. *
  379. * @since 2.0
  380. */
  381. symbolStrokeWidth: 3,
  382. /**
  383. * A configuration object for the button theme. The object accepts
  384. * SVG properties like `stroke-width`, `stroke` and `fill`.
  385. * Tri-state button styles are supported by the `states.hover` and
  386. * `states.select` objects.
  387. *
  388. * @sample highcharts/navigation/buttonoptions-theme/
  389. * Theming the buttons
  390. *
  391. * @since 3.0
  392. */
  393. theme: {
  394. /**
  395. * The default fill exists only to capture hover events.
  396. *
  397. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  398. * @default #ffffff
  399. * @apioption navigation.buttonOptions.theme.fill
  400. */
  401. /**
  402. * Default stroke for the buttons.
  403. * @type {Highcharts.ColorString}
  404. * @default none
  405. * @apioption navigation.buttonOptions.theme.stroke
  406. */
  407. /**
  408. * Padding for the button.
  409. */
  410. padding: 5
  411. }
  412. }
  413. });
  414. // Add the export related options
  415. /**
  416. * Options for the exporting module. For an overview on the matter, see
  417. * [the docs](https://www.highcharts.com/docs/export-module/export-module-overview).
  418. *
  419. * @optionparent exporting
  420. */
  421. defaultOptions.exporting = {
  422. /**
  423. * Experimental setting to allow HTML inside the chart (added through
  424. * the `useHTML` options), directly in the exported image. This allows
  425. * you to preserve complicated HTML structures like tables or bi-directional
  426. * text in exported charts.
  427. *
  428. * Disclaimer: The HTML is rendered in a `foreignObject` tag in the
  429. * generated SVG. The official export server is based on PhantomJS,
  430. * which supports this, but other SVG clients, like Batik, does not
  431. * support it. This also applies to downloaded SVG that you want to
  432. * open in a desktop client.
  433. *
  434. * @type {boolean}
  435. * @default false
  436. * @since 4.1.8
  437. * @apioption exporting.allowHTML
  438. */
  439. /**
  440. * Additional chart options to be merged into an exported chart. For
  441. * example, a common use case is to add data labels to improve readability
  442. * of the exported chart, or to add a printer-friendly color scheme.
  443. *
  444. * @sample {highcharts} highcharts/exporting/chartoptions-data-labels/
  445. * Added data labels
  446. * @sample {highstock} highcharts/exporting/chartoptions-data-labels/
  447. * Added data labels
  448. *
  449. * @type {Highcharts.Options}
  450. * @apioption exporting.chartOptions
  451. */
  452. /**
  453. * Whether to enable the exporting module. Disabling the module will
  454. * hide the context button, but API methods will still be available.
  455. *
  456. * @sample {highcharts} highcharts/exporting/enabled-false/
  457. * Exporting module is loaded but disabled
  458. * @sample {highstock} highcharts/exporting/enabled-false/
  459. * Exporting module is loaded but disabled
  460. *
  461. * @type {boolean}
  462. * @default true
  463. * @since 2.0
  464. * @apioption exporting.enabled
  465. */
  466. /**
  467. * Function to call if the offline-exporting module fails to export
  468. * a chart on the client side, and [fallbackToExportServer](
  469. * #exporting.fallbackToExportServer) is disabled. If left undefined, an
  470. * exception is thrown instead. Receives two parameters, the exporting
  471. * options, and the error from the module.
  472. *
  473. * @see [fallbackToExportServer](#exporting.fallbackToExportServer)
  474. *
  475. * @type {Highcharts.ExportingErrorCallbackFunction}
  476. * @since 5.0.0
  477. * @apioption exporting.error
  478. */
  479. /**
  480. * Whether or not to fall back to the export server if the offline-exporting
  481. * module is unable to export the chart on the client side. This happens for
  482. * certain browsers, and certain features (e.g.
  483. * [allowHTML](#exporting.allowHTML)), depending on the image type exporting
  484. * to. For very complex charts, it is possible that export can fail in
  485. * browsers that don't support Blob objects, due to data URL length limits.
  486. * It is recommended to define the [exporting.error](#exporting.error)
  487. * handler if disabling fallback, in order to notify users in case export
  488. * fails.
  489. *
  490. * @type {boolean}
  491. * @default true
  492. * @since 4.1.8
  493. * @apioption exporting.fallbackToExportServer
  494. */
  495. /**
  496. * The filename, without extension, to use for the exported chart.
  497. *
  498. * @sample {highcharts} highcharts/exporting/filename/
  499. * Custom file name
  500. * @sample {highstock} highcharts/exporting/filename/
  501. * Custom file name
  502. *
  503. * @type {string}
  504. * @default chart
  505. * @since 2.0
  506. * @apioption exporting.filename
  507. */
  508. /**
  509. * An object containing additional key value data for the POST form that
  510. * sends the SVG to the export server. For example, a `target` can be set to
  511. * make sure the generated image is received in another frame, or a custom
  512. * `enctype` or `encoding` can be set.
  513. *
  514. * @type {*}
  515. * @since 3.0.8
  516. * @apioption exporting.formAttributes
  517. */
  518. /**
  519. * Path where Highcharts will look for export module dependencies to
  520. * load on demand if they don't already exist on `window`. Should currently
  521. * point to location of [CanVG](https://github.com/canvg/canvg) library,
  522. * [RGBColor.js](https://github.com/canvg/canvg),
  523. * [jsPDF](https://github.com/yWorks/jsPDF) and
  524. * [svg2pdf.js](https://github.com/yWorks/svg2pdf.js), required for client
  525. * side export in certain browsers.
  526. *
  527. * @type {string}
  528. * @default https://code.highcharts.com/{version}/lib
  529. * @since 5.0.0
  530. * @apioption exporting.libURL
  531. */
  532. /**
  533. * Analogous to [sourceWidth](#exporting.sourceWidth).
  534. *
  535. * @type {number}
  536. * @since 3.0
  537. * @apioption exporting.sourceHeight
  538. */
  539. /**
  540. * The width of the original chart when exported, unless an explicit
  541. * [chart.width](#chart.width) is set, or a pixel width is set on the
  542. * container. The width exported raster image is then multiplied by
  543. * [scale](#exporting.scale).
  544. *
  545. * @sample {highcharts} highcharts/exporting/sourcewidth/
  546. * Source size demo
  547. * @sample {highstock} highcharts/exporting/sourcewidth/
  548. * Source size demo
  549. * @sample {highmaps} maps/exporting/sourcewidth/
  550. * Source size demo
  551. *
  552. * @type {number}
  553. * @since 3.0
  554. * @apioption exporting.sourceWidth
  555. */
  556. /**
  557. * The pixel width of charts exported to PNG or JPG. As of Highcharts
  558. * 3.0, the default pixel width is a function of the [chart.width](
  559. * #chart.width) or [exporting.sourceWidth](#exporting.sourceWidth) and the
  560. * [exporting.scale](#exporting.scale).
  561. *
  562. * @sample {highcharts} highcharts/exporting/width/
  563. * Export to 200px wide images
  564. * @sample {highstock} highcharts/exporting/width/
  565. * Export to 200px wide images
  566. *
  567. * @type {number}
  568. * @since 2.0
  569. * @apioption exporting.width
  570. */
  571. /**
  572. * Default MIME type for exporting if `chart.exportChart()` is called
  573. * without specifying a `type` option. Possible values are `image/png`,
  574. * `image/jpeg`, `application/pdf` and `image/svg+xml`.
  575. *
  576. * @since 2.0
  577. * @validvalue ["image/png", "image/jpeg", "application/pdf", "image/svg+xml"]
  578. */
  579. type: 'image/png',
  580. /**
  581. * The URL for the server module converting the SVG string to an image
  582. * format. By default this points to Highchart's free web service.
  583. *
  584. * @since 2.0
  585. */
  586. url: 'https://export.highcharts.com/',
  587. /**
  588. * When printing the chart from the menu item in the burger menu, if
  589. * the on-screen chart exceeds this width, it is resized. After printing
  590. * or cancelled, it is restored. The default width makes the chart
  591. * fit into typical paper format. Note that this does not affect the
  592. * chart when printing the web page as a whole.
  593. *
  594. * @since 4.2.5
  595. */
  596. printMaxWidth: 780,
  597. /**
  598. * Defines the scale or zoom factor for the exported image compared
  599. * to the on-screen display. While for instance a 600px wide chart
  600. * may look good on a website, it will look bad in print. The default
  601. * scale of 2 makes this chart export to a 1200px PNG or JPG.
  602. *
  603. * @see [chart.width](#chart.width)
  604. * @see [exporting.sourceWidth](#exporting.sourceWidth)
  605. *
  606. * @sample {highcharts} highcharts/exporting/scale/
  607. * Scale demonstrated
  608. * @sample {highstock} highcharts/exporting/scale/
  609. * Scale demonstrated
  610. * @sample {highmaps} maps/exporting/scale/
  611. * Scale demonstrated
  612. *
  613. * @since 3.0
  614. */
  615. scale: 2,
  616. /**
  617. * Options for the export related buttons, print and export. In addition
  618. * to the default buttons listed here, custom buttons can be added.
  619. * See [navigation.buttonOptions](#navigation.buttonOptions) for general
  620. * options.
  621. *
  622. * @type {Highcharts.Dictionary<Highcharts.ExportingButtonsContextButtonOptions>}
  623. */
  624. buttons: {
  625. /**
  626. * Options for the export button.
  627. *
  628. * In styled mode, export button styles can be applied with the
  629. * `.highcharts-contextbutton` class.
  630. *
  631. * @extends navigation.buttonOptions
  632. */
  633. contextButton: {
  634. /**
  635. * A click handler callback to use on the button directly instead of
  636. * the popup menu.
  637. *
  638. * @sample highcharts/exporting/buttons-contextbutton-onclick/
  639. * Skip the menu and export the chart directly
  640. *
  641. * @type {Function}
  642. * @since 2.0
  643. * @apioption exporting.buttons.contextButton.onclick
  644. */
  645. /**
  646. * See [navigation.buttonOptions.symbolFill](
  647. * #navigation.buttonOptions.symbolFill).
  648. *
  649. * @type {Highcharts.ColorString}
  650. * @default #666666
  651. * @since 2.0
  652. * @apioption exporting.buttons.contextButton.symbolFill
  653. */
  654. /**
  655. * The horizontal position of the button relative to the `align`
  656. * option.
  657. *
  658. * @type {number}
  659. * @default -10
  660. * @since 2.0
  661. * @apioption exporting.buttons.contextButton.x
  662. */
  663. /**
  664. * The class name of the context button.
  665. */
  666. className: 'highcharts-contextbutton',
  667. /**
  668. * The class name of the menu appearing from the button.
  669. */
  670. menuClassName: 'highcharts-contextmenu',
  671. /**
  672. * The symbol for the button. Points to a definition function in
  673. * the `Highcharts.Renderer.symbols` collection. The default
  674. * `exportIcon` function is part of the exporting module. Possible
  675. * values are "circle", "square", "diamond", "triangle",
  676. * "triangle-down", "menu", "menuball" or custom shape.
  677. *
  678. * @sample highcharts/exporting/buttons-contextbutton-symbol/
  679. * Use a circle for symbol
  680. * @sample highcharts/exporting/buttons-contextbutton-symbol-custom/
  681. * Custom shape as symbol
  682. *
  683. * @since 2.0
  684. * @validvalue ["menu", "menuball", "exportIcon", "circle", "square", "diamond", "triangle", "triangle-down"]
  685. */
  686. symbol: 'menu',
  687. /**
  688. * The key to a [lang](#lang) option setting that is used for the
  689. * button's title tooltip. When the key is `contextButtonTitle`, it
  690. * refers to [lang.contextButtonTitle](#lang.contextButtonTitle)
  691. * that defaults to "Chart context menu".
  692. *
  693. * @since 6.1.4
  694. */
  695. titleKey: 'contextButtonTitle',
  696. /**
  697. * This option is deprecated, use
  698. * [titleKey](#exporting.buttons.contextButton.titleKey) instead.
  699. *
  700. * @deprecated
  701. * @type {string}
  702. * @apioption exporting.buttons.contextButton._titleKey
  703. */
  704. /**
  705. * A collection of strings pointing to config options for the menu
  706. * items. The config options are defined in the
  707. * `menuItemDefinitions` option.
  708. *
  709. * By default, there is the "Print" menu item plus one menu item
  710. * for each of the available export types.
  711. *
  712. * @sample {highcharts} highcharts/exporting/menuitemdefinitions/
  713. * Menu item definitions
  714. * @sample {highstock} highcharts/exporting/menuitemdefinitions/
  715. * Menu item definitions
  716. * @sample {highmaps} highcharts/exporting/menuitemdefinitions/
  717. * Menu item definitions
  718. *
  719. * @type {Array<string>}
  720. * @default ["printChart", "separator", "downloadPNG", "downloadJPEG", "downloadPDF", "downloadSVG"]
  721. * @since 2.0
  722. */
  723. menuItems: [
  724. 'printChart',
  725. 'separator',
  726. 'downloadPNG',
  727. 'downloadJPEG',
  728. 'downloadPDF',
  729. 'downloadSVG'
  730. ]
  731. }
  732. },
  733. /**
  734. * An object consisting of definitions for the menu items in the context
  735. * menu. Each key value pair has a `key` that is referenced in the
  736. * [menuItems](#exporting.buttons.contextButton.menuItems) setting,
  737. * and a `value`, which is an object with the following properties:
  738. *
  739. * - **onclick:** The click handler for the menu item
  740. *
  741. * - **text:** The text for the menu item
  742. *
  743. * - **textKey:** If internationalization is required, the key to a language
  744. * string
  745. *
  746. * @sample {highcharts} highcharts/exporting/menuitemdefinitions/
  747. * Menu item definitions
  748. * @sample {highstock} highcharts/exporting/menuitemdefinitions/
  749. * Menu item definitions
  750. * @sample {highmaps} highcharts/exporting/menuitemdefinitions/
  751. * Menu item definitions
  752. *
  753. * @type {Highcharts.Dictionary<Highcharts.ExportingMenuObject>}
  754. * @default {"printChart": {}, "separator": {}, "downloadPNG": {}, "downloadJPEG": {}, "downloadPDF": {}, "downloadSVG": {}}
  755. * @since 5.0.13
  756. */
  757. menuItemDefinitions: {
  758. /**
  759. * @ignore
  760. */
  761. printChart: {
  762. textKey: 'printChart',
  763. onclick: function () {
  764. this.print();
  765. }
  766. },
  767. /**
  768. * @ignore
  769. */
  770. separator: {
  771. separator: true
  772. },
  773. /**
  774. * @ignore
  775. */
  776. downloadPNG: {
  777. textKey: 'downloadPNG',
  778. onclick: function () {
  779. this.exportChart();
  780. }
  781. },
  782. /**
  783. * @ignore
  784. */
  785. downloadJPEG: {
  786. textKey: 'downloadJPEG',
  787. onclick: function () {
  788. this.exportChart({
  789. type: 'image/jpeg'
  790. });
  791. }
  792. },
  793. /**
  794. * @ignore
  795. */
  796. downloadPDF: {
  797. textKey: 'downloadPDF',
  798. onclick: function () {
  799. this.exportChart({
  800. type: 'application/pdf'
  801. });
  802. }
  803. },
  804. /**
  805. * @ignore
  806. */
  807. downloadSVG: {
  808. textKey: 'downloadSVG',
  809. onclick: function () {
  810. this.exportChart({
  811. type: 'image/svg+xml'
  812. });
  813. }
  814. }
  815. }
  816. };
  817. /**
  818. * Fires after a chart is printed through the context menu item or the
  819. * `Chart.print` method. Requires the exporting module.
  820. *
  821. * @sample highcharts/chart/events-beforeprint-afterprint/
  822. * Rescale the chart to print
  823. *
  824. * @type {Highcharts.ExportingAfterPrintCallbackFunction}
  825. * @since 4.1.0
  826. * @context Highcharts.Chart
  827. * @apioption chart.events.afterPrint
  828. */
  829. /**
  830. * Fires before a chart is printed through the context menu item or
  831. * the `Chart.print` method. Requires the exporting module.
  832. *
  833. * @sample highcharts/chart/events-beforeprint-afterprint/
  834. * Rescale the chart to print
  835. *
  836. * @type {Highcharts.ExportingBeforePrintCallbackFunction}
  837. * @since 4.1.0
  838. * @context Highcharts.Chart
  839. * @apioption chart.events.beforePrint
  840. */
  841. /**
  842. * The post utility
  843. *
  844. * @private
  845. * @function Highcharts.post
  846. *
  847. * @param {string} url
  848. *
  849. * @param {*} data
  850. *
  851. * @param {*} formAttributes
  852. */
  853. H.post = function (url, data, formAttributes) {
  854. // create the form
  855. var form = createElement('form', merge({
  856. method: 'post',
  857. action: url,
  858. enctype: 'multipart/form-data'
  859. }, formAttributes), {
  860. display: 'none'
  861. }, doc.body);
  862. // add the data
  863. objectEach(data, function (val, name) {
  864. createElement('input', {
  865. type: 'hidden',
  866. name: name,
  867. value: val
  868. }, null, form);
  869. });
  870. // submit
  871. form.submit();
  872. // clean up
  873. discardElement(form);
  874. };
  875. extend(Chart.prototype, /** @lends Highcharts.Chart.prototype */ {
  876. /**
  877. * Exporting module only. A collection of fixes on the produced SVG to
  878. * account for expando properties, browser bugs, VML problems and other.
  879. * Returns a cleaned SVG.
  880. *
  881. * @private
  882. * @function Highcharts.Chart#sanitizeSVG
  883. *
  884. * @param {string} svg
  885. *
  886. * @param {Highcharts.Options} options
  887. *
  888. * @return {string}
  889. */
  890. sanitizeSVG: function (svg, options) {
  891. // Move HTML into a foreignObject
  892. if (options && options.exporting && options.exporting.allowHTML) {
  893. var html = svg.match(/<\/svg>(.*?$)/);
  894. if (html && html[1]) {
  895. html = '<foreignObject x="0" y="0" ' +
  896. 'width="' + options.chart.width + '" ' +
  897. 'height="' + options.chart.height + '">' +
  898. '<body xmlns="http://www.w3.org/1999/xhtml">' +
  899. html[1] +
  900. '</body>' +
  901. '</foreignObject>';
  902. svg = svg.replace('</svg>', html + '</svg>');
  903. }
  904. }
  905. svg = svg
  906. .replace(/zIndex="[^"]+"/g, '')
  907. .replace(/symbolName="[^"]+"/g, '')
  908. .replace(/jQuery[0-9]+="[^"]+"/g, '')
  909. .replace(/url\(("|&quot;)(\S+)("|&quot;)\)/g, 'url($2)')
  910. .replace(/url\([^#]+#/g, 'url(#')
  911. .replace(
  912. /<svg /,
  913. '<svg xmlns:xlink="http://www.w3.org/1999/xlink" '
  914. )
  915. .replace(/ (|NS[0-9]+\:)href=/g, ' xlink:href=') // #3567
  916. .replace(/\n/, ' ')
  917. // Any HTML added to the container after the SVG (#894)
  918. .replace(/<\/svg>.*?$/, '</svg>')
  919. // Batik doesn't support rgba fills and strokes (#3095)
  920. .replace(
  921. /(fill|stroke)="rgba\(([ 0-9]+,[ 0-9]+,[ 0-9]+),([ 0-9\.]+)\)"/g, // eslint-disable-line max-len
  922. '$1="rgb($2)" $1-opacity="$3"'
  923. )
  924. // Replace HTML entities, issue #347
  925. .replace(/&nbsp;/g, '\u00A0') // no-break space
  926. .replace(/&shy;/g, '\u00AD'); // soft hyphen
  927. // Further sanitize for oldIE
  928. if (this.ieSanitizeSVG) {
  929. svg = this.ieSanitizeSVG(svg);
  930. }
  931. return svg;
  932. },
  933. /**
  934. * Return the unfiltered innerHTML of the chart container. Used as hook for
  935. * plugins. In styled mode, it also takes care of inlining CSS style rules.
  936. *
  937. * @see Chart#getSVG
  938. *
  939. * @function Highcharts.Chart#getChartHTML
  940. *
  941. * @returns {string}
  942. * The unfiltered SVG of the chart.
  943. */
  944. getChartHTML: function () {
  945. if (this.styledMode) {
  946. this.inlineStyles();
  947. }
  948. return this.container.innerHTML;
  949. },
  950. /**
  951. * Return an SVG representation of the chart.
  952. *
  953. * @sample highcharts/members/chart-getsvg/
  954. * View the SVG from a button
  955. *
  956. * @function Highcharts.Chart#getSVG
  957. *
  958. * @param {Highcharts.Options} chartOptions
  959. * Additional chart options for the generated SVG representation. For
  960. * collections like `xAxis`, `yAxis` or `series`, the additional
  961. * options is either merged in to the orininal item of the same
  962. * `id`, or to the first item if a common id is not found.
  963. *
  964. * @return {string}
  965. * The SVG representation of the rendered chart.
  966. *
  967. * @fires Highcharts.Chart#event:getSVG
  968. */
  969. getSVG: function (chartOptions) {
  970. var chart = this,
  971. chartCopy,
  972. sandbox,
  973. svg,
  974. seriesOptions,
  975. sourceWidth,
  976. sourceHeight,
  977. cssWidth,
  978. cssHeight,
  979. // Copy the options and add extra options
  980. options = merge(chart.options, chartOptions);
  981. // create a sandbox where a new chart will be generated
  982. sandbox = createElement('div', null, {
  983. position: 'absolute',
  984. top: '-9999em',
  985. width: chart.chartWidth + 'px',
  986. height: chart.chartHeight + 'px'
  987. }, doc.body);
  988. // get the source size
  989. cssWidth = chart.renderTo.style.width;
  990. cssHeight = chart.renderTo.style.height;
  991. sourceWidth = options.exporting.sourceWidth ||
  992. options.chart.width ||
  993. (/px$/.test(cssWidth) && parseInt(cssWidth, 10)) ||
  994. (options.isGantt ? 800 : 600);
  995. sourceHeight = options.exporting.sourceHeight ||
  996. options.chart.height ||
  997. (/px$/.test(cssHeight) && parseInt(cssHeight, 10)) ||
  998. 400;
  999. // override some options
  1000. extend(options.chart, {
  1001. animation: false,
  1002. renderTo: sandbox,
  1003. forExport: true,
  1004. renderer: 'SVGRenderer',
  1005. width: sourceWidth,
  1006. height: sourceHeight
  1007. });
  1008. options.exporting.enabled = false; // hide buttons in print
  1009. delete options.data; // #3004
  1010. // prepare for replicating the chart
  1011. options.series = [];
  1012. chart.series.forEach(function (serie) {
  1013. seriesOptions = merge(serie.userOptions, { // #4912
  1014. animation: false, // turn off animation
  1015. enableMouseTracking: false,
  1016. showCheckbox: false,
  1017. visible: serie.visible
  1018. });
  1019. // Used for the navigator series that has its own option set
  1020. if (!seriesOptions.isInternal) {
  1021. options.series.push(seriesOptions);
  1022. }
  1023. });
  1024. // Assign an internal key to ensure a one-to-one mapping (#5924)
  1025. chart.axes.forEach(function (axis) {
  1026. if (!axis.userOptions.internalKey) { // #6444
  1027. axis.userOptions.internalKey = H.uniqueKey();
  1028. }
  1029. });
  1030. // generate the chart copy
  1031. chartCopy = new H.Chart(options, chart.callback);
  1032. // Axis options and series options (#2022, #3900, #5982)
  1033. if (chartOptions) {
  1034. ['xAxis', 'yAxis', 'series'].forEach(function (coll) {
  1035. var collOptions = {};
  1036. if (chartOptions[coll]) {
  1037. collOptions[coll] = chartOptions[coll];
  1038. chartCopy.update(collOptions);
  1039. }
  1040. });
  1041. }
  1042. // Reflect axis extremes in the export (#5924)
  1043. chart.axes.forEach(function (axis) {
  1044. var axisCopy = H.find(chartCopy.axes, function (copy) {
  1045. return copy.options.internalKey ===
  1046. axis.userOptions.internalKey;
  1047. }),
  1048. extremes = axis.getExtremes(),
  1049. userMin = extremes.userMin,
  1050. userMax = extremes.userMax;
  1051. if (
  1052. axisCopy &&
  1053. (
  1054. (userMin !== undefined && userMin !== axisCopy.min) ||
  1055. (userMax !== undefined && userMax !== axisCopy.max)
  1056. )
  1057. ) {
  1058. axisCopy.setExtremes(userMin, userMax, true, false);
  1059. }
  1060. });
  1061. // Get the SVG from the container's innerHTML
  1062. svg = chartCopy.getChartHTML();
  1063. fireEvent(this, 'getSVG', { chartCopy: chartCopy });
  1064. svg = chart.sanitizeSVG(svg, options);
  1065. // free up memory
  1066. options = null;
  1067. chartCopy.destroy();
  1068. discardElement(sandbox);
  1069. return svg;
  1070. },
  1071. /**
  1072. * @private
  1073. * @function Highcharts.Chart#getSVGForExport
  1074. *
  1075. * @param {Highcharts.ExportingOptions} options
  1076. *
  1077. * @param {Highcharts.Options} chartOptions
  1078. *
  1079. * @return {string}
  1080. */
  1081. getSVGForExport: function (options, chartOptions) {
  1082. var chartExportingOptions = this.options.exporting;
  1083. return this.getSVG(merge(
  1084. { chart: { borderRadius: 0 } },
  1085. chartExportingOptions.chartOptions,
  1086. chartOptions,
  1087. {
  1088. exporting: {
  1089. sourceWidth: (
  1090. (options && options.sourceWidth) ||
  1091. chartExportingOptions.sourceWidth
  1092. ),
  1093. sourceHeight: (
  1094. (options && options.sourceHeight) ||
  1095. chartExportingOptions.sourceHeight
  1096. )
  1097. }
  1098. }
  1099. ));
  1100. },
  1101. /**
  1102. * Get the default file name used for exported charts. By default it creates
  1103. * a file name based on the chart title.
  1104. *
  1105. * @function Highcharts.Chart#getFilename
  1106. *
  1107. * @return {string} A file name without extension.
  1108. */
  1109. getFilename: function () {
  1110. var s = this.userOptions.title && this.userOptions.title.text,
  1111. filename = this.options.exporting.filename;
  1112. if (filename) {
  1113. return filename;
  1114. }
  1115. if (typeof s === 'string') {
  1116. filename = s
  1117. .toLowerCase()
  1118. .replace(/<\/?[^>]+(>|$)/g, '') // strip HTML tags
  1119. .replace(/[\s_]+/g, '-')
  1120. .replace(/[^a-z0-9\-]/g, '') // preserve only latin
  1121. .replace(/^[\-]+/g, '') // dashes in the start
  1122. .replace(/[\-]+/g, '-') // dashes in a row
  1123. .substr(0, 24)
  1124. .replace(/[\-]+$/g, ''); // dashes in the end;
  1125. }
  1126. if (!filename || filename.length < 5) {
  1127. filename = 'chart';
  1128. }
  1129. return filename;
  1130. },
  1131. /**
  1132. * Exporting module required. Submit an SVG version of the chart to a server
  1133. * along with some parameters for conversion.
  1134. *
  1135. * @sample highcharts/members/chart-exportchart/
  1136. * Export with no options
  1137. * @sample highcharts/members/chart-exportchart-filename/
  1138. * PDF type and custom filename
  1139. * @sample highcharts/members/chart-exportchart-custom-background/
  1140. * Different chart background in export
  1141. * @sample stock/members/chart-exportchart/
  1142. * Export with Highstock
  1143. *
  1144. * @function Highcharts.Chart#exportChart
  1145. *
  1146. * @param {Highcharts.ExportingOptions} exportingOptions
  1147. * Exporting options in addition to those defined in
  1148. * [exporting](https://api.highcharts.com/highcharts/exporting).
  1149. *
  1150. * @param {Highcharts.Options} chartOptions
  1151. * Additional chart options for the exported chart. For example a
  1152. * different background color can be added here, or `dataLabels` for
  1153. * export only.
  1154. */
  1155. exportChart: function (exportingOptions, chartOptions) {
  1156. var svg = this.getSVGForExport(exportingOptions, chartOptions);
  1157. // merge the options
  1158. exportingOptions = merge(this.options.exporting, exportingOptions);
  1159. // do the post
  1160. H.post(exportingOptions.url, {
  1161. filename: exportingOptions.filename || this.getFilename(),
  1162. type: exportingOptions.type,
  1163. // IE8 fails to post undefined correctly, so use 0
  1164. width: exportingOptions.width || 0,
  1165. scale: exportingOptions.scale,
  1166. svg: svg
  1167. }, exportingOptions.formAttributes);
  1168. },
  1169. /**
  1170. * Exporting module required. Clears away other elements in the page and
  1171. * prints the chart as it is displayed. By default, when the exporting
  1172. * module is enabled, a context button with a drop down menu in the upper
  1173. * right corner accesses this function.
  1174. *
  1175. * @sample highcharts/members/chart-print/
  1176. * Print from a HTML button
  1177. *
  1178. * @function Highcharts.Chart#print
  1179. *
  1180. * @fires Highcharts.Chart#event:beforePrint
  1181. * @fires Highcharts.Chart#event:afterPrint
  1182. */
  1183. print: function () {
  1184. var chart = this,
  1185. origDisplay = [],
  1186. body = doc.body,
  1187. childNodes = body.childNodes,
  1188. printMaxWidth = chart.options.exporting.printMaxWidth,
  1189. resetParams,
  1190. handleMaxWidth;
  1191. // Move the chart container(s) to another div
  1192. function moveContainers(moveTo) {
  1193. (
  1194. chart.fixedDiv ? // When scrollablePlotArea is active (#9533)
  1195. [chart.fixedDiv, chart.scrollingContainer] :
  1196. [chart.container]
  1197. ).forEach(function (div) {
  1198. moveTo.appendChild(div);
  1199. });
  1200. }
  1201. if (chart.isPrinting) { // block the button while in printing mode
  1202. return;
  1203. }
  1204. chart.isPrinting = true;
  1205. chart.pointer.reset(null, 0);
  1206. fireEvent(chart, 'beforePrint');
  1207. // Handle printMaxWidth
  1208. handleMaxWidth = printMaxWidth && chart.chartWidth > printMaxWidth;
  1209. if (handleMaxWidth) {
  1210. resetParams = [chart.options.chart.width, undefined, false];
  1211. chart.setSize(printMaxWidth, undefined, false);
  1212. }
  1213. // hide all body content
  1214. childNodes.forEach(function (node, i) {
  1215. if (node.nodeType === 1) {
  1216. origDisplay[i] = node.style.display;
  1217. node.style.display = 'none';
  1218. }
  1219. });
  1220. // pull out the chart
  1221. moveContainers(body);
  1222. // Give the browser time to draw WebGL content, an issue that randomly
  1223. // appears (at least) in Chrome ~67 on the Mac (#8708).
  1224. setTimeout(function () {
  1225. win.focus(); // #1510
  1226. win.print();
  1227. // allow the browser to prepare before reverting
  1228. setTimeout(function () {
  1229. // put the chart back in
  1230. moveContainers(chart.renderTo);
  1231. // restore all body content
  1232. childNodes.forEach(function (node, i) {
  1233. if (node.nodeType === 1) {
  1234. node.style.display = origDisplay[i];
  1235. }
  1236. });
  1237. chart.isPrinting = false;
  1238. // Reset printMaxWidth
  1239. if (handleMaxWidth) {
  1240. chart.setSize.apply(chart, resetParams);
  1241. }
  1242. fireEvent(chart, 'afterPrint');
  1243. }, 1000);
  1244. }, 1);
  1245. },
  1246. /**
  1247. * Display a popup menu for choosing the export type.
  1248. *
  1249. * @private
  1250. * @function Highcharts.Chart#contextMenu
  1251. *
  1252. * @param {string} className
  1253. * An identifier for the menu.
  1254. *
  1255. * @param {Array<string|Highcharts.ExportingMenuObject>} items
  1256. * A collection with text and onclicks for the items.
  1257. *
  1258. * @param {number} x
  1259. * The x position of the opener button
  1260. *
  1261. * @param {number} y
  1262. * The y position of the opener button
  1263. *
  1264. * @param {number} width
  1265. * The width of the opener button
  1266. *
  1267. * @param {number} height
  1268. * The height of the opener button
  1269. */
  1270. contextMenu: function (className, items, x, y, width, height, button) {
  1271. var chart = this,
  1272. navOptions = chart.options.navigation,
  1273. chartWidth = chart.chartWidth,
  1274. chartHeight = chart.chartHeight,
  1275. cacheName = 'cache-' + className,
  1276. menu = chart[cacheName],
  1277. menuPadding = Math.max(width, height), // for mouse leave detection
  1278. innerMenu,
  1279. menuStyle;
  1280. // create the menu only the first time
  1281. if (!menu) {
  1282. // create a HTML element above the SVG
  1283. chart.exportContextMenu = chart[cacheName] = menu =
  1284. createElement('div', {
  1285. className: className
  1286. }, {
  1287. position: 'absolute',
  1288. zIndex: 1000,
  1289. padding: menuPadding + 'px',
  1290. pointerEvents: 'auto'
  1291. }, chart.fixedDiv || chart.container);
  1292. innerMenu = createElement(
  1293. 'div',
  1294. { className: 'highcharts-menu' },
  1295. null,
  1296. menu
  1297. );
  1298. // Presentational CSS
  1299. if (!chart.styledMode) {
  1300. css(innerMenu, extend({
  1301. MozBoxShadow: '3px 3px 10px #888',
  1302. WebkitBoxShadow: '3px 3px 10px #888',
  1303. boxShadow: '3px 3px 10px #888'
  1304. }, navOptions.menuStyle));
  1305. }
  1306. // hide on mouse out
  1307. menu.hideMenu = function () {
  1308. css(menu, { display: 'none' });
  1309. if (button) {
  1310. button.setState(0);
  1311. }
  1312. chart.openMenu = false;
  1313. H.clearTimeout(menu.hideTimer);
  1314. };
  1315. // Hide the menu some time after mouse leave (#1357)
  1316. chart.exportEvents.push(
  1317. addEvent(menu, 'mouseleave', function () {
  1318. menu.hideTimer = setTimeout(menu.hideMenu, 500);
  1319. }),
  1320. addEvent(menu, 'mouseenter', function () {
  1321. H.clearTimeout(menu.hideTimer);
  1322. }),
  1323. // Hide it on clicking or touching outside the menu (#2258,
  1324. // #2335, #2407)
  1325. addEvent(doc, 'mouseup', function (e) {
  1326. if (!chart.pointer.inClass(e.target, className)) {
  1327. menu.hideMenu();
  1328. }
  1329. }),
  1330. addEvent(menu, 'click', function () {
  1331. if (chart.openMenu) {
  1332. menu.hideMenu();
  1333. }
  1334. })
  1335. );
  1336. // create the items
  1337. items.forEach(function (item) {
  1338. if (typeof item === 'string') {
  1339. item = chart.options.exporting.menuItemDefinitions[item];
  1340. }
  1341. if (H.isObject(item, true)) {
  1342. var element;
  1343. if (item.separator) {
  1344. element = createElement('hr', null, null, innerMenu);
  1345. } else {
  1346. element = createElement('div', {
  1347. className: 'highcharts-menu-item',
  1348. onclick: function (e) {
  1349. if (e) { // IE7
  1350. e.stopPropagation();
  1351. }
  1352. menu.hideMenu();
  1353. if (item.onclick) {
  1354. item.onclick.apply(chart, arguments);
  1355. }
  1356. },
  1357. innerHTML: (
  1358. item.text ||
  1359. chart.options.lang[item.textKey]
  1360. )
  1361. }, null, innerMenu);
  1362. if (!chart.styledMode) {
  1363. element.onmouseover = function () {
  1364. css(this, navOptions.menuItemHoverStyle);
  1365. };
  1366. element.onmouseout = function () {
  1367. css(this, navOptions.menuItemStyle);
  1368. };
  1369. css(element, extend({
  1370. cursor: 'pointer'
  1371. }, navOptions.menuItemStyle));
  1372. }
  1373. }
  1374. // Keep references to menu divs to be able to destroy them
  1375. chart.exportDivElements.push(element);
  1376. }
  1377. });
  1378. // Keep references to menu and innerMenu div to be able to destroy
  1379. // them
  1380. chart.exportDivElements.push(innerMenu, menu);
  1381. chart.exportMenuWidth = menu.offsetWidth;
  1382. chart.exportMenuHeight = menu.offsetHeight;
  1383. }
  1384. menuStyle = { display: 'block' };
  1385. // if outside right, right align it
  1386. if (x + chart.exportMenuWidth > chartWidth) {
  1387. menuStyle.right = (chartWidth - x - width - menuPadding) + 'px';
  1388. } else {
  1389. menuStyle.left = (x - menuPadding) + 'px';
  1390. }
  1391. // if outside bottom, bottom align it
  1392. if (
  1393. y + height + chart.exportMenuHeight > chartHeight &&
  1394. button.alignOptions.verticalAlign !== 'top'
  1395. ) {
  1396. menuStyle.bottom = (chartHeight - y - menuPadding) + 'px';
  1397. } else {
  1398. menuStyle.top = (y + height - menuPadding) + 'px';
  1399. }
  1400. css(menu, menuStyle);
  1401. chart.openMenu = true;
  1402. },
  1403. /**
  1404. * Add the export button to the chart, with options.
  1405. *
  1406. * @private
  1407. * @function Highcharts.Chart#addButton
  1408. *
  1409. * @param {Highcharts.NavigationButtonOptions} options
  1410. */
  1411. addButton: function (options) {
  1412. var chart = this,
  1413. renderer = chart.renderer,
  1414. btnOptions = merge(chart.options.navigation.buttonOptions, options),
  1415. onclick = btnOptions.onclick,
  1416. menuItems = btnOptions.menuItems,
  1417. symbol,
  1418. button,
  1419. symbolSize = btnOptions.symbolSize || 12;
  1420. if (!chart.btnCount) {
  1421. chart.btnCount = 0;
  1422. }
  1423. // Keeps references to the button elements
  1424. if (!chart.exportDivElements) {
  1425. chart.exportDivElements = [];
  1426. chart.exportSVGElements = [];
  1427. }
  1428. if (btnOptions.enabled === false) {
  1429. return;
  1430. }
  1431. var attr = btnOptions.theme,
  1432. states = attr.states,
  1433. hover = states && states.hover,
  1434. select = states && states.select,
  1435. callback;
  1436. if (!chart.styledMode) {
  1437. attr.fill = pick(attr.fill, '#ffffff');
  1438. attr.stroke = pick(attr.stroke, 'none');
  1439. }
  1440. delete attr.states;
  1441. if (onclick) {
  1442. callback = function (e) {
  1443. if (e) {
  1444. e.stopPropagation();
  1445. }
  1446. onclick.call(chart, e);
  1447. };
  1448. } else if (menuItems) {
  1449. callback = function (e) {
  1450. // consistent with onclick call (#3495)
  1451. if (e) {
  1452. e.stopPropagation();
  1453. }
  1454. chart.contextMenu(
  1455. button.menuClassName,
  1456. menuItems,
  1457. button.translateX,
  1458. button.translateY,
  1459. button.width,
  1460. button.height,
  1461. button
  1462. );
  1463. button.setState(2);
  1464. };
  1465. }
  1466. if (btnOptions.text && btnOptions.symbol) {
  1467. attr.paddingLeft = pick(attr.paddingLeft, 25);
  1468. } else if (!btnOptions.text) {
  1469. extend(attr, {
  1470. width: btnOptions.width,
  1471. height: btnOptions.height,
  1472. padding: 0
  1473. });
  1474. }
  1475. if (!chart.styledMode) {
  1476. attr['stroke-linecap'] = 'round';
  1477. attr.fill = pick(attr.fill, '#ffffff');
  1478. attr.stroke = pick(attr.stroke, 'none');
  1479. }
  1480. button = renderer
  1481. .button(btnOptions.text, 0, 0, callback, attr, hover, select)
  1482. .addClass(options.className)
  1483. .attr({
  1484. title: pick(chart.options.lang[
  1485. btnOptions._titleKey || btnOptions.titleKey
  1486. ], '')
  1487. });
  1488. button.menuClassName = (
  1489. options.menuClassName ||
  1490. 'highcharts-menu-' + chart.btnCount++
  1491. );
  1492. if (btnOptions.symbol) {
  1493. symbol = renderer
  1494. .symbol(
  1495. btnOptions.symbol,
  1496. btnOptions.symbolX - (symbolSize / 2),
  1497. btnOptions.symbolY - (symbolSize / 2),
  1498. symbolSize,
  1499. symbolSize
  1500. // If symbol is an image, scale it (#7957)
  1501. , {
  1502. width: symbolSize,
  1503. height: symbolSize
  1504. }
  1505. )
  1506. .addClass('highcharts-button-symbol')
  1507. .attr({
  1508. zIndex: 1
  1509. })
  1510. .add(button);
  1511. if (!chart.styledMode) {
  1512. symbol.attr({
  1513. stroke: btnOptions.symbolStroke,
  1514. fill: btnOptions.symbolFill,
  1515. 'stroke-width': btnOptions.symbolStrokeWidth || 1
  1516. });
  1517. }
  1518. }
  1519. button.add(chart.exportingGroup)
  1520. .align(extend(btnOptions, {
  1521. width: button.width,
  1522. x: pick(btnOptions.x, chart.buttonOffset) // #1654
  1523. }), true, 'spacingBox');
  1524. chart.buttonOffset += (
  1525. (button.width + btnOptions.buttonSpacing) *
  1526. (btnOptions.align === 'right' ? -1 : 1)
  1527. );
  1528. chart.exportSVGElements.push(button, symbol);
  1529. },
  1530. /**
  1531. * Destroy the export buttons.
  1532. *
  1533. * @private
  1534. * @function Highcharts.Chart#destroyExport
  1535. *
  1536. * @param {global.Event} e
  1537. */
  1538. destroyExport: function (e) {
  1539. var chart = e ? e.target : this,
  1540. exportSVGElements = chart.exportSVGElements,
  1541. exportDivElements = chart.exportDivElements,
  1542. exportEvents = chart.exportEvents,
  1543. cacheName;
  1544. // Destroy the extra buttons added
  1545. if (exportSVGElements) {
  1546. exportSVGElements.forEach(function (elem, i) {
  1547. // Destroy and null the svg elements
  1548. if (elem) { // #1822
  1549. elem.onclick = elem.ontouchstart = null;
  1550. cacheName = 'cache-' + elem.menuClassName;
  1551. if (chart[cacheName]) {
  1552. delete chart[cacheName];
  1553. }
  1554. chart.exportSVGElements[i] = elem.destroy();
  1555. }
  1556. });
  1557. exportSVGElements.length = 0;
  1558. }
  1559. // Destroy the exporting group
  1560. if (chart.exportingGroup) {
  1561. chart.exportingGroup.destroy();
  1562. delete chart.exportingGroup;
  1563. }
  1564. // Destroy the divs for the menu
  1565. if (exportDivElements) {
  1566. exportDivElements.forEach(function (elem, i) {
  1567. // Remove the event handler
  1568. H.clearTimeout(elem.hideTimer); // #5427
  1569. removeEvent(elem, 'mouseleave');
  1570. // Remove inline events
  1571. chart.exportDivElements[i] =
  1572. elem.onmouseout =
  1573. elem.onmouseover =
  1574. elem.ontouchstart =
  1575. elem.onclick = null;
  1576. // Destroy the div by moving to garbage bin
  1577. discardElement(elem);
  1578. });
  1579. exportDivElements.length = 0;
  1580. }
  1581. if (exportEvents) {
  1582. exportEvents.forEach(function (unbind) {
  1583. unbind();
  1584. });
  1585. exportEvents.length = 0;
  1586. }
  1587. }
  1588. });
  1589. // These ones are translated to attributes rather than styles
  1590. SVGRenderer.prototype.inlineToAttributes = [
  1591. 'fill',
  1592. 'stroke',
  1593. 'strokeLinecap',
  1594. 'strokeLinejoin',
  1595. 'strokeWidth',
  1596. 'textAnchor',
  1597. 'x',
  1598. 'y'
  1599. ];
  1600. // These CSS properties are not inlined. Remember camelCase.
  1601. SVGRenderer.prototype.inlineBlacklist = [
  1602. /-/, // In Firefox, both hyphened and camelCased names are listed
  1603. /^(clipPath|cssText|d|height|width)$/, // Full words
  1604. /^font$/, // more specific props are set
  1605. /[lL]ogical(Width|Height)$/,
  1606. /perspective/,
  1607. /TapHighlightColor/,
  1608. /^transition/,
  1609. /^length$/ // #7700
  1610. // /^text (border|color|cursor|height|webkitBorder)/
  1611. ];
  1612. SVGRenderer.prototype.unstyledElements = [
  1613. 'clipPath',
  1614. 'defs',
  1615. 'desc'
  1616. ];
  1617. /**
  1618. * Analyze inherited styles from stylesheets and add them inline
  1619. *
  1620. * @private
  1621. * @function Highcharts.Chart#inlineStyles
  1622. *
  1623. * @todo: What are the border styles for text about? In general, text has a lot
  1624. * of properties.
  1625. * @todo: Make it work with IE9 and IE10.
  1626. */
  1627. Chart.prototype.inlineStyles = function () {
  1628. var renderer = this.renderer,
  1629. inlineToAttributes = renderer.inlineToAttributes,
  1630. blacklist = renderer.inlineBlacklist,
  1631. whitelist = renderer.inlineWhitelist, // For IE
  1632. unstyledElements = renderer.unstyledElements,
  1633. defaultStyles = {},
  1634. dummySVG,
  1635. iframe,
  1636. iframeDoc;
  1637. // Create an iframe where we read default styles without pollution from this
  1638. // body
  1639. iframe = doc.createElement('iframe');
  1640. css(iframe, {
  1641. width: '1px',
  1642. height: '1px',
  1643. visibility: 'hidden'
  1644. });
  1645. doc.body.appendChild(iframe);
  1646. iframeDoc = iframe.contentWindow.document;
  1647. iframeDoc.open();
  1648. iframeDoc.write('<svg xmlns="http://www.w3.org/2000/svg"></svg>');
  1649. iframeDoc.close();
  1650. /**
  1651. * Make hyphenated property names out of camelCase
  1652. */
  1653. function hyphenate(prop) {
  1654. return prop.replace(
  1655. /([A-Z])/g,
  1656. function (a, b) {
  1657. return '-' + b.toLowerCase();
  1658. }
  1659. );
  1660. }
  1661. /**
  1662. * Call this on all elements and recurse to children
  1663. */
  1664. function recurse(node) {
  1665. var styles,
  1666. parentStyles,
  1667. cssText = '',
  1668. dummy,
  1669. styleAttr,
  1670. blacklisted,
  1671. whitelisted,
  1672. i;
  1673. // Check computed styles and whether they are in the white/blacklist for
  1674. // styles or atttributes
  1675. function filterStyles(val, prop) {
  1676. // Check against whitelist & blacklist
  1677. blacklisted = whitelisted = false;
  1678. if (whitelist) {
  1679. // Styled mode in IE has a whitelist instead.
  1680. // Exclude all props not in this list.
  1681. i = whitelist.length;
  1682. while (i-- && !whitelisted) {
  1683. whitelisted = whitelist[i].test(prop);
  1684. }
  1685. blacklisted = !whitelisted;
  1686. }
  1687. // Explicitly remove empty transforms
  1688. if (prop === 'transform' && val === 'none') {
  1689. blacklisted = true;
  1690. }
  1691. i = blacklist.length;
  1692. while (i-- && !blacklisted) {
  1693. blacklisted = (
  1694. blacklist[i].test(prop) ||
  1695. typeof val === 'function'
  1696. );
  1697. }
  1698. if (!blacklisted) {
  1699. // If parent node has the same style, it gets inherited, no need
  1700. // to inline it. Top-level props should be diffed against parent
  1701. // (#7687).
  1702. if (
  1703. (parentStyles[prop] !== val || node.nodeName === 'svg') &&
  1704. defaultStyles[node.nodeName][prop] !== val
  1705. ) {
  1706. // Attributes
  1707. if (inlineToAttributes.indexOf(prop) !== -1) {
  1708. node.setAttribute(hyphenate(prop), val);
  1709. // Styles
  1710. } else {
  1711. cssText += hyphenate(prop) + ':' + val + ';';
  1712. }
  1713. }
  1714. }
  1715. }
  1716. if (
  1717. node.nodeType === 1 &&
  1718. unstyledElements.indexOf(node.nodeName) === -1
  1719. ) {
  1720. styles = win.getComputedStyle(node, null);
  1721. parentStyles = node.nodeName === 'svg' ?
  1722. {} :
  1723. win.getComputedStyle(node.parentNode, null);
  1724. // Get default styles from the browser so that we don't have to add
  1725. // these
  1726. if (!defaultStyles[node.nodeName]) {
  1727. /*
  1728. if (!dummySVG) {
  1729. dummySVG = doc.createElementNS(H.SVG_NS, 'svg');
  1730. dummySVG.setAttribute('version', '1.1');
  1731. doc.body.appendChild(dummySVG);
  1732. }
  1733. */
  1734. dummySVG = iframeDoc.getElementsByTagName('svg')[0];
  1735. dummy = iframeDoc.createElementNS(
  1736. node.namespaceURI,
  1737. node.nodeName
  1738. );
  1739. dummySVG.appendChild(dummy);
  1740. // Copy, so we can remove the node
  1741. defaultStyles[node.nodeName] = merge(
  1742. win.getComputedStyle(dummy, null)
  1743. );
  1744. // Remove default fill, otherwise text disappears when exported
  1745. if (node.nodeName === 'text') {
  1746. delete defaultStyles.text.fill;
  1747. }
  1748. dummySVG.removeChild(dummy);
  1749. }
  1750. // Loop through all styles and add them inline if they are ok
  1751. if (isFirefoxBrowser || isMSBrowser) {
  1752. // Some browsers put lots of styles on the prototype
  1753. for (var p in styles) {
  1754. filterStyles(styles[p], p);
  1755. }
  1756. } else {
  1757. objectEach(styles, filterStyles);
  1758. }
  1759. // Apply styles
  1760. if (cssText) {
  1761. styleAttr = node.getAttribute('style');
  1762. node.setAttribute(
  1763. 'style',
  1764. (styleAttr ? styleAttr + ';' : '') + cssText
  1765. );
  1766. }
  1767. // Set default stroke width (needed at least for IE)
  1768. if (node.nodeName === 'svg') {
  1769. node.setAttribute('stroke-width', '1px');
  1770. }
  1771. if (node.nodeName === 'text') {
  1772. return;
  1773. }
  1774. // Recurse
  1775. [].forEach.call(node.children || node.childNodes, recurse);
  1776. }
  1777. }
  1778. /**
  1779. * Remove the dummy objects used to get defaults
  1780. */
  1781. function tearDown() {
  1782. dummySVG.parentNode.removeChild(dummySVG);
  1783. }
  1784. recurse(this.container.querySelector('svg'));
  1785. tearDown();
  1786. };
  1787. symbols.menu = function (x, y, width, height) {
  1788. var arr = [
  1789. 'M', x, y + 2.5,
  1790. 'L', x + width, y + 2.5,
  1791. 'M', x, y + height / 2 + 0.5,
  1792. 'L', x + width, y + height / 2 + 0.5,
  1793. 'M', x, y + height - 1.5,
  1794. 'L', x + width, y + height - 1.5
  1795. ];
  1796. return arr;
  1797. };
  1798. symbols.menuball = function (x, y, width, height) {
  1799. var path = [],
  1800. h = (height / 3) - 2;
  1801. path = path.concat(
  1802. this.circle(width - h, y, h, h),
  1803. this.circle(width - h, y + h + 4, h, h),
  1804. this.circle(width - h, y + 2 * (h + 4), h, h)
  1805. );
  1806. return path;
  1807. };
  1808. /**
  1809. * Add the buttons on chart load
  1810. *
  1811. * @private
  1812. * @function Highcharts.Chart#renderExporting
  1813. */
  1814. Chart.prototype.renderExporting = function () {
  1815. var chart = this,
  1816. exportingOptions = chart.options.exporting,
  1817. buttons = exportingOptions.buttons,
  1818. isDirty = chart.isDirtyExporting || !chart.exportSVGElements;
  1819. chart.buttonOffset = 0;
  1820. if (chart.isDirtyExporting) {
  1821. chart.destroyExport();
  1822. }
  1823. if (isDirty && exportingOptions.enabled !== false) {
  1824. chart.exportEvents = [];
  1825. chart.exportingGroup = chart.exportingGroup ||
  1826. chart.renderer.g('exporting-group').attr({
  1827. zIndex: 3 // #4955, // #8392
  1828. }).add();
  1829. objectEach(buttons, function (button) {
  1830. chart.addButton(button);
  1831. });
  1832. chart.isDirtyExporting = false;
  1833. }
  1834. // Destroy the export elements at chart destroy
  1835. addEvent(chart, 'destroy', chart.destroyExport);
  1836. };
  1837. // Add update methods to handle chart.update and chart.exporting.update and
  1838. // chart.navigation.update. These must be added to the chart instance rather
  1839. // than the Chart prototype in order to use the chart instance inside the update
  1840. // function.
  1841. addEvent(Chart, 'init', function () {
  1842. var chart = this;
  1843. function update(prop, options, redraw) {
  1844. chart.isDirtyExporting = true;
  1845. merge(true, chart.options[prop], options);
  1846. if (pick(redraw, true)) {
  1847. chart.redraw();
  1848. }
  1849. }
  1850. chart.exporting = {
  1851. update: function (options, redraw) {
  1852. update('exporting', options, redraw);
  1853. }
  1854. };
  1855. // Register update() method for navigation. Can not be set the same way as
  1856. // for exporting, because navigation options are shared with bindings which
  1857. // has separate update() logic.
  1858. chartNavigationMixin.addUpdate(
  1859. function (options, redraw) {
  1860. update('navigation', options, redraw);
  1861. },
  1862. chart
  1863. );
  1864. });
  1865. Chart.prototype.callbacks.push(function (chart) {
  1866. chart.renderExporting();
  1867. addEvent(chart, 'redraw', chart.renderExporting);
  1868. // Uncomment this to see a button directly below the chart, for quick
  1869. // testing of export
  1870. /*
  1871. var button, viewImage, viewSource;
  1872. if (!chart.renderer.forExport) {
  1873. viewImage = function () {
  1874. var div = doc.createElement('div');
  1875. div.innerHTML = chart.getSVGForExport();
  1876. chart.renderTo.parentNode.appendChild(div);
  1877. };
  1878. viewSource = function () {
  1879. var pre = doc.createElement('pre');
  1880. pre.innerHTML = chart.getSVGForExport()
  1881. .replace(/</g, '\n&lt;')
  1882. .replace(/>/g, '&gt;');
  1883. chart.renderTo.parentNode.appendChild(pre);
  1884. };
  1885. viewImage();
  1886. // View SVG Image
  1887. button = doc.createElement('button');
  1888. button.innerHTML = 'View SVG Image';
  1889. chart.renderTo.parentNode.appendChild(button);
  1890. button.onclick = viewImage;
  1891. // View SVG Source
  1892. button = doc.createElement('button');
  1893. button.innerHTML = 'View SVG Source';
  1894. chart.renderTo.parentNode.appendChild(button);
  1895. button.onclick = viewSource;
  1896. }
  1897. //*/
  1898. });