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

[charts] How to avoid component overflow when using composition #12506

Closed
mckelveygreg opened this issue Mar 20, 2024 · 5 comments · Fixed by #12679
Closed

[charts] How to avoid component overflow when using composition #12506

mckelveygreg opened this issue Mar 20, 2024 · 5 comments · Fixed by #12679
Assignees
Labels
component: charts This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation support: commercial Support request from paid users support: question Community support but can be turned into an improvement

Comments

@mckelveygreg
Copy link

mckelveygreg commented Mar 20, 2024

The problem in depth

We have a bar graph where the significant portion is too small if the range starts at zero.
We'd like to be able to set the range to -1 of the min, and +1 of the max. Granted, we can calculate those values, but I do not know where to set them?

Your environment

`npx @mui/envinfo`
  System:
    OS: macOS 14.3.1
  Binaries:
    Node: 20.11.1 - ~/.local/share/mise/installs/node/20.11.1/bin/node
    npm: 10.2.4 - ~/.local/share/mise/installs/node/20.11.1/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 122.0.6261.129

Search keywords: bar, range, min, non zero
Order ID: 86237

@mckelveygreg mckelveygreg added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels Mar 20, 2024
@alexfauquette alexfauquette added component: charts This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Mar 20, 2024
@alexfauquette
Copy link
Member

The min/max properties of the y axis should do the job

yAxis={[{ min: -1000, max: 6000 }]}

https://mui.com/x/react-charts/axis/#axis-sub-domain

@mckelveygreg
Copy link
Author

Sorry, I didn't realize how easy it was to make a repro from the demo's in the docs! 🎉
Here is an illustration of my problem: https://stackblitz.com/edit/react-emgvkt?file=Demo.tsx

You'll see that the bars extend above and below the min and the max values. At the very least, I'm looking to adjust the min, so that it is much closer to where our data can show significant changes. So, displaying from 0 does not allow us to see enough change, and setting min for the y axis causes the bars to overflow the bottom of the chart.

@alexfauquette
Copy link
Member

alexfauquette commented Mar 21, 2024

Ho, you're using composition. In that case, you will need to clip the content. We have an helper <ChartsClipPath /> for that

<g clipPath={`url(#${clipPathId})`}>
  /* ... all the element that need to stay in the drawing area ... */
</g>
<ChartsClipPath id={clipPathId} />

Here it your example with the fix: https://stackblitz.com/edit/react-emgvkt-1vja5y?file=Demo.tsx

You can refer to the current implementation for more details about what's inside and what's not: https://github.com/mui/mui-x/blob/master/packages/x-charts/src/BarChart/BarChart.tsx

Notice that in our codebase the id is obtained by using useId(). The reason is that if you have multiple charts in the same page each chart needs to get a unique id. Otherwise it will be buggy.

I'm keeping the issue open, to remember we need to document this aspect

@alexfauquette alexfauquette added the docs Improvements or additions to the documentation label Mar 21, 2024
@alexfauquette alexfauquette changed the title How can I show a bar graph with a range not starting at 0? [charts] How to avoid component overflow when using composition Mar 21, 2024
@mckelveygreg
Copy link
Author

mckelveygreg commented Mar 21, 2024

Thank you! This is just what I needed, and it seems to be working great

<ResponsiveChartContainer
  yAxis={[
          {
            labelStyle,
            tickLabelStyle,
            fill: extraColors.medium,
            min,
          },
        ]}
>
  <ChartsAxisHighlight x="band" />
  {/* Clip bars when y axis starts at none zero */}
  <g clipPath={`url(#wp-clip-path)`}>
    <BarPlot />
  </g>
  <AreaPlot />
  <LinePlot />
  <ChartsXAxis disableTicks label="Week Starting Date" />
  <ChartsYAxis disableLine />
  <ChartsClipPath id="wp-clip-path" />
</ResponsiveChartContainer>
image

Copy link

github-actions bot commented Apr 5, 2024

⚠️ This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

@mckelveygreg: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation support: commercial Support request from paid users support: question Community support but can be turned into an improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants