Skip to content

Dynamically showing/hiding labels in response to mouse events #589

@joshkel

Description

@joshkel

I set up mouse enter and leave events to dynamically show labels when the user mouses over the annotation:

    enter({ chart, element }) {
      if ('label' in element.options && element.options.label) {
        element.options.label.enabled = true;
        chart.draw();
      }
    },
    leave({ chart, element }) {
      if ('label' in element.options && element.options.label) {
        element.options.label.enabled = false;
        chart.draw();
      }
    },

This was based off of the old horizontal-line.html sample.

This worked fine until chartjs-plugin-annotation 1.2.0 - specifically, #547. If I'm understanding correctly, the changes in #547 cause labelVisible to be precalculated so that it no longer responds to changes in enabled.

I tried fixing this by calling chart.update() instead of chart.draw(), but things still didn't work. I think the code is using its options proxy and context to retrieve the original annotation options object, instead of the dynamically updated element options, but I'm not sure that I fully understand how contexts and options proxies work.

I can directly access the original chart options, but that feels ugly (if using TypeScript, due to its optional properties, and if annotations are in an array instead of keyed by unique ID):

        chart.options.plugins.annotation.annotations[0].label.enabled = true;

Is dynamically updating element options supported? What's the proper way to do it?

Thank you.

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