Search code examples
javafxjfreechart-fx

How to disable zoom in a ChartViewer


I'm working with . I created a scroll panel and I have a ChartViewer (not a ChartPanel) inside of it. Inside the chart, I plot data using a CombinedDomainXYPlot. I add some data to the combined plot at runtime:

XYPlot subplot;

for (int i=0; i<10; i++){
    dataset = .....
    subplot = new XYPlot(dataset, null, rangeAxis1, renderer);
    myCombinedPlot.add(subplot)
}

String panel_title = "bla bla";
myChartViewer.setChart(new JFreeChart(panel_title, JFreeChart.DEFAULT_TITLE_FONT, myCombinedPlot, false));

Now, when I drag and drop on my graph, I see that the default operation of mouse is the zoom on the signal. How do I disable this default function? I tried the following, but it doesn't work:

myChartViewer.getCanvas().setRangeZoomable(false);
myChartViewer.getCanvas().setDomainZoomable(false);

Solution

  • This behavior is controlled by an instance of ZoomHandlerFX installed by the ChartViewer constructor. To eliminate zooming entirely, simply remove the handler.

    ChartViewer chartViewer = new ChartViewer(chart);
    ChartCanvas canvas = chartViewer.getCanvas();
    canvas.removeMouseHandler(canvas.getMouseHandler("zoom"));
    

    If you later need to restore the handler, invoke addMouseHandler() like this:

    canvas.addMouseHandler(new ZoomHandlerFX("zoom", chartViewer));