Skip to content

In line chart when interaction mode is dataset, fade animation is missing when legend item is clicked #10882

Done

Description

@pratheekbhandary

Expected behavior

In a line chart, when legend item is clicked, respective line should fade in/out.

Current behavior

In a specific scenario (mentioned in below section), when legend item is clicked, respective line does not fade but suddenly disappears. This happens when interaction mode is set to dataset with intersect set to false.

Reproducible sample

https://codepen.io/pratheekbhandary/pen/LYrjwGy

Optional extra steps/info to reproduce

Steps to reproduce

  • For a line chart, set below options.
const options =  options: {
    interaction: {
      mode: "dataset",
      intersect: false,
    },
  }
  • Hover over the graph so that the tooltip is visible.
  • Now try hiding the line for which the tooltip is shown.
  • Instead of fading away it abruptly go off the screen.
  • It can also be seen that if tooltip is not active, then the line fades away normally.

Even disabling the tooltip does not seem to add fade animation.
Please check the attached video recording attached.
https://user-images.githubusercontent.com/30550942/202433430-1d104575-1a0e-4a78-b1b2-d49e8f71c89a.mov

Possible solution

No response

Context

Below interaction setting was used to achieve drill-down mechanism in line chart i.e. when user clicks on/near the line data will be switched to show another line chart.

chart.js version

v3.8.0 (also reproduced in 4.0.1)

Browser name and version

Chrome v107.0.5304.110

Link to your project

No response

Activity

  1. changed the title [-]In line chart, fade animation is missing when legend item is clicked and `interaction` mode is `dataset`[/-] [+]In line chart when `interaction` mode is `dataset`, fade animation is missing when legend item is clicked[/+] on Nov 17, 2022
  2. stockiNail commented on Jan 16, 2023

    @stockiNail
    Contributor

    @pratheekbhandary as you outlined, this is also happening in version 4.

    What is generating this behavior is in the following code:

    if (!inChartArea) {
    // Let user control the active elements outside chartArea. Eg. using Legend.
    return lastActive;
    }

    When event is out of chart area (when you hover the legend), the previous active elements are returned.
    It seems something is working as designed (see comment).

  3. guanzo commented on Dec 29, 2023

    @guanzo

    I'm experiencing the same thing with an HTML legend. If I change the dataset visibility with setDatasetVisibility, there's no fade animation for the line, it just blinks in/out. However the other existing lines do animate their position.

    Found a solution:

    Broken code from HTML legend example:

        // no animation
        chart.setDatasetVisibility(item.datasetIndex, !chart.isDatasetVisible(item.datasetIndex));
        chart.update();

    Working code:

        if (chart.isDatasetVisible(item.datasetIndex)) {
            chart.hide(item.datasetIndex); // has animation
        } else {
            chart.show(item.datasetIndex); // has animation
        }
       // chart.update() called by hide()/show()

    Found the solution by browsing the default legend plugin code:

    onClick(e, legendItem, legend) {
    const index = legendItem.datasetIndex;
    const ci = legend.chart;
    if (ci.isDatasetVisible(index)) {
    ci.hide(index);
    legendItem.hidden = true;
    } else {
    ci.show(index);
    legendItem.hidden = false;
    }
    },

    It appears that explicitly setting legendItem.hidden = true/false is redundant, as show()/hide() eventually sets hidden.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions