Skip to content
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

Zero values skipped on logarithmic scale line chart #9629

Open
flesher1 opened this issue Sep 7, 2021 · 8 comments
Open

Zero values skipped on logarithmic scale line chart #9629

flesher1 opened this issue Sep 7, 2021 · 8 comments

Comments

@flesher1
Copy link

flesher1 commented Sep 7, 2021

Expected Behavior

A line chart should be able to display values of 0 along the bottom axis and the y axis should start at zero if the y scale is logarithmic.

Current Behavior

Values of 0 on a logarithmic scale line chart are not shown on the chart. min:0 and beginAtZero: true both appear to have no effect on the issue that 0 does not appear on the line chart.

This functionality appears to be working in v2. In v3.0.2 it somewhat works by displaying the 0 points at the bottom of the chart, however the scale is still not set to 0.

Steps to Reproduce

https://codepen.io/flesher1/pen/WNORQoQ

Provide a dataset containing 0s. Use a line graph with logarithmic scale. Copying example below:

var ctx = document.getElementById("chart").getContext("2d");
var chart = new Chart(ctx, {
  type: "line",
  data: {
    labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    datasets: [
      {
        backgroundColor: "rgb(75, 192, 192, 0.5)",
        data: [2000, 400, 10, 1, 0, 0.001, 0.5],
      }
    ]
  },
  options: {
    scales: {
      xAxes: [{
        type: 'category'
      }],
      y: {
        type: 'logarithmic',
        beginAtZero: true
      }
    }
  }
});

Context

In this case, I have Data that contains 10s, 20s, 0s, and numbers around 80,000.

Environment

  • Chart.js version: 3.1.0 and newer
  • Browser name and version: Chrome
@kurkle
Copy link
Member

kurkle commented Sep 8, 2021

log(0) is undefined. Distance between any positive value (0.001 in your case) and 0 is infinite. How do you think it should be plotted?
For example, replacing the 0 with 1E-20 which still is nowhere near 0, the graph is completely different:
https://codepen.io/kurkle/pen/powRWQo

image

@flesher1
Copy link
Author

flesher1 commented Sep 8, 2021

Yes, Log(0) is undefined, and there are infinite points between 0 and the next lowest data value; however, 0 should be considered a valid data value on the graph given that we are not taking the log of the data value, but changing the rate of growth of the y axis. In your example, we could change the min to any point between the lowest data value and 0, but it would completely change the log growth of the y scale.

As for possible solutions, are the methods that were used previously no longer available? It looks like there was a solution created for version 2.7 to create a 0 tick on the graph. #4913

@kurkle
Copy link
Member

kurkle commented Sep 8, 2021

Fake it: https://codepen.io/kurkle/pen/WNORmJr

@flesher1
Copy link
Author

flesher1 commented Sep 9, 2021

Okay, that solves the issue for my use case. Thanks for the help.

For people running across this thread, I updated all 0 values to 0.1, set the min of the graph to 0.1, changed the scale tick label to 0 instead of 0.1, and updated the tooltips to map the value back to 0. Not an ideal solution, and is hacky, but works for the use case I have. It would only work since I know the data I have will be integers and not floats, otherwise changing the value this way probably wouldn’t work.

@leeoniya
Copy link

leeoniya commented Sep 9, 2021

Fake it: https://codepen.io/kurkle/pen/WNORmJr

❤️ 🤣

uPlot takes a slightly different approach and renders <= 0 an extra order of magnitude below the plot edge (see bottom chart in https://leeoniya.github.io/uPlot/demos/log-scales2.html). but you can "fake it" there, too.

@lz000
Copy link

lz000 commented Oct 4, 2021

I'm having the same issue. But I don't want to use this workaround as it will show value 0.1 when I hover on it, but I want it to show 0. Also my dataset may have any number between 0-1 so I can't change 0 to other number which will cause conflict. Any plan to fix this?

@skalimer0
Copy link

Hello,
Zeros values are displayed on ChartJS 2.9.x

log10 of 0 is -infinity... The zero value need to be compute in exception case

@skalimer0
Copy link

For now, points at 0 are skipped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants