chartist-js / chartist

Simple responsive charts
https://chartist.dev
MIT License
13.34k stars 2.53k forks source link

Trouble Capturing Right Click Event #1232

Closed BPortfolio closed 1 year ago

BPortfolio commented 4 years ago

Thank you for this library!

I am having trouble capturing the right click event.

I tried adding the right click event, "contextmenu", using the api below. https://gionkunz.github.io/chartist-js/api-documentation.html#chartistevent-function-addeventhandler

canvas.eventEmitter.addEventHandler("contextmenu", function(){alert('Handle Context Menu')});

This code compiles but does not work.
I know that it needs the eventEmitter object. Without it, it doesn't compile.

I was able to add a left click event on the point on the graph with the code below.

canvas.on("created", function () { // attach the necessary events to the nodes: //$('.ct-chart-line .ct-point').click(function () { $('.ct-chart-line .ct-point').click(function () { alert('EventCreated'); var val = $(this).attr("ct:value"); console.log(val); });
});

However, I was not able to add a right click event with the approach below.

canvas.on("created", function () { $('contextmenu').click(function () { alert('Right Click Event'); console.log('Right Click Event'); }); });

I am looking for suggestions. To be clear, I am testing the right click event with an alert message. I am not trying to pop-up a menu. My full code is below.

Arantiryo commented 2 years ago

Hi @BPortfolio 👋

Something like this should work to capture the right click event on ct-points:

chart.on("created", () => {
  const elems = document.querySelectorAll(".ct-point");

  elems.forEach((el) => {
    el.addEventListener("contextmenu", () => console.log("Right Click"));
  });
});

Here's a code sandbox with a small example.