Skip to content

Conversation

@jledentu
Copy link
Collaborator

Closes #176

This PR allows switching between wheel/drag modes of the zoom (if options.zoom.drag is changed, it will be taken into account in next events).

  • All event listeners are set up in the beforeInit method, and the options.zoom.drag setting is checked in the body of these listeners rather than at the initialization.
  • I added a Enable/disable drag mode in the zoom-time sample to test this improvement.
  • I also fixed the Reset Zoom button in the zoom-time sample.

src/plugin.js Outdated
node.addEventListener('mousemove', chartInstance.zoom._mouseMoveHandler);

chartInstance.zoom._mouseUpHandler = function(event) {
if (chartInstance.options.zoom.drag && chartInstance.zoom._dragZoomStart) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably prefer to do the inverse to save from having to indent the rest of the code as much:

if (!chartInstance.options.zoom.drag || !chartInstance.zoom._dragZoomStart) {
  return;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But probably only for a longer method like this one. For a method that's only a few lines, what you have now is shorter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the mouseup handler as suggested, I also prefer that.

src/plugin.js Outdated
chartInstance.update(0);
}
};
node.addEventListener('mousemove', chartInstance.zoom._mouseMoveHandler);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot we have a mousemove handler. mousemove can fire hundreds of times a second constantly, so we should be a lot more careful than with mouseup, mousedown, etc. I'd be more comfortable for performance reasons if went the route of switching the listeners on and off instead eventhough it would be a bit more complicated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I fixed and now add the mousemove listener on mousedown. Other listeners stay initialized as it is.

@benmccann benmccann merged commit 1bd916c into chartjs:master Feb 4, 2019
@benmccann benmccann mentioned this pull request Jul 3, 2019
exwm pushed a commit to exwm/chartjs-plugin-zoom that referenced this pull request Apr 30, 2021
Enable switching between wheel/drag modes of the zoom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

toggle options.zoom.drag true <=> false

2 participants