SaveAsImage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var env = require("zrender/lib/core/env");
  20. var lang = require("../../../lang");
  21. var featureManager = require("../featureManager");
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /* global Uint8Array */
  41. var saveAsImageLang = lang.toolbox.saveAsImage;
  42. function SaveAsImage(model) {
  43. this.model = model;
  44. }
  45. SaveAsImage.defaultOption = {
  46. show: true,
  47. icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
  48. title: saveAsImageLang.title,
  49. type: 'png',
  50. // Default use option.backgroundColor
  51. // backgroundColor: '#fff',
  52. connectedBackgroundColor: '#fff',
  53. name: '',
  54. excludeComponents: ['toolbox'],
  55. pixelRatio: 1,
  56. lang: saveAsImageLang.lang.slice()
  57. };
  58. SaveAsImage.prototype.unusable = !env.canvasSupported;
  59. var proto = SaveAsImage.prototype;
  60. proto.onclick = function (ecModel, api) {
  61. var model = this.model;
  62. var title = model.get('name') || ecModel.get('title.0.text') || 'echarts';
  63. var isSvg = api.getZr().painter.getType() === 'svg';
  64. var type = isSvg ? 'svg' : model.get('type', true) || 'png';
  65. var url = api.getConnectedDataURL({
  66. type: type,
  67. backgroundColor: model.get('backgroundColor', true) || ecModel.get('backgroundColor') || '#fff',
  68. connectedBackgroundColor: model.get('connectedBackgroundColor'),
  69. excludeComponents: model.get('excludeComponents'),
  70. pixelRatio: model.get('pixelRatio')
  71. }); // Chrome and Firefox
  72. if (typeof MouseEvent === 'function' && !env.browser.ie && !env.browser.edge) {
  73. var $a = document.createElement('a');
  74. $a.download = title + '.' + type;
  75. $a.target = '_blank';
  76. $a.href = url;
  77. var evt = new MouseEvent('click', {
  78. // some micro front-end framework, window maybe is a Proxy
  79. view: document.defaultView,
  80. bubbles: true,
  81. cancelable: false
  82. });
  83. $a.dispatchEvent(evt);
  84. } // IE
  85. else {
  86. if (window.navigator.msSaveOrOpenBlob) {
  87. var bstr = atob(url.split(',')[1]);
  88. var n = bstr.length;
  89. var u8arr = new Uint8Array(n);
  90. while (n--) {
  91. u8arr[n] = bstr.charCodeAt(n);
  92. }
  93. var blob = new Blob([u8arr]);
  94. window.navigator.msSaveOrOpenBlob(blob, title + '.' + type);
  95. } else {
  96. var lang = model.get('lang');
  97. var html = '' + '<body style="margin:0;">' + '<img src="' + url + '" style="max-width:100%;" title="' + (lang && lang[0] || '') + '" />' + '</body>';
  98. var tab = window.open();
  99. tab.document.write(html);
  100. }
  101. }
  102. };
  103. featureManager.register('saveAsImage', SaveAsImage);
  104. var _default = SaveAsImage;
  105. module.exports = _default;