Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting up Zoom with react-chartjs-2 #64

Closed
miguelperes opened this issue Feb 22, 2017 · 9 comments
Closed

Setting up Zoom with react-chartjs-2 #64

miguelperes opened this issue Feb 22, 2017 · 9 comments

Comments

@miguelperes
Copy link

What is the proper way to setup zoom plugin with react-chartjs-2? Is it compatible?

I just ran npm install chartjs-plugin-zoom which installed the plugin and its hammerjs dependency and started configuring my chart options:

pan: {
  enabled: true,
  mode: 'x'
},
zoom: {
  enabled: true,
  mode: 'xy'
},

However this is not working. Am I skipping any step? I searched for react-chartjs-2 specific issues but found nothing. Thanks!

@miguelperes
Copy link
Author

Solved: I don't know if this is the proper way to do this, but I just imported the plugin in my chart component file:

import * as zoom from 'chartjs-plugin-zoom'

and the pan and zoom options started working. If there is no better approach, I think the issue can be closed! :)

@CaioMM
Copy link

CaioMM commented Mar 31, 2017

Hi, i'm a newbie with js and i'm trying to use the zoom and pan functions in one graph, but only zoom works, can you show me your code or tell me how do i import it like you did ?

ps: i don't speak very well English

@junbinku
Copy link

Does anyone know do I implement an onClick button to reset the zoom back to default?

@absrivastava
Copy link

@kashifsulaiman
Copy link

@miguelperes can you tell me how did you configured hammerjs?

@junbinku
Copy link

@kashifsulaiman import Hammer from 'hammerjs';

@jcrombez
Copy link

jcrombez commented Jan 3, 2020

it has been a few years but if someone else want to do a reset button when using react (like @junbinku was asking) :

in your render you need to get the component reference, for example with a <Line> :

<button onClick={() => this.lineReference.chartInstance.handleZoomReset() }>Reset zoom</button>
<Line (here your data, options and so on)  ref={(reference) => this.lineReference = reference} />

@dopry
Copy link

dopry commented Mar 2, 2021

or if you're using hooks

function Chart() {
    const chartRef = useRef(null)
    const resetZoom = () => {
       chartRef.current.chartInstance.resetZoom()
    }
    return (
       <Button onClick={resetZoom} />
       <Line ref={chartRef} data={{ ... }} options={{ ... }} />
    )
}

@Tom687
Copy link

Tom687 commented Jun 25, 2021

@dopry I had to use chartRef.current.resetZoom() for it to work. Do you know why I don't have chartInstance in chartRef.current ? Is it a new version thing ? Thanks

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

No branches or pull requests

9 participants