Skip to content

Commit

Permalink
feat(export_to_png): Add button on slice component for export D3 svg …
Browse files Browse the repository at this point in the history
…to png file
  • Loading branch information
Yan MATAGNE committed Sep 19, 2017
1 parent 5718375 commit 5a18021
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
31 changes: 31 additions & 0 deletions superset/assets/javascripts/components/ExportSlice.js
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();
};
}
}
13 changes: 12 additions & 1 deletion superset/assets/javascripts/dashboard/components/SliceCell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import PropTypes from 'prop-types';

import { getExploreUrl } from '../../explore/exploreUtils';

import { exportSlice, hasSvg } from '../../components/ExportSlice';

const propTypes = {
slice: PropTypes.object.isRequired,
removeSlice: PropTypes.func.isRequired,
Expand Down Expand Up @@ -50,6 +52,15 @@ function SliceCell({ expandedSlices, removeSlice, slice }) {
>
<i className="fa fa-table" />
</a>
<a
className="exportPNG"
onClick={() => { exportSlice(slice, 'png'); }}
title="Export PNG"
data-toggle="tooltip"
style={{ display: hasSvg(slice) ? 'inline' : 'none' }}
>
<i className="fa fa-download" />
</a>
<a
className="exploreChart"
href={getExploreUrl(slice.form_data)}
Expand All @@ -76,7 +87,7 @@ function SliceCell({ expandedSlices, removeSlice, slice }) {
className="slice_description bs-callout bs-callout-default"
style={
expandedSlices &&
expandedSlices[String(slice.slice_id)] ? {} : { display: 'none' }
expandedSlices[String(slice.slice_id)] ? {} : { display: 'none' }
}
dangerouslySetInnerHTML={{ __html: slice.description_markeddown }}
/>
Expand Down

0 comments on commit 5a18021

Please sign in to comment.