-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export_to_png): Add button on slice component for export D3 svg …
…to png file
- Loading branch information
Yan MATAGNE
committed
Sep 19, 2017
1 parent
5718375
commit 5a18021
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint camelcase: 0 */ | ||
|
||
export function hasSvg(slice) { | ||
return !['table', 'filter_box'].includes(slice.form_data.viz_type); | ||
} | ||
|
||
export function exportSlice(slice, format) { | ||
if (format === 'png') { | ||
const tmp = document.getElementById('con_' + slice.slice_id); | ||
const svg = tmp.getElementsByTagName('svg')[0]; | ||
const svg_xml = (new XMLSerializer()).serializeToString(svg); | ||
const data_uri = 'data:image/svg+xml;base64,' + window.btoa(svg_xml); | ||
|
||
const image = new Image(); | ||
image.src = data_uri; | ||
image.onload = function () { | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = image.width; | ||
canvas.height = image.height; | ||
|
||
const context = canvas.getContext('2d'); | ||
context.clearRect(0, 0, image.width, image.height); | ||
context.drawImage(image, 0, 0); | ||
|
||
const a = document.createElement('a'); | ||
a.download = slice.slice_name; | ||
a.href = canvas.toDataURL('image/png'); | ||
a.click(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters