public class ChartZoomManager extends Object
start()
and disabled via stop()
. The normal usage is to create a
StackPane with two children, an XYChart type and a Rectangle. The Rectangle should start out
invisible and have mouseTransparent set to true. If it has a stroke, it should be of INSIDE
type to be pixel perfect.
You can also use JFXChartUtil.setupZooming(XYChart)
for a default solution.
Six types of zooming are supported. All are enabled by default. The drag zooming can be disabled
with the setMouseFilter(javafx.event.EventHandler<? super javafx.scene.input.MouseEvent>)
set to a mouse filter that allows nothing. Mouse wheel zooming
can be disabled via the setMouseWheelZoomAllowed(boolean)
method.
A lot of code in ChartZoomManager currently assumes there are no scale or rotate transforms between the chartPane and the axes and plot area. However, all translation transforms, layoutX/Y changes, padding, margin, and setTranslate issues should be OK. This might be improved later, for example JavaFX 8 is rumored to allow transform multiplication, which could solve this.
Example FXML to create the components used by this class:
<StackPane fx:id="chartPane" alignment="CENTER"> <LineChart fx:id="chart" animated="false" legendVisible="false"> <xAxis> <NumberAxis animated="false" side="BOTTOM" /> </xAxis> <yAxis> <NumberAxis animated="false" side="LEFT" /> </yAxis> </LineChart> <Rectangle fx:id="selectRect" fill="DODGERBLUE" height="0.0" mouseTransparent="true" opacity="0.3" stroke="#002966" strokeType="INSIDE" strokeWidth="3.0" width="0.0" x="0.0" y="0.0" StackPane.alignment="TOP_LEFT" /> </StackPane>Example Java code in bound controller class:
ChartZoomManager zoomManager = new ChartZoomManager( chartPane, selectRect, chart ); zoomManager.start();
Type | Property and Description |
---|---|
javafx.beans.property.BooleanProperty |
mouseWheelZoomAllowed
If true, allow zooming via mouse wheel.
|
javafx.beans.property.BooleanProperty |
zoomAnimated
If true, animates the zoom.
|
javafx.beans.property.DoubleProperty |
zoomDurationMillis
Returns the number of milliseconds the zoom animation takes.
|
Modifier and Type | Field and Description |
---|---|
static javafx.event.EventHandler<javafx.scene.input.MouseEvent> |
DEFAULT_FILTER
The default mouse filter for the
ChartZoomManager filters events unless only primary
mouse button (usually left) is depressed. |
Constructor and Description |
---|
ChartZoomManager(javafx.scene.layout.Pane chartPane,
javafx.scene.shape.Rectangle selectRect,
javafx.scene.chart.XYChart<?,?> chart)
Construct a new ChartZoomManager.
|
Modifier and Type | Method and Description |
---|---|
AxisConstraintStrategy |
getAxisConstraintStrategy()
Returns the current strategy in use for mouse drag events.
|
javafx.event.EventHandler<? super javafx.scene.input.MouseEvent> |
getMouseFilter()
Returns the mouse filter.
|
AxisConstraintStrategy |
getMouseWheelAxisConstraintStrategy()
Returns the current strategy in use for mouse wheel events.
|
double |
getZoomDurationMillis()
Returns the number of milliseconds the zoom animation takes.
|
boolean |
isMouseWheelZoomAllowed()
If true, allow zooming via mouse wheel.
|
boolean |
isZoomAnimated()
If true, animates the zoom.
|
javafx.beans.property.BooleanProperty |
mouseWheelZoomAllowedProperty()
If true, allow zooming via mouse wheel.
|
void |
setAxisConstraintStrategy(AxisConstraintStrategy axisConstraintStrategy)
Sets the
AxisConstraintStrategy to use for mouse drag events, which determines which axis is allowed for
zooming. |
void |
setMouseFilter(javafx.event.EventHandler<? super javafx.scene.input.MouseEvent> mouseFilter)
Sets the mouse filter for starting the zoom action.
|
void |
setMouseWheelAxisConstraintStrategy(AxisConstraintStrategy mouseWheelAxisConstraintStrategy)
Sets the
AxisConstraintStrategy to use for mouse wheel events, which determines which axis is allowed for
zooming. |
void |
setMouseWheelZoomAllowed(boolean allowed)
If true, allow zooming via mouse wheel.
|
void |
setZoomAnimated(boolean zoomAnimated)
If true, animates the zoom.
|
void |
setZoomDurationMillis(double zoomDurationMillis)
Sets the number of milliseconds the zoom animation takes.
|
void |
start()
Start managing zoom management by adding event handlers and bindings as appropriate.
|
void |
stop()
Stop managing zoom management by removing all event handlers and bindings, and hiding the
rectangle.
|
javafx.beans.property.BooleanProperty |
zoomAnimatedProperty()
If true, animates the zoom.
|
javafx.beans.property.DoubleProperty |
zoomDurationMillisProperty()
Returns the number of milliseconds the zoom animation takes.
|
public javafx.beans.property.BooleanProperty zoomAnimatedProperty
isZoomAnimated()
,
setZoomAnimated(boolean)
public javafx.beans.property.DoubleProperty zoomDurationMillisProperty
public javafx.beans.property.BooleanProperty mouseWheelZoomAllowedProperty
public static final javafx.event.EventHandler<javafx.scene.input.MouseEvent> DEFAULT_FILTER
ChartZoomManager
filters events unless only primary
mouse button (usually left) is depressed.public ChartZoomManager(javafx.scene.layout.Pane chartPane, javafx.scene.shape.Rectangle selectRect, javafx.scene.chart.XYChart<?,?> chart)
ChartZoomManager
documentation for normal usage.chartPane
- A Pane which is the ancestor of all argumentsselectRect
- A Rectangle whose layoutX/Y makes it line up with the chartchart
- Chart to manage, where both X and Y axis are a ValueAxis
.public AxisConstraintStrategy getAxisConstraintStrategy()
public void setAxisConstraintStrategy(AxisConstraintStrategy axisConstraintStrategy)
AxisConstraintStrategy
to use for mouse drag events, which determines which axis is allowed for
zooming. The default implementation is AxisConstraintStrategies.getIgnoreOutsideChart()
.AxisConstraintStrategies
public AxisConstraintStrategy getMouseWheelAxisConstraintStrategy()
public void setMouseWheelAxisConstraintStrategy(AxisConstraintStrategy mouseWheelAxisConstraintStrategy)
AxisConstraintStrategy
to use for mouse wheel events, which determines which axis is allowed for
zooming. The default implementation is AxisConstraintStrategies.getDefault()
.AxisConstraintStrategies
public boolean isZoomAnimated()
public javafx.beans.property.BooleanProperty zoomAnimatedProperty()
isZoomAnimated()
,
setZoomAnimated(boolean)
public void setZoomAnimated(boolean zoomAnimated)
public double getZoomDurationMillis()
public javafx.beans.property.DoubleProperty zoomDurationMillisProperty()
public void setZoomDurationMillis(double zoomDurationMillis)
public boolean isMouseWheelZoomAllowed()
public javafx.beans.property.BooleanProperty mouseWheelZoomAllowedProperty()
public void setMouseWheelZoomAllowed(boolean allowed)
public javafx.event.EventHandler<? super javafx.scene.input.MouseEvent> getMouseFilter()
setMouseFilter(EventHandler)
public void setMouseFilter(javafx.event.EventHandler<? super javafx.scene.input.MouseEvent> mouseFilter)
Event.consume()
, then the event is ignored. If the filter is null, all events are
passed through. The default filter is DEFAULT_FILTER
.public void start()
public void stop()
Copyright © 2013–2016 Jason Winnebeck. All rights reserved.