ui: [bug]: chart not rendering
Describe the bug
Hey, I am using charts, and everything was working fine, but now suddenly the pie chart is not rendering. This happens when I use two pie charts side by side. but it rendered the data. see the below image
Affected component/components
chart
How to reproduce
no
Codesandbox/StackBlitz link
No response
Logs
No response
System Info
node-20
next-14
Before submitting
- I’ve made research efforts and searched the documentation
- I’ve searched for existing issues
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 4
- Comments: 45
The charts are not visible for me as well.
Only when explicitly setting
widthandheightprops for the recharts Chart Component (e.g.BarChartorLineChart), the chart is visible. However, responsiveness is lost due to this. Seems like theChartContainerwrapper aroundResponsiveContainerdoes not work?Any suggestions?
Same problem for me. No examples from shadcn-ui’s docs works with next-js 15 and react 19. But if I use a recharts’ official example, it works.
I finally solved it, nothing I searched for worked, but I found a way to solve it by trying. First I’ll explain my problem:
I was trying to use literally any chart from the ones in the documentation, but NONE of them showed me the graphic part of the chart, the text, but nothing about the chart. The error was that all the charts in the documentation are without the width and height defined.
It was quite a headache to guess that the BarChart component requires you to pass its dimensions as a parameter (without being required/mandatory by the component itself!!!)
<BarChart width={500} height={250}>[edit] Oh and I’d like to mention in case you’re wondering, yes, I’m on the latest version of React and Next.
react-isis breaking in recharts 2.13.0-alpha.4 & react 19 / next 15.You need to manually override this in your package.json
See below thread over at recharts. https://github.com/recharts/recharts/issues/4558#issuecomment-2135604065
Updating the above has shadcn charts working on my next 15 / react 19 project with recharts alpha.
May need to update below screenshot in the docs.
Fixed by changing the version of recharts to “recharts”:“^2.13.3”.
Check to make sure the
ChartContainercomponent has a minimum height class or style attribute e.g:min-h-[300px]. The issue that I had was that I was using the tailwindcss aspect plugin. Removing this allowed my charts to render properly.Rechart 2.13.0-alpha.4 with React 19.0.0-beta works fine with NextJs 15. Only shadcn’s chart examples doesn’t work.
for pnpm users:
(updated at 2024/12/11)
the issue for me was that the shadcn example used
max-h-[250px]in the ChartContainer component, while the documentation states to usemin-h-[your-height]. So, after replacing max with min everything worked correctly.Goodmorging at least for me,
I encountered the same issue where the Shadcn chart would not render, and I managed to solve it after through investigation. The primary problem seemed to be that wrapping a div, card, or any other element outside the ChartContainer prevents the chart from rendering. I tried the suggestion by AlexandreLrz to remove the wrapper div, which worked temporarily but wasn’t an ideal solution for my needs.
To get to the bottom of this, I created a new Next.js 14 app and meticulously set it up by installing charts, shadcn, Tailwind CSS, and other necessary dependencies. Upon rendering a chart, it worked perfectly fine!
I didn’t stop there. I conducted a comprehensive comparison of the package.json, tailwind.config.js, and other configuration files between my new working setup and the problematic one. I examined dependencies, configurations, and potential conflicts thoroughly.
After extensive debugging, I discovered that removing require(“@tailwindcss/aspect-ratio”) from the plugins section in tailwind.config.js resolved the rendering issue. Once this line was removed, the charts rendered flawlessly without any further problems. To double-check, I added the aspect-ratio plugin to the new app, which caused the chart to stop rendering. This confirmed that the plugin was the culprit, interfering with the ChartContainer in some way.
I hope i save some debug time for you guys to day!
The main issue is that charts don’t render correctly with dynamic data.
"First bug I found on GitHub."🫡
for people using yarn this is what worked for me:
See my above suggestion if you are using React 19 or Next 15 https://github.com/shadcn-ui/ui/issues/4231#issuecomment-2226782779
By forcing the rc version of
react-is(which supports react 19 rc / 15 rc) the shadcn examples work out of the box and theChartContaineris responsive for me using themin-hthat shadcn tells you to.react-isis a dependency forrechartsthat hasn’t been updated yet in the recharts package. Hence, why you need to force the update.it is solve here https://ui.shadcn.com/docs/react-19#recharts
I manage to fix it by forcing two things:
value:Numberinstead ofStringwidthandheightto<PieChart>component.It’s working even with dynamic data!
Example:
In my case the height and width for
ChartContainerwere missing. When set using Tailwind, it worked Working with React18.3.1and Recharts2.13.3I’m sharing here a hook I made especially for that, I’m not sure it’s the most pro thing out there because I’m just a backend dev who’s doing front-end work
Basically what it does is grab the size of your screen, do a resize and return the width as a numeric (which is the only thing that chart components support as dimensions), so just use the useResize hook and grab the width.
Oh and that “-150” is something I needed for my particular view, adjust it as needed.
Custom useResize hook:
Example using it for a component with a chart:
This is exactly what I did and Charts started working again, remove that plugin and uninstall that!
Can someone help me solve this? using nextjs and react 18.2, whenever i tried copy and paste even shadcn charts example getting this error
Thanks, this solved my problem
I solved it using this CSS code in my global stylesheet file:
Component code:
I solved my problem, it turns out that in my data structure, the nameKey value was a string and not a number
I’m using a LineChart and the line is not rendering, Nextjs 14 and React 18
My chart not render with dynamic data, I am using React 18 and this is my code:
Same for me here with Next JS 14, React 18. I will investigate more during the day EDIT : I modified the ChartContainer component by removing the wrapper div and this solved the issue for me.
If you are using the
@tailwindcss/aspect-ratioplugin, you can follow the instructions on the “Compatibility with default aspect-ratio utilities” section of the readme to support the default aspect-ratio utilities these charts rely on.