amcharts5 icon indicating copy to clipboard operation
amcharts5 copied to clipboard

V5: overwrite legend click event

Open flaming-cl opened this issue 1 year ago • 3 comments

Hey for V5, do you know how to replace each legend label's click event with our own user event?

flaming-cl avatar Oct 11 '24 20:10 flaming-cl

You can disable built-in toggle events by setting clickTarget: "none": https://www.amcharts.com/docs/v5/concepts/legend/#disabling-toggling

Then you can just add your own click events:

leged.itemContainers.template.events.on("click", function(ev) {
  console.log(ev.target);
});

martynasma avatar Oct 12 '24 06:10 martynasma

Thanks! clickTarget: "none" also works if I set it with JSON config for legend.settings, right?

flaming-cl avatar Oct 12 '24 16:10 flaming-cl

Thanks! clickTarget: "none" also works if I set it with JSON config for legend.settings, right?

Yes.

martynasma avatar Oct 12 '24 18:10 martynasma

doesn't seem to work while setting up legend.itemContainers.template.events

flaming-cl avatar Nov 04 '24 02:11 flaming-cl

This is because by the time you add the event, item containers are already created.

You may need to add to actual items:

legend.itemContainers.each(function(container) {
  container.events.on("click", .....);
});

martynasma avatar Nov 04 '24 08:11 martynasma

Thanks the above solution works great!

One small question on clickTarget: "none": this also disables the hovering (curser: pointer 👆) effect on legend.

Do you know how to show the cursor while disable the original click event?

flaming-cl avatar Nov 05 '24 00:11 flaming-cl

itemContainers: {
  properties: {
    template: {
      settings: {
        x: { type: 'Percent', value: 0 },
        centerX: { type: 'Percent', value: 10 },
        width: { type: 'Percent', value: 98 },
        height: 50,
        cursorOverStyle: "pointer",
        interactive: true
      }
    }
  }
},

martynasma avatar Nov 05 '24 06:11 martynasma