chartjs-plugin-crosshair icon indicating copy to clipboard operation
chartjs-plugin-crosshair copied to clipboard

Sync is not working

Open erkanisuf opened this issue 3 years ago • 26 comments

I am using chartjs version 3.7 , plugin version 1.2.0 and react-chartjs version 4.0.0 . The sync is not workinng for some reason .

Import and registration :

import {CrosshairPlugin, Interpolate} from 'chartjs-plugin-crosshair'; Chart.register(CrosshairPlugin); Interaction.modes.interpolate = Interpolate

options setup:

crosshair: { line: { color: '#F66', // crosshair line color width: 1 // crosshair line width }, sync: { enabled: true, // enable trace line syncing with other charts group: 1, // chart group suppressTooltips: false // suppress tooltips when showing a synced tracer }, zoom: { enabled: true, // enable zooming zoomboxBackgroundColor: 'rgba(66,133,244,0.2)', // background color of zoom box zoomboxBorderColor: '#48F', // border color of zoom box zoomButtonText: 'Reset Zoom', // reset zoom button text zoomButtonClass: 'reset-zoom', // reset zoom button class }, callbacks: { beforeZoom: () => function (start, end) { // called before zoom, return false to prevent zoom return true; }, afterZoom: () => function (start, end) { // called after zoom } } }

I used two identical charts with exacly same props/data. But the sync is gone. When i have used the plugin with chartjs before version 3v and plugin version 1.1.2 , the sync has worked. Am i missing something or is this some kind of bug?

erkanisuf avatar Jan 10 '22 17:01 erkanisuf

Ayo, Download the unminified file, and change where new event is defined to this: native: { buttons: buttons, type: e.original.type == "click" ? "mousemove" : e.original.type }, It loses context between charts. You're welcome

geriatricdan avatar Feb 01 '22 20:02 geriatricdan

Hit the same issue.

It appears that sync is working on chartjs version 3.4.x but not newer versions. I was able to pin chartjs to ~3.4.1 to work around this temporarily.

andreialecu avatar Mar 15 '22 12:03 andreialecu

type: e.original.type == "click" ? "mousemove" : e.original.type

this worked for me. very nice

savejeff avatar Mar 16 '22 16:03 savejeff

Ayo, Download the unminified file, and change where new event is defined to this: native: { buttons: buttons, type: e.original.type == "click" ? "mousemove" : e.original.type }, It loses context between charts. You're welcome

Thanks, it's work in "chart.js": "^3.7.1", "chartjs-plugin-crosshair": "^1.2.0" Снимок экрана 2022-04-01 115611

Bodrosh avatar Apr 01 '22 08:04 Bodrosh

@Bodrosh I am also facing the same issue. Can you please upload an example on codesandbox with react-chartjs ?

cmark1302 avatar Apr 13 '22 01:04 cmark1302

@geriatricdan Thank you so much :) I was stuck with this and you saved my time. Any idea why reset zoom button was placed below instead of on top of the graph ?

image

cmark1302 avatar Apr 13 '22 06:04 cmark1302

@geriatricdan possibly PR so they can put this fix in the official plugin 👀

erkanisuf avatar Apr 13 '22 17:04 erkanisuf

@erkanisuf Does zoom works ? graph not showing up after zoom. it goes away

cmark1302 avatar Apr 15 '22 17:04 cmark1302

I am not using the zoom but yes its feels wierd and buggy

erkanisuf avatar Apr 15 '22 17:04 erkanisuf

I have done a range of fixes and improvements to the chartjs-plugin-crosshair.js file. It works out of the box with the latest version of chart.js. I don't thing the Owner of the code is maintaining it anymore or is doing PR reviews. If anybody wants to try out me "fork" of the code hier it is

chartjs-plugin-crosshair.zip

savejeff avatar Apr 15 '22 17:04 savejeff

@savejeff It looks zoom doesn't work with above attached file.

cmark1302 avatar Apr 15 '22 17:04 cmark1302

what kind of zoom are you talking about? If you are talking about the zoom where you click and drag sideways to zoom into the area draged over, this works perfectly find with the code in the zip file. i use it daily. Other zoom like pitch and scroll zoom are not implemented in this library.

check this: do you have the latest version of chats.js is your code correct? check the code from the examples does it give you any errors in the console ?

i would start looking for the fault in you code.

savejeff avatar Apr 15 '22 18:04 savejeff

No errors on console. but when i zoom, line chart vanished. After reset zoom click, i can see the line chart.

I am using latest version of charts.js react-chart-2.js

cmark1302 avatar Apr 15 '22 18:04 cmark1302

dont know about react. i can assure you, that it works with chart.js 3.7.1.

savejeff avatar Apr 16 '22 14:04 savejeff

@savejeff Thanks for your zip - It syncs perfect also with 3.8.0.. I have one problem: I cannot switch on/off the datasets by clicking on the smbol and text in the legend (sync or w/o sync). Unbenannt Now I made more tests: I want that the tooltips on the last datapoint is shwon on the first call of the chart. When I add this code to my config file, the tooltip is shown, but then the problem with legend happens. you can see this effect at www.zebrafell.de/test.html. Without this plugins code the click to the legend works.. Do you have an idea what I can do?

    plugins: [
      {
        afterDatasetUpdate: (chart) => {
          if (!chart.tooltip.getActiveElements().length) {
            sets = [];
            for (let index = 0; index < data.datasets.length; index++) {
              sets[index] = {
                datasetIndex: index,
                index: chart.data.datasets[index].data.length - 1,
              };
            }
            chart.tooltip.setActiveElements(sets);
            chart.update();
          }
        },
      },
    ],

I find finally the solution: I deleted the chart.update() Problem is solved!!

GeoBau avatar Jul 09 '22 22:07 GeoBau

@cmark1302 sorry, did you manage to find a working solution for react-chart-2? Thanks.

sarates avatar Jan 26 '23 07:01 sarates

It seems that the fix by @geriatricdan is already contained in the repository code, but at the wrong place:

var newEvent = {
  // REMOVED FROM HERE
  // type: e.original.type == "click" ? "mousemove" : e.original.type,  // do not transmit click events to prevent unwanted changing of synced charts. We do need to transmit a event to stop zooming on synced charts however.
  chart: chart,
  x: xScale.getPixelForValue(e.xValue),
  y: e.original.y,
  native: {
    // INSERTED HERE
    type: e.original.type == "click" ? "mousemove" : e.original.type,  // do not transmit click events to prevent unwanted changing of synced charts. We do need to transmit a event to stop zooming on synced charts however.
    buttons: buttons
  },
  stop: true
};

bolau avatar Aug 02 '23 09:08 bolau

Im also struggling with this. Only managed to get the synced tooltips working with chartjs 3.5.1.

samadarshad avatar Sep 27 '23 16:09 samadarshad

I have the same problem. sync is not working in Chart.js version 4. It is working up to version 3.5.1, but not after

Heunsig avatar Oct 22 '23 13:10 Heunsig

I also have this problem, can confirm that it works with chart.js 3.5.1 but not greater version. There is a fix available in branch 4.0.1 but unfortunately no plugin.js file to use it in web application. How can i get such file to import it in html via <script> src... </script> tag ?

Any help would be great , thanks in advanced!

Update: Ok, realized that i have to pull repository, switch to branch 4.0.1 --> npm install --> gulp build --> find files in dist folder :D

NiTRoeSE avatar Nov 24 '23 09:11 NiTRoeSE

Ok, realized that i have to pull repository, switch to branch 4.0.1 --> npm install --> gulp build --> find files in dist folder :D

Can you explain more? Please.

airplaneboy avatar Mar 24 '24 02:03 airplaneboy

Can you explain more? Please.

Hi, you have to compile version 4.0.1 by yourself, because there are no precompiled releases. To do that you have to:

  1. Clone the Repository
  2. Install NPM ( if not already done) on your machine
  3. Switch to cloned repository
  4. Switch the branch to 4.0.1 (git checkout 4.0.1)
  5. run npm install
  6. run gulp install
  7. You can find the compiled files in the dist folder.

NiTRoeSE avatar Mar 24 '24 07:03 NiTRoeSE

Thank you for getting back. I just did what you said, but it still doesn’t work for me. It’s telling something about “interpolate “ not being apart of some object type.

airplaneboy avatar Mar 24 '24 09:03 airplaneboy

Same issues with sync and zoom here.

mgmorcos avatar May 21 '24 20:05 mgmorcos