Skip to content

Conversation

bernardobelchior
Copy link
Member

Add a demo that shows how to export the chart title and caption. These aren't part of the chart per se, so they aren't exported by default. This demo is meant as a guide for users looking to customize their export.

Exported PNG:

Charts - Export - MUI X (54)

@bernardobelchior bernardobelchior added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. scope: charts Changes related to the charts. labels Sep 10, 2025
@mui-bot
Copy link

mui-bot commented Sep 10, 2025

Deploy preview: https://deploy-preview-19524--material-ui-x.netlify.app/

Updated pages:

Bundle size report

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) ▼-1B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 🔺+213B(+0.05%) 🔺+113B(+0.09%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against fe6672a

@bernardobelchior bernardobelchior force-pushed the charts-add-title-and-caption branch from ec092cd to b4ab616 Compare September 10, 2025 14:36
Copy link

codspeed-hq bot commented Sep 10, 2025

CodSpeed Performance Report

Merging #19524 will not alter performance

Comparing bernardobelchior:charts-add-title-and-caption (fe6672a) with master (844f100)1

Summary

✅ 10 untouched

Footnotes

  1. No successful run was found on master (69a4e2b) during the generation of this report, so 844f100 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@bernardobelchior bernardobelchior force-pushed the charts-add-title-and-caption branch from 2e0429b to efd5d94 Compare September 10, 2025 17:17
@bernardobelchior bernardobelchior marked this pull request as ready for review September 11, 2025 08:32
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

The feature looks good. I'm wondering if a better apporach for the user would be to use composition with different order.

<DataProvider>
  <Toolbar />
  <div ref={chartRef}>
    <p>The Title</p>
    <ChartSurface>{...}</ChartSurface>
    <p>caption</p>
  </div>
</DataProvider>

Comment on lines 71 to 90
`.${legendClasses.root}`,
) as HTMLElement | null;
const stack = document.createElement('div');
const chart = document.body.firstElementChild!;

if (legend) {
legend.style.display = 'flex';
}
stack.style.display = 'flex';
stack.style.flexDirection = 'column';
stack.style.width = 'fit-content';
stack.style.margin = 'auto';

document.body.appendChild(stack);

const title = titleRef.current;
if (title) {
const titleClone = title.cloneNode(true) as HTMLSpanElement;
stack.appendChild(titleClone);
}

document.body.removeChild(chart);
stack.appendChild(chart);

Copy link
Member

Choose a reason for hiding this comment

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

Might worth some comments like:

  • Create a parent stack div to glue organize elements
  • Move the title in the stack
  • Move the chart as a next child
  • Move the caption as a next child

@bernardobelchior
Copy link
Member Author

The feature looks good. I'm wondering if a better apporach for the user would be to use composition with different order.

<DataProvider>
  <Toolbar />
  <div ref={chartRef}>
    <p>The Title</p>
    <ChartSurface>{...}</ChartSurface>
    <p>caption</p>
  </div>
</DataProvider>

So in the demo we should render the chart using composition instead of the normal way?

@alexfauquette
Copy link
Member

So in the demo we should render the chart using composition instead of the normal way?

Not really, because with composition you don't need to modify the onBefore.
I just think we shoudl enphasis that with coposition most of those workaround are not needed.

What about adding a "Downloaded from mui.com" bellow the chart that appears only at export, and mofiy the sentence as follow

Instead of "For example, you can add the title and caption to the exported chart, as shown below:"

In the following demo, the onBeforeExport() is used to:

  • Add the title and caption to the exported chart.
  • Add an extra text "Downloaded from mui.com" bellow the chart

If you're using composition you might not need the onBeforeExport() for to add the title and caption to the exported chart.

@bernardobelchior
Copy link
Member Author

Not really, because with composition you don't need to modify the onBefore. I just think we shoudl enphasis that with coposition most of those workaround are not needed.

That makes sense, but does it make sense to specify that in the onBeforeExport section?

We have a composition section for the export. What about updating that example with a title and a caption and mentioning in the onBeforeExport that we don't need to do it when using composition and link to the composition section?

@alexfauquette
Copy link
Member

We have a composition section for the export. What about updating that example with a title and a caption and mentioning in the onBeforeExport that we don't need to do it when using composition and link to the composition section?

Looks good 👍

@bernardobelchior
Copy link
Member Author

We have a composition section for the export. What about updating that example with a title and a caption and mentioning in the onBeforeExport that we don't need to do it when using composition and link to the composition section?

Looks good 👍

Done 👍

@bernardobelchior bernardobelchior merged commit 92e4dc2 into mui:master Sep 15, 2025
22 checks passed
@bernardobelchior bernardobelchior deleted the charts-add-title-and-caption branch September 15, 2025 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: charts Changes related to the charts. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants