-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[charts] Export charts as SVG #22661
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
Open
sai6855
wants to merge
29
commits into
mui:master
Choose a base branch
from
sai6855:export-svg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c98d7cb
boiler plate code
sai6855 537aa17
barebones export
sai6855 5f3ab5b
add styles
sai6855 4566a25
fix type
sai6855 eb73b78
clone legend
sai6855 e1c40bc
clone legend
sai6855 3c3fe5e
clone legend
sai6855 05cd9ad
clone legend
sai6855 ac20f50
fix
sai6855 1125f98
fix
sai6855 0195010
add docs
sai6855 7fa89d7
docs:api
sai6855 5c400bb
fix chart name
sai6855 cf737c2
Merge branch 'export-svg' of https://github.com/sai6855/mui-x into ex…
sai6855 7901555
fix export
sai6855 bfe4314
Merge branch 'export-svg' of https://github.com/sai6855/mui-x into ex…
sai6855 e51ee40
fix docs
sai6855 790ab3d
Merge branch 'export-svg' of https://github.com/sai6855/mui-x into ex…
sai6855 1a2029c
fix l10n
sai6855 2ac17bd
Merge branch 'master' into export-svg
sai6855 13090ff
export svg export
sai6855 f66f361
Merge branch 'export-svg' of https://github.com/sai6855/mui-x into ex…
sai6855 f95e0d6
export
sai6855 473d337
fix docs
sai6855 28e03b9
fix logic
sai6855 d6685c3
Merge branch 'master' into export-svg
sai6855 5eb4205
svg export doc
sai6855 e19f2cf
fix deployment
sai6855 03d83cc
Merge branch 'master' into export-svg
sai6855 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| import * as React from 'react'; | ||
| import Button from '@mui/material/Button'; | ||
| import Stack from '@mui/material/Stack'; | ||
| import { LineChartPro } from '@mui/x-charts-pro/LineChartPro'; | ||
| import { useChartProApiRef } from '@mui/x-charts-pro/hooks'; | ||
|
|
||
| export default function ExportChartAsSvg() { | ||
| const apiRef = useChartProApiRef(); | ||
|
|
||
| return ( | ||
| <Stack sx={{ width: '100%', gap: 2 }}> | ||
| <LineChartPro | ||
| apiRef={apiRef} | ||
| xAxis={[{ data: [1, 2, 3, 5, 8, 10] }]} | ||
| series={[ | ||
| { data: [4, 9, 1, 4, 9, 6], label: 'Series A' }, | ||
| { data: [2, 5.5, 2, 8.5, 1.5, 5], area: true, label: 'Series B' }, | ||
| ]} | ||
| height={300} | ||
| grid={{ vertical: true, horizontal: true }} | ||
| /> | ||
| <div> | ||
| <Button | ||
| onClick={() => apiRef.current.exportAsSvg({ fileName: 'chart' })} | ||
| variant="contained" | ||
| > | ||
| Export SVG | ||
| </Button> | ||
| </div> | ||
| </Stack> | ||
| ); | ||
| } |
This file contains hidden or 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,32 @@ | ||
| import * as React from 'react'; | ||
| import Button from '@mui/material/Button'; | ||
| import Stack from '@mui/material/Stack'; | ||
| import { LineChartPro } from '@mui/x-charts-pro/LineChartPro'; | ||
| import { useChartProApiRef } from '@mui/x-charts-pro/hooks'; | ||
|
|
||
| export default function ExportChartAsSvg() { | ||
| const apiRef = useChartProApiRef<'line'>(); | ||
|
|
||
| return ( | ||
| <Stack sx={{ width: '100%', gap: 2 }}> | ||
| <LineChartPro | ||
| apiRef={apiRef} | ||
| xAxis={[{ data: [1, 2, 3, 5, 8, 10] }]} | ||
| series={[ | ||
| { data: [4, 9, 1, 4, 9, 6], label: 'Series A' }, | ||
| { data: [2, 5.5, 2, 8.5, 1.5, 5], area: true, label: 'Series B' }, | ||
| ]} | ||
| height={300} | ||
| grid={{ vertical: true, horizontal: true }} | ||
| /> | ||
| <div> | ||
| <Button | ||
| onClick={() => apiRef.current!.exportAsSvg({ fileName: 'chart' })} | ||
| variant="contained" | ||
| > | ||
| Export SVG | ||
| </Button> | ||
| </div> | ||
| </Stack> | ||
| ); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| --- | ||
| title: Charts - Export | ||
| productId: x-charts | ||
| components: ScatterChartPro, BarChartPro, LineChartPro, Heatmap, FunnelChart, RadarChartPro, SankeyChart | ||
| components: ScatterChartPro, BarChartPro, LineChartPro, Heatmap, FunnelChart, RadarChartPro, SankeyChart, ChartsToolbarSvgExportTrigger | ||
| --- | ||
|
|
||
| # Charts - Export [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan') | ||
|
|
||
| <p class="description">Let users export a chart as an image or in PDF format.</p> | ||
|
|
||
| Charts can be exported as images, or as PDFs using the browser's native print dialog. | ||
| Charts can be exported as images, as SVG files, or as PDFs using the browser's native print dialog. | ||
| The exporting feature is available for the following charts: | ||
|
|
||
| - `LineChartPro` | ||
|
|
@@ -61,23 +61,25 @@ yarn add rasterizehtml | |
|
|
||
| ## Export options | ||
|
|
||
| Export behavior can be modified with [print](/x/api/charts/chart-print-export-options/) and [image export](/x/api/charts/chart-image-export-options/) options. | ||
| Export behavior can be modified with [print](/x/api/charts/chart-print-export-options/), [image export](/x/api/charts/chart-image-export-options/), and [SVG export](/x/api/charts/chart-svg-export-options/) options. | ||
| These options can be passed to the built-in toolbar using `slotProps.toolbar`, and are then automatically displayed. | ||
|
|
||
| You can customize their respective behaviors by passing an options object to `slotProps.toolbar`, or to the export trigger itself if you're using a custom toolbar: | ||
|
|
||
| ```tsx | ||
| // Default toolbar: | ||
| <BarChartPro slotProps={{ toolbar: { printOptions, imageExportOptions } }} /> | ||
| <BarChartPro slotProps={{ toolbar: { printOptions, imageExportOptions, svgExportOptions } }} /> | ||
|
|
||
| // Custom trigger: | ||
| <ChartsToolbarImageExportTrigger options={imageExportOptions} /> | ||
| <ChartsToolbarPrintExportTrigger options={printExportOptions} /> | ||
| <ChartsToolbarSvgExportTrigger options={svgExportOptions} /> | ||
| ``` | ||
|
|
||
| ### Export formats | ||
|
|
||
| To disable the print export, set the `disableToolbarButton` property to `true`. | ||
| To disable the print export, set the `disableToolbarButton` property to `true` on `printOptions`. | ||
| The SVG export can be disabled the same way through `svgExportOptions`. | ||
|
|
||
| You can customize image export formats by providing an array of objects to the `imageExportOptions` property. | ||
| These objects must contain the `type` property which specifies the image format. | ||
|
|
@@ -95,8 +97,8 @@ The name of the exported file has been customized to resemble the chart's title. | |
|
|
||
| To add custom styles or modify the chart's appearance before exporting, use the `onBeforeExport` callback. | ||
|
|
||
| When exporting, the chart is first rendered into an iframe and then exported as an image or PDF. | ||
| The `onBeforeExport` callback gives you access to the iframe before the export process starts. | ||
| For image and PDF export, `onBeforeExport` receives the iframe the chart is rendered into before the export process starts. | ||
| SVG export serializes an SVG document, so its `onBeforeExport` receives the `<svg>` element to be exported instead. | ||
|
|
||
| For example, you can add the title and caption to the exported chart as shown below: | ||
|
|
||
|
|
@@ -177,3 +179,25 @@ apiRef.current?.exportAsImage({ pixelRatio: 3 }); | |
| ``` | ||
|
|
||
| {{"demo": "ExportChartAsImage.js"}} | ||
|
|
||
| ### Export as SVG | ||
|
|
||
| The `apiRef` prop also exposes the `exportAsSvg()` method to export the chart as a standalone SVG file. | ||
| Unlike image export, this requires no extra dependency. | ||
|
|
||
| The output is vector-based, so it stays crisp at any size and remains editable in design tools such as Figma. | ||
| Series rendered to a canvas (for example, the WebGL renderer) cannot be vectorized and are embedded as a raster image inside the SVG, while the rest of the chart (axes, labels, legend) stays vector. | ||
|
|
||
| ```tsx | ||
| apiRef.current?.exportAsSvg({ fileName: 'my-chart' }); | ||
| ``` | ||
|
|
||
| {{"demo": "ExportChartAsSvg.js"}} | ||
|
|
||
| :::info | ||
| The chart is always exported in light mode, regardless of the color scheme of the page it is rendered in. | ||
| ::: | ||
|
|
||
|
Comment on lines
+197
to
+200
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a general information impacting also PNG and PDF. Might be better to put it higher in the docs. And maybe add an issue to up-vote for dark mode support |
||
| :::warning | ||
| Styles served from a cross-origin stylesheet cannot be read for security reasons and are omitted from the exported SVG, the same way they are for image export. | ||
| ::: | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,22 @@ | ||
| import * as React from 'react'; | ||
| import InterfaceApiPage from 'docs/src/modules/components/InterfaceApiPage'; | ||
| import layoutConfig from 'docs/src/modules/utils/dataGridLayoutConfig'; | ||
| import { mapApiPageTranslations } from '@mui/internal-core-docs/mapApiPageTranslations'; | ||
| import jsonPageContent from './chart-svg-export-options.json'; | ||
|
|
||
| export default function Page(props) { | ||
| const { descriptions } = props; | ||
| return ( | ||
| <InterfaceApiPage {...layoutConfig} descriptions={descriptions} pageContent={jsonPageContent} /> | ||
| ); | ||
| } | ||
|
|
||
| export async function getStaticProps() { | ||
| const req = require.context( | ||
| 'docs/translations/api-docs/charts/', | ||
| false, | ||
| /\.\/chart-svg-export-options.*.json$/, | ||
| ); | ||
| const descriptions = mapApiPageTranslations(req); | ||
| return { props: { descriptions } }; | ||
| } |
This file contains hidden or 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,18 @@ | ||
| { | ||
| "name": "ChartSvgExportOptions", | ||
| "imports": ["import { ChartSvgExportOptions } from '@mui/x-charts-pro'"], | ||
| "demos": "<ul><li><a href=\"https://mui.com/x/react-charts/export/#export-as-svg\">SVG export</a></li></ul>", | ||
| "properties": { | ||
| "copyStyles": { "type": { "description": "boolean" }, "default": "true", "isProPlan": true }, | ||
| "fileName": { | ||
| "type": { "description": "string" }, | ||
| "default": "The title of the document the chart belongs to", | ||
| "isProPlan": true | ||
| }, | ||
| "nonce": { "type": { "description": "string" }, "isProPlan": true }, | ||
| "onBeforeExport": { | ||
| "type": { "description": "(svg: SVGElement) => Promise<void> | void" }, | ||
| "isProPlan": true | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
docs/pages/x/api/charts/charts-toolbar-svg-export-trigger.js
This file contains hidden or 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,20 @@ | ||
| import * as React from 'react'; | ||
| import { ApiPage } from '@mui/internal-core-docs/ApiPage'; | ||
| import { mapApiPageTranslations } from '@mui/internal-core-docs/mapApiPageTranslations'; | ||
| import jsonPageContent from './charts-toolbar-svg-export-trigger.json'; | ||
|
|
||
| export default function Page(props) { | ||
| const { descriptions } = props; | ||
| return <ApiPage descriptions={descriptions} pageContent={jsonPageContent} />; | ||
| } | ||
|
|
||
| export async function getStaticProps() { | ||
| const req = require.context( | ||
| 'docs/translations/api-docs/charts/charts-toolbar-svg-export-trigger', | ||
| false, | ||
| /\.\/charts-toolbar-svg-export-trigger.*\.json$/, | ||
| ); | ||
| const descriptions = mapApiPageTranslations(req); | ||
|
|
||
| return { props: { descriptions } }; | ||
| } |
14 changes: 14 additions & 0 deletions
14
docs/pages/x/api/charts/charts-toolbar-svg-export-trigger.json
This file contains hidden or 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,14 @@ | ||
| { | ||
| "props": {}, | ||
| "name": "ChartsToolbarSvgExportTrigger", | ||
| "imports": [ | ||
| "import { ChartsToolbarSvgExportTrigger } from '@mui/x-charts-pro/ChartsToolbarPro';", | ||
| "import { ChartsToolbarSvgExportTrigger } from '@mui/x-charts-pro';" | ||
| ], | ||
| "classes": [], | ||
| "muiName": "MuiChartsToolbarSvgExportTrigger", | ||
| "filename": "/packages/x-charts-pro/src/ChartsToolbarPro/ChartsToolbarSvgExportTrigger.tsx", | ||
| "inheritance": null, | ||
| "demos": "<ul><li><a href=\"/x/react-charts/export/\">Charts - Export <a href=\"/x/introduction/licensing/#pro-plan\" title=\"Pro plan\"><span class=\"plan-pro\"></span></a></a></li></ul>", | ||
| "cssComponent": false | ||
| } |
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
docs/translations/api-docs/charts/chart-svg-export-options.json
This file contains hidden or 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,15 @@ | ||
| { | ||
| "interfaceDescription": "The options to apply on the SVG export.", | ||
| "propertiesDescriptions": { | ||
| "copyStyles": { | ||
| "description": "If <code>true</code>, the styles of the page the chart belongs to will be copied to the export iframe.<br />Copying styles is useful to ensure that the exported chart looks the same as it does on the page." | ||
| }, | ||
| "fileName": { "description": "The name of the file without the extension." }, | ||
| "nonce": { | ||
| "description": "A nonce to be used for Content Security Policy (CSP) compliance.<br />If provided, this nonce will be added to any style elements created during the export process." | ||
| }, | ||
| "onBeforeExport": { | ||
| "description": "Callback function that is called before the export is triggered.<br />It receives the SVG element to be exported, so it can be modified before serialization<br />(such as adding elements, updating styles, removing elements, etc.)." | ||
| } | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
.../api-docs/charts/charts-toolbar-svg-export-trigger/charts-toolbar-svg-export-trigger.json
This file contains hidden or 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,5 @@ | ||
| { | ||
| "componentDescription": "A button that triggers an SVG export.\nIt renders the `baseButton` slot.", | ||
| "propDescriptions": {}, | ||
| "classDescriptions": {} | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WOuld be easier if the
onBeforeExportgot the sameiframeinput, and add a warning in the SVG export section to say:::warning
The SVG export is a serialization of the outer
svgcomponent.If you CSS modification or add elements outside, they will have no impact ont the generated svg file
:::
Having same
onBeforeExportsignature should simplify sharing customization across different exports