Search code examples
javascriptdygraphs

Two `dygraphs` with synchronized crosshair


I have implemented two dygraphs with custom option verticalCrosshair : true here:

https://rawgit.com/danielkrizian/dygraphs/master/tests/synchronize-Crosshair.html

enter image description here

When I hover over any of the graphs at a particular x point, I want all graphs to display the vertical crosshair at that point. So far I've been able to get this working on the top graph (gs[0]) like this:

highlightCallback: function(e, x, pts, row) {
  var sel = gs[0].getSelection();
  gs[1].setSelection(sel);
},

Nothing happens when I hover over the bottom graph. How to generalize it with for loop over all graphs?


Solution

  • You should put the dygraphs objects in an array and loop over it in your highlightCallback, updating the selection in all the dygraphs other than the one generating the event.

    One complication is that highlightCallback doesn't get the dygraph object as a parameter. This is an oversight in the API which I hope to fix in dygraphs 2.0. You can work around it by capturing the relevant Dygraph object in a closure when you set highlightCallback.

    See the synchronize demo for some inspiration.