exporting.src.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /**
  2. * @license Highcharts JS v2.2.3 (2012-05-07)
  3. * Exporting module
  4. *
  5. * (c) 2010-2011 Torstein Hønsi
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. // JSLint options:
  10. /*global Highcharts, document, window, Math, setTimeout */
  11. (function () { // encapsulate
  12. // create shortcuts
  13. var HC = Highcharts,
  14. Chart = HC.Chart,
  15. addEvent = HC.addEvent,
  16. removeEvent = HC.removeEvent,
  17. createElement = HC.createElement,
  18. discardElement = HC.discardElement,
  19. css = HC.css,
  20. merge = HC.merge,
  21. each = HC.each,
  22. extend = HC.extend,
  23. math = Math,
  24. mathMax = math.max,
  25. doc = document,
  26. win = window,
  27. hasTouch = doc.documentElement.ontouchstart !== undefined,
  28. M = 'M',
  29. L = 'L',
  30. DIV = 'div',
  31. HIDDEN = 'hidden',
  32. NONE = 'none',
  33. PREFIX = 'highcharts-',
  34. ABSOLUTE = 'absolute',
  35. PX = 'px',
  36. UNDEFINED,
  37. defaultOptions = HC.getOptions();
  38. // Add language
  39. extend(defaultOptions.lang, {
  40. downloadPNG: 'Download PNG image',
  41. downloadJPEG: 'Download JPEG image',
  42. downloadPDF: 'Download PDF document',
  43. downloadSVG: 'Download SVG vector image',
  44. exportButtonTitle: 'Export to raster or vector image',
  45. printButtonTitle: 'Print the chart'
  46. });
  47. // Buttons and menus are collected in a separate config option set called 'navigation'.
  48. // This can be extended later to add control buttons like zoom and pan right click menus.
  49. defaultOptions.navigation = {
  50. menuStyle: {
  51. border: '1px solid #A0A0A0',
  52. background: '#FFFFFF'
  53. },
  54. menuItemStyle: {
  55. padding: '0 5px',
  56. background: NONE,
  57. color: '#303030',
  58. fontSize: hasTouch ? '14px' : '11px'
  59. },
  60. menuItemHoverStyle: {
  61. background: '#4572A5',
  62. color: '#FFFFFF'
  63. },
  64. buttonOptions: {
  65. align: 'right',
  66. backgroundColor: {
  67. linearGradient: [0, 0, 0, 20],
  68. stops: [
  69. [0.4, '#F7F7F7'],
  70. [0.6, '#E3E3E3']
  71. ]
  72. },
  73. borderColor: '#B0B0B0',
  74. borderRadius: 3,
  75. borderWidth: 1,
  76. //enabled: true,
  77. height: 20,
  78. hoverBorderColor: '#909090',
  79. hoverSymbolFill: '#81A7CF',
  80. hoverSymbolStroke: '#4572A5',
  81. symbolFill: '#E0E0E0',
  82. //symbolSize: 12,
  83. symbolStroke: '#A0A0A0',
  84. //symbolStrokeWidth: 1,
  85. symbolX: 11.5,
  86. symbolY: 10.5,
  87. verticalAlign: 'top',
  88. width: 24,
  89. y: 10
  90. }
  91. };
  92. // Add the export related options
  93. defaultOptions.exporting = {
  94. //enabled: true,
  95. //filename: 'chart',
  96. type: 'image/png',
  97. url: 'http://export.highcharts.com/',
  98. width: 800,
  99. buttons: {
  100. exportButton: {
  101. //enabled: true,
  102. symbol: 'exportIcon',
  103. x: -10,
  104. symbolFill: '#A8BF77',
  105. hoverSymbolFill: '#768F3E',
  106. _id: 'exportButton',
  107. _titleKey: 'exportButtonTitle',
  108. menuItems: [{
  109. textKey: 'downloadPNG',
  110. onclick: function () {
  111. this.exportChart();
  112. }
  113. }, {
  114. textKey: 'downloadJPEG',
  115. onclick: function () {
  116. this.exportChart({
  117. type: 'image/jpeg'
  118. });
  119. }
  120. }, {
  121. textKey: 'downloadPDF',
  122. onclick: function () {
  123. this.exportChart({
  124. type: 'application/pdf'
  125. });
  126. }
  127. }, {
  128. textKey: 'downloadSVG',
  129. onclick: function () {
  130. this.exportChart({
  131. type: 'image/svg+xml'
  132. });
  133. }
  134. }
  135. // Enable this block to add "View SVG" to the dropdown menu
  136. /*
  137. ,{
  138. text: 'View SVG',
  139. onclick: function () {
  140. var svg = this.getSVG()
  141. .replace(/</g, '\n&lt;')
  142. .replace(/>/g, '&gt;');
  143. doc.body.innerHTML = '<pre>' + svg + '</pre>';
  144. }
  145. } // */
  146. ]
  147. },
  148. printButton: {
  149. //enabled: true,
  150. symbol: 'printIcon',
  151. x: -36,
  152. symbolFill: '#B5C9DF',
  153. hoverSymbolFill: '#779ABF',
  154. _id: 'printButton',
  155. _titleKey: 'printButtonTitle',
  156. onclick: function () {
  157. this.print();
  158. }
  159. }
  160. }
  161. };
  162. extend(Chart.prototype, {
  163. /**
  164. * Return an SVG representation of the chart
  165. *
  166. * @param additionalOptions {Object} Additional chart options for the generated SVG representation
  167. */
  168. getSVG: function (additionalOptions) {
  169. var chart = this,
  170. chartCopy,
  171. sandbox,
  172. svg,
  173. seriesOptions,
  174. options = merge(chart.options, additionalOptions); // copy the options and add extra options
  175. // IE compatibility hack for generating SVG content that it doesn't really understand
  176. if (!doc.createElementNS) {
  177. /*jslint unparam: true*//* allow unused parameter ns in function below */
  178. doc.createElementNS = function (ns, tagName) {
  179. var elem = doc.createElement(tagName);
  180. elem.getBBox = function () {
  181. return HC.Renderer.prototype.Element.prototype.getBBox.apply({ element: elem });
  182. };
  183. return elem;
  184. };
  185. /*jslint unparam: false*/
  186. }
  187. // create a sandbox where a new chart will be generated
  188. sandbox = createElement(DIV, null, {
  189. position: ABSOLUTE,
  190. top: '-9999em',
  191. width: chart.chartWidth + PX,
  192. height: chart.chartHeight + PX
  193. }, doc.body);
  194. // override some options
  195. extend(options.chart, {
  196. renderTo: sandbox,
  197. forExport: true
  198. });
  199. options.exporting.enabled = false; // hide buttons in print
  200. options.chart.plotBackgroundImage = null; // the converter doesn't handle images
  201. // prepare for replicating the chart
  202. options.series = [];
  203. each(chart.series, function (serie) {
  204. seriesOptions = merge(serie.options, {
  205. animation: false, // turn off animation
  206. showCheckbox: false,
  207. visible: serie.visible
  208. });
  209. if (!seriesOptions.isInternal) { // used for the navigator series that has its own option set
  210. // remove image markers
  211. if (seriesOptions && seriesOptions.marker && /^url\(/.test(seriesOptions.marker.symbol)) {
  212. seriesOptions.marker.symbol = 'circle';
  213. }
  214. options.series.push(seriesOptions);
  215. }
  216. });
  217. // generate the chart copy
  218. chartCopy = new Highcharts.Chart(options);
  219. // reflect axis extremes in the export
  220. each(['xAxis', 'yAxis'], function (axisType) {
  221. each(chart[axisType], function (axis, i) {
  222. var axisCopy = chartCopy[axisType][i],
  223. extremes = axis.getExtremes(),
  224. userMin = extremes.userMin,
  225. userMax = extremes.userMax;
  226. if (userMin !== UNDEFINED || userMax !== UNDEFINED) {
  227. axisCopy.setExtremes(userMin, userMax, true, false);
  228. }
  229. });
  230. });
  231. // get the SVG from the container's innerHTML
  232. svg = chartCopy.container.innerHTML;
  233. // free up memory
  234. options = null;
  235. chartCopy.destroy();
  236. discardElement(sandbox);
  237. // sanitize
  238. svg = svg
  239. .replace(/zIndex="[^"]+"/g, '')
  240. .replace(/isShadow="[^"]+"/g, '')
  241. .replace(/symbolName="[^"]+"/g, '')
  242. .replace(/jQuery[0-9]+="[^"]+"/g, '')
  243. .replace(/isTracker="[^"]+"/g, '')
  244. .replace(/url\([^#]+#/g, 'url(#')
  245. .replace(/<svg /, '<svg xmlns:xlink="http://www.w3.org/1999/xlink" ')
  246. .replace(/ href=/g, ' xlink:href=')
  247. .replace(/\n/, ' ')
  248. .replace(/<\/svg>.*?$/, '</svg>') // any HTML added to the container after the SVG (#894)
  249. /* This fails in IE < 8
  250. .replace(/([0-9]+)\.([0-9]+)/g, function(s1, s2, s3) { // round off to save weight
  251. return s2 +'.'+ s3[0];
  252. })*/
  253. // Replace HTML entities, issue #347
  254. .replace(/&nbsp;/g, '\u00A0') // no-break space
  255. .replace(/&shy;/g, '\u00AD') // soft hyphen
  256. // IE specific
  257. .replace(/<IMG /g, '<image ')
  258. .replace(/height=([^" ]+)/g, 'height="$1"')
  259. .replace(/width=([^" ]+)/g, 'width="$1"')
  260. .replace(/hc-svg-href="([^"]+)">/g, 'xlink:href="$1"/>')
  261. .replace(/id=([^" >]+)/g, 'id="$1"')
  262. .replace(/class=([^" ]+)/g, 'class="$1"')
  263. .replace(/ transform /g, ' ')
  264. .replace(/:(path|rect)/g, '$1')
  265. .replace(/style="([^"]+)"/g, function (s) {
  266. return s.toLowerCase();
  267. });
  268. // IE9 beta bugs with innerHTML. Test again with final IE9.
  269. svg = svg.replace(/(url\(#highcharts-[0-9]+)&quot;/g, '$1')
  270. .replace(/&quot;/g, "'");
  271. if (svg.match(/ xmlns="/g).length === 2) {
  272. svg = svg.replace(/xmlns="[^"]+"/, '');
  273. }
  274. return svg;
  275. },
  276. /**
  277. * Submit the SVG representation of the chart to the server
  278. * @param {Object} options Exporting options. Possible members are url, type and width.
  279. * @param {Object} chartOptions Additional chart options for the SVG representation of the chart
  280. */
  281. exportChart: function (options, chartOptions) {
  282. var form,
  283. chart = this,
  284. svg = chart.getSVG(merge(chart.options.exporting.chartOptions, chartOptions)); // docs
  285. // merge the options
  286. options = merge(chart.options.exporting, options);
  287. // create the form
  288. form = createElement('form', {
  289. method: 'post',
  290. action: options.url,
  291. enctype: 'multipart/form-data'
  292. }, {
  293. display: NONE
  294. }, doc.body);
  295. // add the values
  296. each(['filename', 'type', 'width', 'svg'], function (name) {
  297. createElement('input', {
  298. type: HIDDEN,
  299. name: name,
  300. value: {
  301. filename: options.filename || 'chart',
  302. type: options.type,
  303. width: options.width,
  304. svg: svg
  305. }[name]
  306. }, null, form);
  307. });
  308. // submit
  309. form.submit();
  310. // clean up
  311. discardElement(form);
  312. },
  313. /**
  314. * Print the chart
  315. */
  316. print: function () {
  317. var chart = this,
  318. container = chart.container,
  319. origDisplay = [],
  320. origParent = container.parentNode,
  321. body = doc.body,
  322. childNodes = body.childNodes;
  323. if (chart.isPrinting) { // block the button while in printing mode
  324. return;
  325. }
  326. chart.isPrinting = true;
  327. // hide all body content
  328. each(childNodes, function (node, i) {
  329. if (node.nodeType === 1) {
  330. origDisplay[i] = node.style.display;
  331. node.style.display = NONE;
  332. }
  333. });
  334. // pull out the chart
  335. body.appendChild(container);
  336. // print
  337. win.print();
  338. // allow the browser to prepare before reverting
  339. setTimeout(function () {
  340. // put the chart back in
  341. origParent.appendChild(container);
  342. // restore all body content
  343. each(childNodes, function (node, i) {
  344. if (node.nodeType === 1) {
  345. node.style.display = origDisplay[i];
  346. }
  347. });
  348. chart.isPrinting = false;
  349. }, 1000);
  350. },
  351. /**
  352. * Display a popup menu for choosing the export type
  353. *
  354. * @param {String} name An identifier for the menu
  355. * @param {Array} items A collection with text and onclicks for the items
  356. * @param {Number} x The x position of the opener button
  357. * @param {Number} y The y position of the opener button
  358. * @param {Number} width The width of the opener button
  359. * @param {Number} height The height of the opener button
  360. */
  361. contextMenu: function (name, items, x, y, width, height) {
  362. var chart = this,
  363. navOptions = chart.options.navigation,
  364. menuItemStyle = navOptions.menuItemStyle,
  365. chartWidth = chart.chartWidth,
  366. chartHeight = chart.chartHeight,
  367. cacheName = 'cache-' + name,
  368. menu = chart[cacheName],
  369. menuPadding = mathMax(width, height), // for mouse leave detection
  370. boxShadow = '3px 3px 10px #888',
  371. innerMenu,
  372. hide,
  373. menuStyle;
  374. // create the menu only the first time
  375. if (!menu) {
  376. // create a HTML element above the SVG
  377. chart[cacheName] = menu = createElement(DIV, {
  378. className: PREFIX + name
  379. }, {
  380. position: ABSOLUTE,
  381. zIndex: 1000,
  382. padding: menuPadding + PX
  383. }, chart.container);
  384. innerMenu = createElement(DIV, null,
  385. extend({
  386. MozBoxShadow: boxShadow,
  387. WebkitBoxShadow: boxShadow,
  388. boxShadow: boxShadow
  389. }, navOptions.menuStyle), menu);
  390. // hide on mouse out
  391. hide = function () {
  392. css(menu, { display: NONE });
  393. };
  394. addEvent(menu, 'mouseleave', hide);
  395. // create the items
  396. each(items, function (item) {
  397. if (item) {
  398. var div = createElement(DIV, {
  399. onmouseover: function () {
  400. css(this, navOptions.menuItemHoverStyle);
  401. },
  402. onmouseout: function () {
  403. css(this, menuItemStyle);
  404. },
  405. innerHTML: item.text || chart.options.lang[item.textKey]
  406. }, extend({
  407. cursor: 'pointer'
  408. }, menuItemStyle), innerMenu);
  409. div[hasTouch ? 'ontouchstart' : 'onclick'] = function () {
  410. hide();
  411. item.onclick.apply(chart, arguments);
  412. };
  413. // Keep references to menu divs to be able to destroy them
  414. chart.exportDivElements.push(div);
  415. }
  416. });
  417. // Keep references to menu and innerMenu div to be able to destroy them
  418. chart.exportDivElements.push(innerMenu, menu);
  419. chart.exportMenuWidth = menu.offsetWidth;
  420. chart.exportMenuHeight = menu.offsetHeight;
  421. }
  422. menuStyle = { display: 'block' };
  423. // if outside right, right align it
  424. if (x + chart.exportMenuWidth > chartWidth) {
  425. menuStyle.right = (chartWidth - x - width - menuPadding) + PX;
  426. } else {
  427. menuStyle.left = (x - menuPadding) + PX;
  428. }
  429. // if outside bottom, bottom align it
  430. if (y + height + chart.exportMenuHeight > chartHeight) {
  431. menuStyle.bottom = (chartHeight - y - menuPadding) + PX;
  432. } else {
  433. menuStyle.top = (y + height - menuPadding) + PX;
  434. }
  435. css(menu, menuStyle);
  436. },
  437. /**
  438. * Add the export button to the chart
  439. */
  440. addButton: function (options) {
  441. var chart = this,
  442. renderer = chart.renderer,
  443. btnOptions = merge(chart.options.navigation.buttonOptions, options),
  444. onclick = btnOptions.onclick,
  445. menuItems = btnOptions.menuItems,
  446. buttonWidth = btnOptions.width,
  447. buttonHeight = btnOptions.height,
  448. box,
  449. symbol,
  450. button,
  451. borderWidth = btnOptions.borderWidth,
  452. boxAttr = {
  453. stroke: btnOptions.borderColor
  454. },
  455. symbolAttr = {
  456. stroke: btnOptions.symbolStroke,
  457. fill: btnOptions.symbolFill
  458. },
  459. symbolSize = btnOptions.symbolSize || 12;
  460. // Keeps references to the button elements
  461. if (!chart.exportDivElements) {
  462. chart.exportDivElements = [];
  463. chart.exportSVGElements = [];
  464. }
  465. if (btnOptions.enabled === false) {
  466. return;
  467. }
  468. // element to capture the click
  469. function revert() {
  470. symbol.attr(symbolAttr);
  471. box.attr(boxAttr);
  472. }
  473. // the box border
  474. box = renderer.rect(
  475. 0,
  476. 0,
  477. buttonWidth,
  478. buttonHeight,
  479. btnOptions.borderRadius,
  480. borderWidth
  481. )
  482. //.translate(buttonLeft, buttonTop) // to allow gradients
  483. .align(btnOptions, true)
  484. .attr(extend({
  485. fill: btnOptions.backgroundColor,
  486. 'stroke-width': borderWidth,
  487. zIndex: 19
  488. }, boxAttr)).add();
  489. // the invisible element to track the clicks
  490. button = renderer.rect(
  491. 0,
  492. 0,
  493. buttonWidth,
  494. buttonHeight,
  495. 0
  496. )
  497. .align(btnOptions)
  498. .attr({
  499. id: btnOptions._id,
  500. fill: 'rgba(255, 255, 255, 0.001)',
  501. title: chart.options.lang[btnOptions._titleKey],
  502. zIndex: 21
  503. }).css({
  504. cursor: 'pointer'
  505. })
  506. .on('mouseover', function () {
  507. symbol.attr({
  508. stroke: btnOptions.hoverSymbolStroke,
  509. fill: btnOptions.hoverSymbolFill
  510. });
  511. box.attr({
  512. stroke: btnOptions.hoverBorderColor
  513. });
  514. })
  515. .on('mouseout', revert)
  516. .on('click', revert)
  517. .add();
  518. // add the click event
  519. if (menuItems) {
  520. onclick = function () {
  521. revert();
  522. var bBox = button.getBBox();
  523. chart.contextMenu('export-menu', menuItems, bBox.x, bBox.y, buttonWidth, buttonHeight);
  524. };
  525. }
  526. /*addEvent(button.element, 'click', function() {
  527. onclick.apply(chart, arguments);
  528. });*/
  529. button.on('click', function () {
  530. onclick.apply(chart, arguments);
  531. });
  532. // the icon
  533. symbol = renderer.symbol(
  534. btnOptions.symbol,
  535. btnOptions.symbolX - (symbolSize / 2),
  536. btnOptions.symbolY - (symbolSize / 2),
  537. symbolSize,
  538. symbolSize
  539. )
  540. .align(btnOptions, true)
  541. .attr(extend(symbolAttr, {
  542. 'stroke-width': btnOptions.symbolStrokeWidth || 1,
  543. zIndex: 20
  544. })).add();
  545. // Keep references to the renderer element so to be able to destroy them later.
  546. chart.exportSVGElements.push(box, button, symbol);
  547. },
  548. /**
  549. * Destroy the buttons.
  550. */
  551. destroyExport: function () {
  552. var i,
  553. chart = this,
  554. elem;
  555. // Destroy the extra buttons added
  556. for (i = 0; i < chart.exportSVGElements.length; i++) {
  557. elem = chart.exportSVGElements[i];
  558. // Destroy and null the svg/vml elements
  559. elem.onclick = elem.ontouchstart = null;
  560. chart.exportSVGElements[i] = elem.destroy();
  561. }
  562. // Destroy the divs for the menu
  563. for (i = 0; i < chart.exportDivElements.length; i++) {
  564. elem = chart.exportDivElements[i];
  565. // Remove the event handler
  566. removeEvent(elem, 'mouseleave');
  567. // Remove inline events
  568. chart.exportDivElements[i] = elem.onmouseout = elem.onmouseover = elem.ontouchstart = elem.onclick = null;
  569. // Destroy the div by moving to garbage bin
  570. discardElement(elem);
  571. }
  572. }
  573. });
  574. /**
  575. * Crisp for 1px stroke width, which is default. In the future, consider a smarter,
  576. * global function.
  577. */
  578. function crisp(arr) {
  579. var i = arr.length;
  580. while (i--) {
  581. if (typeof arr[i] === 'number') {
  582. arr[i] = Math.round(arr[i]) - 0.5;
  583. }
  584. }
  585. return arr;
  586. }
  587. // Create the export icon
  588. HC.Renderer.prototype.symbols.exportIcon = function (x, y, width, height) {
  589. return crisp([
  590. M, // the disk
  591. x, y + width,
  592. L,
  593. x + width, y + height,
  594. x + width, y + height * 0.8,
  595. x, y + height * 0.8,
  596. 'Z',
  597. M, // the arrow
  598. x + width * 0.5, y + height * 0.8,
  599. L,
  600. x + width * 0.8, y + height * 0.4,
  601. x + width * 0.4, y + height * 0.4,
  602. x + width * 0.4, y,
  603. x + width * 0.6, y,
  604. x + width * 0.6, y + height * 0.4,
  605. x + width * 0.2, y + height * 0.4,
  606. 'Z'
  607. ]);
  608. };
  609. // Create the print icon
  610. HC.Renderer.prototype.symbols.printIcon = function (x, y, width, height) {
  611. return crisp([
  612. M, // the printer
  613. x, y + height * 0.7,
  614. L,
  615. x + width, y + height * 0.7,
  616. x + width, y + height * 0.4,
  617. x, y + height * 0.4,
  618. 'Z',
  619. M, // the upper sheet
  620. x + width * 0.2, y + height * 0.4,
  621. L,
  622. x + width * 0.2, y,
  623. x + width * 0.8, y,
  624. x + width * 0.8, y + height * 0.4,
  625. 'Z',
  626. M, // the lower sheet
  627. x + width * 0.2, y + height * 0.7,
  628. L,
  629. x, y + height,
  630. x + width, y + height,
  631. x + width * 0.8, y + height * 0.7,
  632. 'Z'
  633. ]);
  634. };
  635. // Add the buttons on chart load
  636. Chart.prototype.callbacks.push(function (chart) {
  637. var n,
  638. exportingOptions = chart.options.exporting,
  639. buttons = exportingOptions.buttons;
  640. if (exportingOptions.enabled !== false) {
  641. for (n in buttons) {
  642. chart.addButton(buttons[n]);
  643. }
  644. // Destroy the export elements at chart destroy
  645. addEvent(chart, 'destroy', chart.destroyExport);
  646. }
  647. });
  648. }());