question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart.js axes and gridlines get messed up when changing axis type from time scale to linear/logarithmic scale

See original GitHub issue

Expected behavior

When I switch from a time scale axis to a linear or logarithmic scale, the axes should be properly formatted. When switching from time to logarithmic, the graph should look like this: image

Current behavior

When the axis type is changed from linear/logarithmic to time, everything works perfectly. However, when I change a time scale axis to linear/logarithmic, the tick numbers are incorrect and the axes get visually messed up.

Here is what the graph looks like with a time scale axis: image

And then, what the graph looks like one the user changes to logarithmic axes: image

This issue also occurs when switching to a linear axis type but it’s less noticeable.

Reproducible sample

https://codepen.io/jack2104/pen/zYPxLZd

Optional extra steps/info to reproduce

Note how the log axis is (admittedly slightly) messed up when switching (on the right side of the graph, the top and bottom gridlines are cut off). To see what it should look like, set the axis type to logarithmic within myChart and refresh the pen (from there, you can also toggle the axis type to time and see how the ticks are messed up).

All I’m doing in my actual project is using vue to change the axis type and then refreshing the chart:

// Chart reference
const chartRef = ref(null);

// Watch the vuex store value that holds the axis type
watch(() => store.state.chart_settings.xAxis["scale"], () => {
    // Set the axis type in the chart settings
    chart.options.scales.xAxis.type = store.state.chart_settings.xAxis["scale"];
    
    // Update the chart  
    chartRef.value.update();
});

const chart = {
    ...
    options {
        scales {
            xAxis: {
                type: "time", // Changes when vuex store value changes
            }
        }
    }
    ...
}

Then, as can be seen above, when the vuex store value changes for the scale type, the chart settings are updated and the chart canvas is refreshed.

Possible solution

This doesn’t happen when switching from logarithmic to linear and vice versa, so I know it’s something to do with the switch from a numerical axis to a time scale axis. I have any idea how to fix it though…

chart.js version

3.6.2

Browser name and version

Chrome 97.0.4692.99

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
etimbergcommented, Jul 28, 2022

A documentation fix sounds fine to me. @kurkle @LeeLenaleee any objections?

1reaction
thabarbadoscommented, Jul 29, 2022

Hello @Jack2104, I tried to fix this bug and found it easy to set with an additional option in the scale config. The chart will redraw correctly if you add bounds: 'ticks' in the scale options. You can see the result here. I just added a line to your codepen.

@etimberg do you think adding info about this to the docs is necessary?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linear Axis - Chart.js
Linear Axis. The linear scale is used to chart numerical data. It can be placed on ... This allows changing the type of...
Read more >
Axes | Chart.js
Type of scale being employed. Custom scales can be created and registered with a string key. This allows changing the type of an...
Read more >
Time Cartesian Axis - Chart.js
When changing the scale type from Time scale to Logarithmic/Linear scale, you need to add bounds: 'ticks' to the scale options. Changing the...
Read more >
Cartesian Axes - Chart.js
Type of scale being employed. Custom scales can be created and registered with a string key. This allows changing the type of an...
Read more >
Logarithmic Axis - Chart.js
The logarithmic scale is used to chart numerical data. It can be placed on either the x or y-axis. As the name suggests,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found