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

Tooltip looks incorrect on stacked bar chart with sparse times #9468

Closed
FlukeFan opened this issue Jul 25, 2021 · 10 comments
Closed

Tooltip looks incorrect on stacked bar chart with sparse times #9468

FlukeFan opened this issue Jul 25, 2021 · 10 comments

Comments

@FlukeFan
Copy link

Expected Behavior

Attempting to stack values on a bar chart, where the data x value has sparse time values.

The chart draws correctly I think, but the tooltip is not correct when hovering the mouse over the chart.
The data for 1st May should total 30 (10 + 20);
The data for 2nd May should total 50 (20 + 30);
The data for 5th May should total 40 (10 + 30).

The tooltip should show the combined stacked values when hovered over.

image

Current Behavior

The tooltip is showing incorrect values when hovering over a stacked-bar:
image

(The x-axis labels also look a little suspect, it's missing the first label - so I'm curious if I'm doing something wrong here?):
image

Steps to Reproduce

got to: https://jsfiddle.net/ajgqkvcf/1/
Hover over the first (or any) stacked bar, and the tooltip looks incorrect.

Environment

var ctx = document.getElementById('myChart').getContext('2d');

new Chart(ctx, {
  type: 'bar',
  data: {
    datasets: [{
      label: 'Amount1',
      backgroundColor: '#004c6d',
      data: [{
          x: '2010-05-01T00:00:00Z',
          y: 10
        },
        {
          x: '2010-05-02T00:00:00Z',
          y: 20
        }
      ]
    }, {
      label: 'Amount2',
      backgroundColor: '#b3cce2',
      data: [{
          x: '2010-05-02T00:00:00Z',
          y: 30
        },
        {
          x: '2010-05-05T00:00:00Z',
          y: 10
        }
      ]
    }, {
      label: 'Amount3',
      backgroundColor: '#547c9a',
      data: [{
          x: '2010-05-01T00:00:00Z',
          y: 20
        },
        {
          x: '2010-05-05T00:00:00Z',
          y: 30
        },
      ]
    }]
  },
  options: {
    plugins: {
      legend: {
        display: false
      }
    },
    scales: {
      x: {
        stacked: true,
        type: 'time',
        time: {
          unit: 'day',
          displayFormats: {
            day: 'YYYY-MMM-DD'
          }
        }
      },
      y: {
        stacked: true,
      }
    }
  }
});
@LeeLenaleee
Copy link
Collaborator

Setting a different tooltip mode resolves your issue: https://jsfiddle.net/Leelenaleee/8oLgvu41/1/
Since it defaults to index it seems wrong

@FlukeFan
Copy link
Author

Oh perfect, thanks - I hadn't seen that option.

So is the default behaviour correct? (i.e., this isn't a bug?)

@FlukeFan
Copy link
Author

(btw, I tried mode: 'point', and that seems much closer to what I'm after - thanks again.)

@LeeLenaleee
Copy link
Collaborator

No the behaviour itself is not a bug since its taking the index in the data array so even though its linked to a different label its at the same index in the data array.

@FlukeFan
Copy link
Author

OK cool, thanks.

@LeeLenaleee
Copy link
Collaborator

@FlukeFan read over the part that first label is not showing, this does seem like a bug so you might want to reopen this issue again or create a new one so its a bit more clear since in the samples it also seems like the first label is missing: https://www.chartjs.org/docs/master/samples/scales/time-max-span.html

@FlukeFan
Copy link
Author

@LeeLenaleee thanks - I've opened it as a new issue: #9470

@ex5
Copy link

ex5 commented Dec 18, 2023

Setting a different tooltip mode resolves your issue

mode: "index" appears to be the only way to display all of the dataset belonging to a single stacked bar, so changing it doesn't seem like a solution at all.
Why is this considered "not a bug"?

@LeeLenaleee
Copy link
Collaborator

Because mode index takes the value at the index in the data array so it does not look at the the specific mapping you give in the object notation. If you use mode x it will work as expected

@ex5
Copy link

ex5 commented Dec 18, 2023

@LeeLenaleee actually, you are right, x appears to do the trick on the latest chart.js >_<

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

3 participants