001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * -------------------- 028 * ChartViewerSkin.java 029 * -------------------- 030 * (C) Copyright 2014, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes: 036 * -------- 037 * 20-Jun-2014 : Version 1 (DG); 038 */ 039 040 041package org.jfree.chart.fx; 042 043import org.jfree.chart.fx.interaction.ZoomHandlerFX; 044import javafx.scene.control.SkinBase; 045import javafx.scene.layout.BorderPane; 046import javafx.scene.layout.StackPane; 047import javafx.scene.paint.Color; 048import javafx.scene.paint.Paint; 049import javafx.scene.shape.Rectangle; 050import org.jfree.chart.ChartMouseEvent; 051import org.jfree.chart.ChartRenderingInfo; 052import org.jfree.chart.JFreeChart; 053import org.jfree.chart.fx.interaction.ChartMouseListenerFX; 054import org.jfree.chart.util.ParamChecks; 055 056/** 057 * A default skin for the {@link ChartViewer} control. 058 * 059 * <p>THE API FOR THIS CLASS IS SUBJECT TO CHANGE IN FUTURE RELEASES. This is 060 * so that we can incorporate feedback on the (new) JavaFX support in 061 * JFreeChart.</p> 062 * 063 * @since 1.0.18 064 */ 065public class ChartViewerSkin extends SkinBase<ChartViewer> { 066 067 /** The chart canvas. */ 068 private ChartCanvas canvas; 069 070 /** 071 * The zoom rectangle is used to display the zooming region when 072 * doing a drag-zoom with the mouse. Most of the time this rectangle 073 * is not visible. 074 */ 075 private Rectangle zoomRectangle; 076 077 /** 078 * Creates a new instance. 079 * 080 * @param control the control ({@code null} not permitted). 081 */ 082 public ChartViewerSkin(ChartViewer control) { 083 super(control); 084 getChildren().add(createNode(control)); 085 this.zoomRectangle = new Rectangle(0, 0, new Color(0, 0, 1, 0.25)); 086 this.zoomRectangle.setManaged(false); 087 this.zoomRectangle.setVisible(false); 088 getChildren().add(this.zoomRectangle); 089 } 090 091 /** 092 * Returns the rendering info from the most recent drawing of the chart. 093 * 094 * @return The rendering info (possibly {@code null}). 095 * 096 * @since 1.0.19 097 */ 098 public ChartRenderingInfo getRenderingInfo() { 099 return this.canvas.getRenderingInfo(); 100 } 101 102 /** 103 * Sets the chart displayed by this control. 104 * 105 * @param chart the chart ({@code null} not permitted). 106 */ 107 public void setChart(JFreeChart chart) { 108 this.canvas.setChart(chart); 109 } 110 111 public void setTooltipEnabled(boolean enabled) { 112 this.canvas.setTooltipEnabled(enabled); 113 } 114 115 /** 116 * Returns the current fill paint for the zoom rectangle. 117 * 118 * @return The fill paint. 119 */ 120 public Paint getZoomFillPaint() { 121 return this.zoomRectangle.getFill(); 122 } 123 124 /** 125 * Sets the fill paint for the zoom rectangle. 126 * 127 * @param paint the new paint. 128 */ 129 public void setZoomFillPaint(Paint paint) { 130 this.zoomRectangle.setFill(paint); 131 } 132 133 /** 134 * Registers a listener to receive {@link ChartMouseEvent} notifications 135 * from the chart viewer. 136 * 137 * @param listener the listener ({@code null} not permitted). 138 */ 139 public void addChartMouseListener(ChartMouseListenerFX listener) { 140 ParamChecks.nullNotPermitted(listener, "listener"); 141 this.canvas.addChartMouseListener(listener); 142 } 143 144 /** 145 * Removes a listener from the list of objects listening for chart mouse 146 * events. 147 * 148 * @param listener the listener. 149 */ 150 public void removeChartMouseListener(ChartMouseListenerFX listener) { 151 this.canvas.removeChartMouseListener(listener); 152 } 153 154 /** 155 * Sets the visibility of the zoom rectangle. 156 * 157 * @param visible the new flag value. 158 */ 159 public void setZoomRectangleVisible(boolean visible) { 160 this.zoomRectangle.setVisible(visible); 161 } 162 163 /** 164 * Sets the location and size of the zoom rectangle and makes it visible 165 * if it is not already visible. 166 * 167 * @param x the x-coordinate. 168 * @param y the y-coordinate. 169 * @param w the width. 170 * @param h the height. 171 */ 172 public void showZoomRectangle(double x, double y, double w, double h) { 173 this.zoomRectangle.setX(x); 174 this.zoomRectangle.setY(y); 175 this.zoomRectangle.setWidth(w); 176 this.zoomRectangle.setHeight(h); 177 this.zoomRectangle.setVisible(true); 178 } 179 180 /** 181 * Creates the node representing this control. 182 * 183 * @return The node. 184 */ 185 private BorderPane createNode(ChartViewer control) { 186 BorderPane borderPane = new BorderPane(); 187 borderPane.setPrefSize(800, 500); 188 StackPane sp = new StackPane(); 189 sp.setMinSize(10, 10); 190 sp.setPrefSize(600, 400); 191 this.canvas = new ChartCanvas(getSkinnable().getChart()); 192 this.canvas.setTooltipEnabled(control.isTooltipEnabled()); 193 this.canvas.addChartMouseListener(control); 194 this.canvas.widthProperty().bind(sp.widthProperty()); 195 this.canvas.heightProperty().bind(sp.heightProperty()); 196 197 this.canvas.addMouseHandler(new ZoomHandlerFX("zoom", control)); 198 sp.getChildren().add(this.canvas); 199 200 borderPane.setCenter(sp); 201 return borderPane; 202 } 203 204}