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

Fix exporting plots to SVG #4835

Merged
merged 4 commits into from
Oct 18, 2023
Merged

Fix exporting plots to SVG #4835

merged 4 commits into from
Oct 18, 2023

Conversation

mattseddon
Copy link
Member

@mattseddon mattseddon commented Oct 16, 2023

1/2 main <- this <- #4849

This PR fixes exporting plots to SVGs. It also removes exporting plots to PNGs as this currently does not work.

PTAL, I've left comments inline.

Demo

https://github.com/iterative/vscode-dvc/assets/37993418/1ff4c704-5f42-41d2-985e-e6841c4014fe

Screen.Recording.2023-10-17.at.3.08.36.pm.mov

@mattseddon mattseddon added the bug Something isn't working label Oct 16, 2023
@mattseddon mattseddon self-assigned this Oct 16, 2023
@@ -91,7 +106,7 @@ export const ZoomedInPlot: React.FC<ZoomedInPlotProps> = ({
actions: {
compiled: false,
editor: false,
export: true,
export: false,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] As per this thread these actions don't work. We might be able to drop this altogether.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to keep the attribute or the actions do not show up

const actions: HTMLDivElement | null | undefined =
zoomedInPlotRef.current?.querySelector('.vega-actions')
if (!actions) {
return
}

appendActionToVega('SVG', actions, () => {
void view.toSVG().then(svg => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Q] @julieg18 do you remember if there is a way to close the menu on click?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like ;(vegaActions.parentNode as HTMLElement).removeAttribute('open') works

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;(vegaActions.parentNode as HTMLElement).removeAttribute('open') works

Yes, we adjust the same attribute when we're opening the menu automatically for the ZoomedInPlot.

ThemeProperty.FONT
])

const fullThemedSvg = preventSvgTruncation(themedSvg)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] Without this we end up with SVGs that look like this:

image

I have spent several hours looking for alternatives including things like https://github.com/tsayen/dom-to-image.

IMO this is good enough for now.

@@ -433,6 +438,25 @@ export class WebviewMessages {
writeFile(file.path, plotId)
}

private async exportPlotAsSvg(svg: string) {
const file = await showSaveDialog(
join(this.dvcRoot, 'visualization.svg'),
Copy link
Member Author

@mattseddon mattseddon Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] I lost a couple of SVGs because they were saved into a folder that VS Code couldn't open. Personally, I think this is a better default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to give the file a unique name (the title of the plot maybe)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recreated exactly what vega does but I can update.

@mattseddon mattseddon changed the title Fix exporting plot to SVG Fix exporting plots to SVG Oct 17, 2023
@mattseddon mattseddon marked this pull request as ready for review October 17, 2023 04:23
@mattseddon mattseddon requested a review from sroy3 as a code owner October 17, 2023 04:23
@@ -433,6 +438,25 @@ export class WebviewMessages {
writeFile(file.path, plotId)
}

private async exportPlotAsSvg(svg: string) {
const file = await showSaveDialog(
join(this.dvcRoot, 'visualization.svg'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to give the file a unique name (the title of the plot maybe)?

@mattseddon mattseddon enabled auto-merge (squash) October 18, 2023 01:24
@codeclimate
Copy link

codeclimate bot commented Oct 18, 2023

Code Climate has analyzed commit d95340a and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 97.8% (85% is the threshold).

This pull request will bring the total coverage in the repository to 95.0% (0.0% change).

View more on Code Climate.

@mattseddon mattseddon merged commit 21979f0 into main Oct 18, 2023
3 checks passed
@mattseddon mattseddon deleted the fix-export-plot branch October 18, 2023 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants