Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Zero values skipped on logarithmic scale line chart

Open flesher1 opened this issue 4 years ago • 11 comments

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

flesher1 avatar Sep 07 '21 19:09 flesher1

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

kurkle avatar Sep 08 '21 09:09 kurkle

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. https://github.com/chartjs/Chart.js/pull/4913

flesher1 avatar Sep 08 '21 19:09 flesher1

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

kurkle avatar Sep 08 '21 20:09 kurkle

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.

flesher1 avatar Sep 09 '21 18:09 flesher1

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

:heart: :rofl:

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.

leeoniya avatar Sep 09 '21 18:09 leeoniya

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?

lz000 avatar Oct 04 '21 04:10 lz000

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 avatar Sep 23 '22 09:09 skalimer0

For now, points at 0 are skipped

skalimer0 avatar Sep 23 '22 14:09 skalimer0

Hello,

Same problem on version 3.9.1

skalimer0 avatar Jun 03 '24 14:06 skalimer0

And also in 4.4.3 version

image

skalimer0 avatar Jun 03 '24 14:06 skalimer0

I am too affected by this issue. I understand the fact that mathematically speaking log(0) is -infinity and if we were to be strictly mathematically correct, the distance between any epsilon>0 and -infinity on the scale should be infinite.

However, from a user perspective, I don't care about that. I just want a 0 line in the chart with a very small distance between it and the next >0 value. This is certainly doable and would be very helpful. It doesn't have to be 100% mathematically accurate, I can accept a small error below the smallest positive value for the sake of having the lines uninterrupted and able to show 0 values.

The value in user experience that this would bring greatly outweighs the theoretical inaccuracy.

bogdanionitabp avatar Jul 31 '24 09:07 bogdanionitabp