MCPcopy
hub / github.com/apache/echarts / SaveAsImage

Class SaveAsImage

src/component/toolbox/feature/SaveAsImage.ts:45–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43}
44
45class SaveAsImage extends ToolboxFeature<ToolboxSaveAsImageFeatureOption> {
46
47 onclick(ecModel: GlobalModel, api: ExtensionAPI) {
48 const model = this.model;
49 const title = model.get('name') || ecModel.get('title.0.text') || 'echarts';
50 const isSvg = api.getZr().painter.getType() === 'svg';
51 const type = isSvg ? 'svg' : model.get('type', true) || 'png';
52 const url = api.getConnectedDataURL({
53 type: type,
54 backgroundColor: model.get('backgroundColor', true)
55 || ecModel.get('backgroundColor') || tokens.color.neutral00,
56 connectedBackgroundColor: model.get('connectedBackgroundColor'),
57 excludeComponents: model.get('excludeComponents'),
58 pixelRatio: model.get('pixelRatio')
59 });
60 const browser = env.browser;
61 // Chrome, Firefox, New Edge
62 if (typeof MouseEvent === 'function' && (browser.newEdge || (!browser.ie && !browser.edge))) {
63 const $a = document.createElement('a');
64 $a.download = title + '.' + type;
65 $a.target = '_blank';
66 $a.href = url;
67 const evt = new MouseEvent('click', {
68 // some micro front-end framework, window maybe is a Proxy
69 view: document.defaultView,
70 bubbles: true,
71 cancelable: false
72 });
73 $a.dispatchEvent(evt);
74 }
75 // IE or old Edge
76 else {
77 // @ts-ignore
78 if (window.navigator.msSaveOrOpenBlob || isSvg) {
79 const parts = url.split(',');
80 // data:[<mime type>][;charset=<charset>][;base64],<encoded data>
81 const base64Encoded = parts[0].indexOf('base64') > -1;
82 let bstr = isSvg
83 // should decode the svg data uri first
84 ? decodeURIComponent(parts[1])
85 : parts[1];
86 // only `atob` when the data uri is encoded with base64
87 // otherwise, like `svg` data uri exported by zrender,
88 // there will be an error, for it's not encoded with base64.
89 // (just a url-encoded string through `encodeURIComponent`)
90 base64Encoded && (bstr = window.atob(bstr));
91 const filename = title + '.' + type;
92 // @ts-ignore
93 if (window.navigator.msSaveOrOpenBlob) {
94 let n = bstr.length;
95 const u8arr = new Uint8Array(n);
96 while (n--) {
97 u8arr[n] = bstr.charCodeAt(n);
98 }
99 const blob = new Blob([u8arr]);// @ts-ignore
100 window.navigator.msSaveOrOpenBlob(blob, filename);
101 }
102 else {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…