Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added button on slice for export D3js graph to png #3496

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions superset/assets/spec/javascripts/dashboard/SliceCell_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('SliceCell', () => {
React.isValidElement(<SliceCell {...mockedProps} />),
).to.equal(true);
});
it('renders six links', () => {
it('renders seven links', () => {
const wrapper = mount(<SliceCell {...mockedProps} />);
expect(wrapper.find('a')).to.have.length(6);
expect(wrapper.find('a')).to.have.length(7);
});
});