gillius / jfxutils

JavaFX Utilities - Zoom and Pan Charts and Pane Scaling
Apache License 2.0
68 stars 22 forks source link

Utility method to reset zoom on double click #9

Closed gillius closed 8 years ago

gillius commented 8 years ago

Requested by @gforman44 with some suggested code:

/** Add double-click for auto-ranging.  If user double-clicks the x-axis, only that axis will be reset.  Likewise for y-axis
 */
public static void addDoubleClickAutoRange(final ScatterChart<?, ?> sc) {
    sc.addEventHandler(MouseEvent.MOUSE_CLICKED, ev -> {
        if (ev.getClickCount() == 2) {
            resetRange(sc, ev);
            ev.consume();
        }
    });
}
/** Reset AutoRanging for one or both axes, depending on where user clicked.  
 * x-axis only?  y-axis only?  chart --> both. 
 */
public static void resetRange(final ScatterChart<?, ?> sc, MouseEvent ev) {
    XYChartInfo i = new XYChartInfo(sc);
    if (!i.getXAxisArea().contains(ev.getX(), ev.getY())) sc.getYAxis().setAutoRanging(true);
    if (!i.getYAxisArea().contains(ev.getX(), ev.getY())) sc.getXAxis().setAutoRanging(true);
}

I will add similar functionality but use XYChart type and anonymous classes instead of Lambdas since jfxutils supports Java 7. Thanks for the code suggestions @gforman44!