You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also, I found out this bug produces "non-stack bars drawn off canvas" bug #5312 .
Therefore fixing this issue will also fix that.
Possible Solution
This looks like nasty bug. Long source code explanation below.
Notice that in source code, there are default options for "chart type" (config.type e.g. 'bar', 'doughnut'), and also for "scale type" (config.option.scales[i].type e.g. 'category', 'time', 'linear').
"User config" is what we put in chart constructor new Chart(ctx, config).
And currently there are only two locations in which default value is written:
If "user config" has only one scale and its type is 'category' (or has no scale option), the flow goes to else statement.
Otherwise, it goes into if statement, and "user config" is merged into "scale type default" (which has offset: false).
Ta-da! So now "user config" has offset: false (even if you didn't specify), before actually merging with "chart type default",
which has offset: true (if chart type is 'bar'), but overwritten to false.
The pain is that this is an expected behavior in general if you think of other cases.
I couldn't think of any elegant solution (which is why I posted as an issue instead of PR) but two dirty workarounds:
Exceptional precedence: Manually add offset: true to all scales in "user config" (config.options) if its chart type is 'bar'
Only one default: Make default offset false even if chart type is 'bar' (which I think requires major version up, and worsen UX)
I think we can come up with much better solution than these. Please help.
The text was updated successfully, but these errors were encountered:
Expected Behavior
Default value of
offset
option (for scale) when chart is barchart should betrue
. See documentationCurrent Behavior
It only defaults to
true
, iff it is the first scale (inscales
list), and its type is 'category' (not 'time').Otherwise it defaults to
false
if user doesn't specify the option. (If specified, it works as expected.)Example:
First 'category' scale WORKS (JSFiddle)
Second 'category' scale FAILS (JSFiddle)
First 'time' scale FAILS (JSFiddle)
Also, I found out this bug produces "non-stack bars drawn off canvas" bug #5312 .
Therefore fixing this issue will also fix that.
Possible Solution
This looks like nasty bug. Long source code explanation below.
Notice that in source code, there are default options for "chart type" (
config.type
e.g. 'bar', 'doughnut'), and also for "scale type" (config.option.scales[i].type
e.g. 'category', 'time', 'linear')."User config" is what we put in chart constructor
new Chart(ctx, config)
.And currently there are only two locations in which default value is written:
true
codefalse
codeIt looks weird, and this is causing the bug.
Explanation
Let's think of case when we are merging "chart type default"(
defaults[config.type]
) and "user config"(config.options
):Chart.js/src/core/core.controller.js
Lines 38 to 41 in a0a195f
See the following code called inside the function
helpers.scaleMerge()
:Chart.js/src/core/core.helpers.js
Lines 52 to 59 in a0a195f
If "user config" has only one scale and its type is 'category' (or has no scale option), the flow goes to
else
statement.Otherwise, it goes into
if
statement, and "user config" is merged into "scale type default" (which hasoffset: false
).Ta-da! So now "user config" has
offset: false
(even if you didn't specify), before actually merging with "chart type default",which has
offset: true
(if chart type is 'bar'), but overwritten tofalse
.The pain is that this is an expected behavior in general if you think of other cases.
I couldn't think of any elegant solution (which is why I posted as an issue instead of PR) but two dirty workarounds:
offset: true
to all scales in "user config" (config.options
) if its chart type is 'bar'offset
false even if chart type is 'bar' (which I think requires major version up, and worsen UX)I think we can come up with much better solution than these. Please help.
The text was updated successfully, but these errors were encountered: