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

export both plotly plot and legend images #1126

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default function PlotLegend({
type === 'colorscale' ||
type === 'bubble') && (
<div
// set id for export legend
id="plotLegend"
style={{
display: 'inline-block', // for general usage (e.g., story)
border: '1px solid #dedede',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ export default function PlotListLegend({
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
// set width to avoid unnecessary ellipsis for image export
width: '100%',
}}
>
{item.label === 'No data' ||
Expand Down
40 changes: 37 additions & 3 deletions packages/libs/components/src/plots/ExportPlotToImageButton.tsx
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Image } from '@material-ui/icons';
import { colors, SingleSelect, Warning } from '@veupathdb/coreui';
import { CSSProperties, useState } from 'react';
import domtoimage from 'dom-to-image';

interface ToImageOpts {
height: number;
Expand Down Expand Up @@ -63,11 +64,34 @@ export function ExportPlotToImageButton(props: Props) {
if (format) {
setSawError(false);
try {
await downloadImage(toImage, filename, {
const options = {
format,
height: imageHeight,
width: imageWidth,
});
};
const legendStyles = {
style: {
width: '400px',
backgroundColor: '#fff',
},
};

// export plotly plot
await downloadImage(toImage, filename, options);

// export legend
if (format === 'svg')
await domtoimage
.toSvg(document.getElementById('plotLegend')!, legendStyles)
.then((dataUrl) => {
saveImage(dataUrl, filename, options, true);
});
else if (format === 'png')
await domtoimage
.toPng(document.getElementById('plotLegend')!, legendStyles)
.then((dataUrl) => {
saveImage(dataUrl, filename, options, true);
});
} catch (error) {
setSawError(true);
console.error(error);
Expand Down Expand Up @@ -102,9 +126,19 @@ async function downloadImage(
options: ToImageOpts
) {
const imgUrl = await toImage(options);
saveImage(imgUrl, filename, options);
}

function saveImage(
imgUrl: string,
filename: string,
options: ToImageOpts,
isLegend: boolean = false
) {
const downloadLink = document.createElement('a');
downloadLink.href = imgUrl;
downloadLink.download = filename + '.' + options.format;
downloadLink.download =
filename + (isLegend ? 'Legend' : '') + '.' + options.format;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
Expand Down
Loading