-
Notifications
You must be signed in to change notification settings - Fork 12k
Fix: display stacked bar with multiple x-Axis #12070
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
Fix: display stacked bar with multiple x-Axis #12070
Conversation
@LeeLenaleee thanks for the approval on the pipeline, do you have an idea why it failed ? It seems to be unrelated to my changes ? |
Sorry I have not had the time to give the tests a good look, I will try to look at it later this week |
@LeeLenaleee Hi there, do you we can retry the pipeline to see if it's still failing ? 🙏 |
@xavierleune I re-ran the pipeline to see. It looked like an image difference was detected. If it fails again, I'll look a bit more closely |
ok, it failed again. The failing image is https://github.com/chartjs/Chart.js/blob/master/test/fixtures/scale.linear/grace/grace-beginAtZero.png Not sure why that's failing only on ubuntu, but I wonder if the failure is due to the test having multiple x axes and now the image has changed. I'm a bit hesitant to change the tolerance on that test without knowing why it has changed. Are you able to run the test locally and see? |
thanks for the feedback @etimberg I'll have a look on that test 👍 |
Ok I see, this test use: |
This should be correct now, I carefuly reviewed the tests and I think the other failures are only due to fonts mismatch and so... 🤞 |
@LeeLenaleee, @etimberg This change broke my code. I'm using two x-axes to create an annotation effect with a bar chart: The annotation is the red bullet. A real world scenario would be to indicate that a bar is beyond a limit. I'm doing this by having a secondary x-axis on top of the chart area. To create an (optional) annotation I'm styling the tick label using a red bullet "⬤". This approach solved many other issues, e.g. you can easily hide the annotations by hiding just the secondary x-axis and it also frees space on the canvas when it's hidden by just the way Chart.js works. With this change my bars appear misaligned to the left. The reason is obvious as now two x-axes are counted and that alters the calculation of the bar positions in a bad in IMO unexpected way. I understand the intention of this PR but I'm also conviced the new behavior is a bug. Instead of grouping bars using multiple x-axes one should group using a group of bars instead: https://www.chartjs.org/docs/latest/samples/bar/stacked-groups.html. I can imagine this change will break other applications, too. For me, I either create another local patch to restore the previous behavior as now I cannot see any other way to implement this aside from creating a (complex) plugin. Edit: Or maybe we need a flag to indicate whether an axis should be counted. Creating this flag should be easy. But the question remains whether this is the right approach anyways or if one should just use groups of bars. |
We are experiencing the exact same problem |
@etimberg maby best of we revert this change for now. What do you think? |
@liondog the stacked group has a different purpose, having different x-axis allows to display side by side different temporal data (i.e: comparison between 2024 and 2025, month by month) ; with stacked groups you won't have different labels (that why multiple axis are made for). You probably may achieve the same behavior by:
This implementation would be much more solid in my opinion |
Thank you for giving more insights into your rationale for using two stacked timescales :-). It's a nice way of some sort of "line wrap" on one axis. I wouldn't say that the previous behavior was a bug. That was, after all, the unchanged behavior of Chart.js for many years. I think both approaches are correct in their way. Perhaps your behavior could be extended to only count axes on the same border? Or on the same configurable "stack" (meaning a type of configuration that logically groups scales together, as implemented by this bug fix). Otherwise, what is the point of a second x-axis aligned to the top? Imagine another example with months being plotted on the bottom x-axis and quarters on another x-axis at the top of the chart to get a different visual grouping for the same bar dataset. This won't work anymore. I will need more time to work out another possible solution for my use case, but for now I am fine with my patch. Your suggestions are not easily implementable as they introduce other problems. To summarize: In my humble opinion, these two different approaches should be configurable. |
@LeeLenaleee inagree, we should revert it. We can reintroduce it behind a config option if necessary |
hi there :) I am experiencing an issue related to this bug fix. I utilized stacked bar charts across different axes to overlay a background chart aligned with a time-based axis for visualizing weekend days. When rendering my bar charts with the Would it be possible to implement a |
 <h3>Snyk has created this PR to upgrade chart.js from 4.4.9 to 4.5.0.</h3> :information_source: Keep your dependencies up-to-date. This makes it easier to fix existing vulnerabilities and to more quickly identify and fix newly disclosed vulnerabilities when they affect your project. <hr/> - The recommended version is **1 version** ahead of your current version. - The recommended version was released **a month ago**. <details> <summary><b>Release notes</b></summary> <br/> <details> <summary>Package name: <b>chart.js</b></summary> <ul> <li> <b>4.5.0</b> - <a href="https://redirect.github.com/chartjs/Chart.js/releases/tag/v4.5.0">2025-06-14</a></br><h1>Essential Links</h1> <ul> <li><a href="https://www.npmjs.com/package/chart.js" rel="nofollow">npm</a></li> <li><a href="https://www.chartjs.org/docs/4.5.0/migration/v4-migration.html" rel="nofollow">Migration guide</a></li> <li><a href="https://www.chartjs.org/docs/4.5.0/" rel="nofollow">Docs</a></li> <li><a href="https://www.chartjs.org/docs/4.5.0/api/" rel="nofollow">API</a></li> <li><a href="https://www.chartjs.org/docs/4.5.0/samples/information.html" rel="nofollow">Samples</a></li> </ul> <h2>Enhancements</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1870361497" data-permission-text="Title is private" data-url="chartjs/Chart.js#11470" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/11470/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/11470">#11470</a> Fix min js two renames</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2966641895" data-permission-text="Title is private" data-url="chartjs/Chart.js#12054" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12054/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12054">#12054</a> [fix] #12048 - Add selfJoin option for doughnut graphs</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2992511358" data-permission-text="Title is private" data-url="chartjs/Chart.js#12058" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12058/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12058">#12058</a> feature: make above/below colors in filler plugin work with pivoted line charts</li> </ul> <h2>Bugs Fixed</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3045985317" data-permission-text="Title is private" data-url="chartjs/Chart.js#12070" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12070/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12070">#12070</a> Fix: display stacked bar with multiple x-Axis</li> </ul> <h2>Types</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3000100733" data-permission-text="Title is private" data-url="chartjs/Chart.js#12064" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12064/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12064">#12064</a> Document requirement of 'this' when calling tick formatters</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2997699156" data-permission-text="Title is private" data-url="chartjs/Chart.js#12063" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12063/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12063">#12063</a> Add type definition for beforeEvent changed</li> </ul> <h2>Documentation</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3137696049" data-permission-text="Title is private" data-url="chartjs/Chart.js#12088" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12088/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12088">#12088</a> Fix grammar typo: 'only draw line' -> 'only draw lines'</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3135889497" data-permission-text="Title is private" data-url="chartjs/Chart.js#12087" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12087/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12087">#12087</a> Fix minified URL for development builds in the docs</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3127790559" data-permission-text="Title is private" data-url="chartjs/Chart.js#12084" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12084/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12084">#12084</a> fix: correct grammar in segment comments (an segment -> a segment)</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3107078825" data-permission-text="Title is private" data-url="chartjs/Chart.js#12082" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12082/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12082">#12082</a> Update docs for Developers API</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3018509008" data-permission-text="Title is private" data-url="chartjs/Chart.js#12068" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12068/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12068">#12068</a> Fix minor typo, missing period</li> </ul> <h2>Development</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3146667294" data-permission-text="Title is private" data-url="chartjs/Chart.js#12090" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12090/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12090">#12090</a> bump version to 4.5.0</li> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2841166263" data-permission-text="Title is private" data-url="chartjs/Chart.js#12025" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12025/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12025">#12025</a> Bump pnpm/action-setup from 4.0.0 to 4.1.0</li> </ul> <p>Thanks to <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/FabTechAT/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/FabTechAT">@ FabTechAT</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/Jee-on/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/Jee-on">@ Jee-on</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/LeeLenaleee/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/LeeLenaleee">@ LeeLenaleee</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/R2Jeu-prive/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/R2Jeu-prive">@ R2Jeu-prive</a>, <a class="user-mention notranslate" data-hovercard-type="organization" data-hovercard-url="/orgs/dependabot/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/dependabot">@ dependabot</a>[bot], <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ffdm/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/ffdm">@ ffdm</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/fulldecent/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/fulldecent">@ fulldecent</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/joshkel/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/joshkel">@ joshkel</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/lucasleung8/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/lucasleung8">@ lucasleung8</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/nakanoh/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/nakanoh">@ nakanoh</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/xavierleune/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/xavierleune">@ xavierleune</a> and <a href="https://redirect.github.com/apps/dependabot">dependabot[bot]</a></p> </li> <li> <b>4.4.9</b> - <a href="https://redirect.github.com/chartjs/Chart.js/releases/tag/v4.4.9">2025-04-15</a></br><h1>Essential Links</h1> <ul> <li><a href="https://www.npmjs.com/package/chart.js" rel="nofollow">npm</a></li> <li><a href="https://www.chartjs.org/docs/4.4.9/migration/v4-migration.html" rel="nofollow">Migration guide</a></li> <li><a href="https://www.chartjs.org/docs/4.4.9/" rel="nofollow">Docs</a></li> <li><a href="https://www.chartjs.org/docs/4.4.9/api/" rel="nofollow">API</a></li> <li><a href="https://www.chartjs.org/docs/4.4.9/samples/information.html" rel="nofollow">Samples</a></li> </ul> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2870946621" data-permission-text="Title is private" data-url="chartjs/Chart.js#12037" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12037/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12037">#12037</a> Update docs</li> </ul> <h2>Bugs Fixed</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2990870549" data-permission-text="Title is private" data-url="chartjs/Chart.js#12057" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12057/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12057">#12057</a> fix: respect dataset clipping area when filling line charts</li> </ul> <h2>Documentation</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2878759649" data-permission-text="Title is private" data-url="chartjs/Chart.js#12039" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12039/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12039">#12039</a> Add docs on using from Node.js</li> </ul> <h2>Development</h2> <ul> <li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2996393290" data-permission-text="Title is private" data-url="chartjs/Chart.js#12062" data-hovercard-type="pull_request" data-hovercard-url="/chartjs/Chart.js/pull/12062/hovercard" href="https://redirect.github.com/chartjs/Chart.js/pull/12062">#12062</a> Bump version to 4.4.9</li> </ul> <p>Thanks to <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/LeeLenaleee/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/LeeLenaleee">@ LeeLenaleee</a>, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/adrianbrs/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/adrianbrs">@ adrianbrs</a> and <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/joshkel/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://redirect.github.com/joshkel">@ joshkel</a></p> </li> </ul> from <a href="https://redirect.github.com/chartjs/Chart.js/releases">chart.js GitHub release notes</a> </details> </details> --- > [!IMPORTANT] > > - Check the changes in this PR to ensure they won't cause issues with your project. > - This PR was automatically created by Snyk using the credentials of a real user. --- **Note:** _You are seeing this because you or someone else with access to this repository has authorized Snyk to open upgrade PRs._ **For more information:** <img src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiIyMWM4YzRjYy01Y2I1LTQyZWUtYmVkMS04YjRhYjM0OTg2ZjgiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6IjIxYzhjNGNjLTVjYjUtNDJlZS1iZWQxLThiNGFiMzQ5ODZmOCJ9fQ==" width="0" height="0"/> > - 🧐 [View latest project report](https://app.snyk.io/org/dawoudio/project/e27b08aa-e5d2-4b10-8303-630a69d0b669?utm_source=github&utm_medium=referral&page=upgrade-pr) > - 📜 [Customise PR templates](https://docs.snyk.io/scan-using-snyk/pull-requests/snyk-fix-pull-or-merge-requests/customize-pr-templates?utm_source=&utm_content=fix-pr-template) > - 🛠 [Adjust upgrade PR settings](https://app.snyk.io/org/dawoudio/project/e27b08aa-e5d2-4b10-8303-630a69d0b669/settings/integration?utm_source=github&utm_medium=referral&page=upgrade-pr) > - 🔕 [Ignore this dependency or unsubscribe from future upgrade PRs](https://app.snyk.io/org/dawoudio/project/e27b08aa-e5d2-4b10-8303-630a69d0b669/settings/integration?pkg=chart.js&utm_source=github&utm_medium=referral&page=upgrade-pr#auto-dep-upgrades) [//]: # 'snyk:metadata:{"customTemplate":{"variablesUsed":[],"fieldsUsed":[]},"dependencies":[{"name":"chart.js","from":"4.4.9","to":"4.5.0"}],"env":"prod","hasFixes":false,"isBreakingChange":false,"isMajorUpgrade":false,"issuesToFix":[],"prId":"21c8c4cc-5cb5-42ee-bed1-8b4ab34986f8","prPublicId":"21c8c4cc-5cb5-42ee-bed1-8b4ab34986f8","packageManager":"npm","priorityScoreList":[],"projectPublicId":"e27b08aa-e5d2-4b10-8303-630a69d0b669","projectUrl":"https://app.snyk.io/org/dawoudio/project/e27b08aa-e5d2-4b10-8303-630a69d0b669?utm_source=github&utm_medium=referral&page=upgrade-pr","prType":"upgrade","templateFieldSources":{"branchName":"default","commitMessage":"default","description":"default","title":"default"},"templateVariants":[],"type":"auto","upgrade":[],"upgradeInfo":{"versionsDiff":1,"publishedDate":"2025-06-14T20:42:08.165Z"},"vulns":[]}'
Hello everyone,
When you try to use stackedBars with multiple axes, the bars all render at the center of each axis chunk, so they're hidden by whichever bar is rendered last.
You can see a live example on codepen
This pull request fixes this behavior. It counts the number of x axes and adjusts size and positions according to that number.
As this is my first pull request on this project, I may have missed something. If that's so, please tell me.
Have a nice day !