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
Automatic bar width is set to zero when there is only one data point #3640
Comments
It is worth noting that if your chart is affected by this behavior, it is possible to set manually |
This behavior is the same in previous versions right? (I think it is) |
Yes, I have the same behavior with earlier versions. |
This is still broken and the |
I created another example showing how this works (or doesn't work) with date axes: https://codesandbox.io/p/sandbox/tiny-bar-chart-forked-vk9fyv?file=%2Fsrc%2FApp.tsx I don't believe the PR achieves what's in the second image, instead making the bar always 1/3 the width of the chart. How can we have one bar with a size considering the ticks/domain? My hunch is that the bug isn't in the calculation of the bar size but in the calculation of the visible ticks... For now the only real workaround seems to be to create dummy data for missing x values yourself. |
After much digging, I have concluded that there is no easy & correct fix for this.
@ckifer @nikolasrieble does this make sense? |
…echarts#4390) This adds support for percentage values for `barSize` as a workaround for recharts#3640. See also my comment at recharts#3640 (comment) This is not really the perfect solution, but it allows users to at least have a good workaround. If you know how many data points your axis domain expects, you can hard-code `barSize` to `100%/numberOfDataPoints` (e.g. `'10%'` for 10 data points). You can also combine this with the automatic calculation, e.g. `barSize={data.length > 1 ? undefined : '10%'}`. ``` <BarChart width={500} height={300} data={[[4.5, 10]]} barSize="30%" /* When there's only one data point on a numerical domain, we cannot automatically calculate the bar size */ margin={{ top: 5, right: 30, left: 20, bottom: 5, }} > <XAxis dataKey={v => v[0]} type="number" domain={[0, 10]} /> <YAxis /> <Tooltip /> <CartesianGrid strokeDasharray="3 3" /> <Bar dataKey={v => v[1]} /> </BarChart> ``` ![Screenshot 2024-04-04 at 16 31 49](https://github.com/recharts/recharts/assets/898549/d4fcbdad-d6ae-468d-b522-fb008c20a2a4) # Conflicts: # src/chart/generateCategoricalChart.tsx
***Backport of #4390*** This adds support for percentage values for `barSize` as a workaround for #3640. See also my comment at #3640 (comment) This is not really the perfect solution, but it allows users to at least have a good workaround. If you know how many data points your axis domain expects, you can hard-code `barSize` to `100%/numberOfDataPoints` (e.g. `'10%'` for 10 data points). You can also combine this with the automatic calculation, e.g. `barSize={data.length > 1 ? undefined : '10%'}`. ``` <BarChart width={500} height={300} data={[[4.5, 10]]} barSize="30%" /* When there's only one data point on a numerical domain, we cannot automatically calculate the bar size */ margin={{ top: 5, right: 30, left: 20, bottom: 5, }} > <XAxis dataKey={v => v[0]} type="number" domain={[0, 10]} /> <YAxis /> <Tooltip /> <CartesianGrid strokeDasharray="3 3" /> <Bar dataKey={v => v[1]} /> </BarChart> ``` ![Screenshot 2024-04-04 at 16 31 49](https://github.com/recharts/recharts/assets/898549/d4fcbdad-d6ae-468d-b522-fb008c20a2a4) # Conflicts: # src/chart/generateCategoricalChart.tsx <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Related Issue <!--- This project only accepts pull requests related to open issues --> <!--- If suggesting a new feature or change, please discuss it in an issue first --> <!--- If fixing a bug, there should be an issue describing it with steps to reproduce --> <!--- Please link to the issue here: --> ## Motivation and Context <!--- Why is this change required? What problem does it solve? --> ## How Has This Been Tested? <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots (if appropriate): ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] I have added a storybook story or extended an existing story to show my changes
Reproduction link
Steps to reproduce
BarChart
with aXAxis
of typenumber
and adomain
What is expected?
The bar has a width that makes it visible.
What is actually happening?
The bar has a zero width by default, which makes it invisible.
The text was updated successfully, but these errors were encountered: