Skip to content

[charts][docs] Improve Charts export docs #17538

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

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 9 additions & 13 deletions docs/data/charts/export/ExportChartAsImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ function ExportParamsSelector({ apiRef }) {
const quality = Math.max(0, Math.min(1, Number.parseFloat(rawQuality)));

return (
<Stack
direction="row"
justifyContent="space-between"
flexWrap="wrap"
gap={2}
sx={{ width: '100%' }}
>
<Stack justifyContent="space-between" gap={2} sx={{ width: '100%' }}>
<FormControl fullWidth>
<FormLabel id="image-format-radio-buttons-group-label">
Image Format
Expand Down Expand Up @@ -59,12 +53,14 @@ function ExportParamsSelector({ apiRef }) {
helperText="Only applicable to lossy formats."
/>
</FormControl>
<Button
variant="outlined"
onClick={() => apiRef.current?.exportAsImage({ type, quality })}
>
Export Image
</Button>
<div>
<Button
onClick={() => apiRef.current.exportAsImage({ type, quality })}
variant="contained"
>
Export Image
</Button>
</div>
</Stack>
);
}
Expand Down
22 changes: 9 additions & 13 deletions docs/data/charts/export/ExportChartAsImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ function ExportParamsSelector({
const quality = Math.max(0, Math.min(1, Number.parseFloat(rawQuality)));

return (
<Stack
direction="row"
justifyContent="space-between"
flexWrap="wrap"
gap={2}
sx={{ width: '100%' }}
>
<Stack justifyContent="space-between" gap={2} sx={{ width: '100%' }}>
<FormControl fullWidth>
<FormLabel id="image-format-radio-buttons-group-label">
Image Format
Expand Down Expand Up @@ -66,12 +60,14 @@ function ExportParamsSelector({
helperText="Only applicable to lossy formats."
/>
</FormControl>
<Button
variant="outlined"
onClick={() => apiRef.current?.exportAsImage({ type, quality })}
>
Export Image
</Button>
<div>
<Button
onClick={() => apiRef.current!.exportAsImage({ type, quality })}
variant="contained"
>
Export Image
</Button>
</div>
</Stack>
);
}
Expand Down
11 changes: 8 additions & 3 deletions docs/data/charts/export/ExportCompositionNoSnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export default function ExportCompositionNoSnap() {
const apiRef = React.useRef(undefined);

return (
<Stack width="100%">
<Stack width="100%" sx={{ display: 'block' }}>
<Button
onClick={() => apiRef.current.exportAsPrint()}
variant="contained"
sx={{ mb: 1 }}
>
Print
</Button>
<ChartDataProviderPro
apiRef={apiRef}
height={300}
Expand Down Expand Up @@ -65,8 +72,6 @@ export default function ExportCompositionNoSnap() {
</ChartsSurface>
</CustomChartWrapper>
</ChartDataProviderPro>

<Button onClick={() => apiRef.current?.exportAsPrint()}>Print</Button>
</Stack>
);
}
11 changes: 8 additions & 3 deletions docs/data/charts/export/ExportCompositionNoSnap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export default function ExportCompositionNoSnap() {
const apiRef = React.useRef<ChartProApi>(undefined);

return (
<Stack width="100%">
<Stack width="100%" sx={{ display: 'block' }}>
<Button
onClick={() => apiRef.current!.exportAsPrint()}
variant="contained"
sx={{ mb: 1 }}
>
Print
</Button>
<ChartDataProviderPro
apiRef={apiRef}
height={300}
Expand Down Expand Up @@ -65,8 +72,6 @@ export default function ExportCompositionNoSnap() {
</ChartsSurface>
</CustomChartWrapper>
</ChartDataProviderPro>

<Button onClick={() => apiRef.current?.exportAsPrint()}>Print</Button>
</Stack>
);
}
30 changes: 0 additions & 30 deletions docs/data/charts/export/PrintChart.js

This file was deleted.

30 changes: 0 additions & 30 deletions docs/data/charts/export/PrintChart.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions docs/data/charts/export/PrintChart.tsx.preview

This file was deleted.

34 changes: 34 additions & 0 deletions docs/data/charts/export/PrintChartNoSnap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { ScatterChartPro } from '@mui/x-charts-pro/ScatterChartPro';

import { data } from './randomData';

const series = [
{
label: 'Series A',
data: data.map((v) => ({ x: v.x1, y: v.y1, id: v.id })),
},
{
label: 'Series B',
data: data.map((v) => ({ x: v.x1, y: v.y2, id: v.id })),
},
];

export default function PrintChartNoSnap() {
const apiRef = React.useRef(undefined);

return (
<Stack width="100%" sx={{ display: 'block' }}>
<Button
onClick={() => apiRef.current.exportAsPrint()}
variant="contained"
sx={{ mb: 1 }}
>
Print
</Button>
<ScatterChartPro apiRef={apiRef} height={300} series={series} />
</Stack>
);
}
34 changes: 34 additions & 0 deletions docs/data/charts/export/PrintChartNoSnap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { ScatterChartPro } from '@mui/x-charts-pro/ScatterChartPro';
import { ChartProApi } from '@mui/x-charts-pro/ChartContainerPro';
import { data } from './randomData';

const series = [
{
label: 'Series A',
data: data.map((v) => ({ x: v.x1, y: v.y1, id: v.id })),
},
{
label: 'Series B',
data: data.map((v) => ({ x: v.x1, y: v.y2, id: v.id })),
},
];

export default function PrintChartNoSnap() {
const apiRef = React.useRef<ChartProApi>(undefined);

return (
<Stack width="100%" sx={{ display: 'block' }}>
<Button
onClick={() => apiRef.current!.exportAsPrint()}
variant="contained"
sx={{ mb: 1 }}
>
Print
</Button>
<ScatterChartPro apiRef={apiRef} height={300} series={series} />
</Stack>
);
}
8 changes: 8 additions & 0 deletions docs/data/charts/export/PrintChartNoSnap.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Button
onClick={() => apiRef.current!.exportAsPrint()}
variant="contained"
sx={{ mb: 1 }}
>
Print
</Button>
<ScatterChartPro apiRef={apiRef} height={300} series={series} />
8 changes: 4 additions & 4 deletions docs/data/charts/export/export.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ Export is available on the **Pro**[<span class="plan-pro"></span>](/x/introducti

## Print/Export as PDF

The `apiRef` prop exposes a `exportAsPrint` method that can be used to open the browser's print dialog.
The `apiRef` prop exposes a `exportAsPrint()` method that can be used to open the browser's print dialog.

The print dialog allows you to print the chart or save it as a PDF, as well as configuring other settings.

{{"demo": "PrintChart.js"}}
{{"demo": "PrintChartNoSnap.js"}}

## Export as image

The `apiRef` prop also exposes a `exportAsImage()` method to export the chart as an image.

### Dependency

For `exportAsImage()` to work, you need to add `rasterizehtml` as a dependency in your project.
For `exportAsImage()` to work, you need to add the`rasterizehtml` npm dependency in your project.
Copy link
Member Author

Choose a reason for hiding this comment

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

a bit clearer for people that wonder what a dependency is.


<codeblock storageKey="package-manager">

Expand Down Expand Up @@ -63,6 +63,6 @@ As detailed in the [Composition](/x/react-charts/composition/) section, charts c
When exporting a chart, the `ChartsWrapper` element is considered the root element of the chart, and every descendant is included in the export.
As such, you need to ensure that the `ChartsWrapper` element is the root element of the chart you want to export.

If you want to use a custom wrapper element, you need to use the `useChartRootRef` hook to set the reference to the chart's root element so that exporting works properly, as exemplified below.
If you want to use a custom wrapper element, you need to use the `useChartRootRef()` hook to set the reference to the chart's root element so that exporting works properly, as exemplified below.
Copy link
Member Author

Choose a reason for hiding this comment

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

function should be (), see style guide.


{{"demo": "ExportCompositionNoSnap.js"}}