exporting.src.js 15 KB

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