📄 panscrollzoomdemo.java
字号:
&& newMax <= hvp.getDataRange(xAxis).getUpperBound()) { xAxis.setLowerBound(newMin); xAxis.setUpperBound(newMax); } } } // vertical pan (1. Y-Axis) if (plot instanceof XYPlot) { final XYPlot vvp = (XYPlot) plot; final ValueAxis yAxis = vvp.getRangeAxis(); if (yAxis != null) { final double translatedStartPoint = yAxis.java2DToValue( (float) this.panStartPoint.getY(), scaledDataArea, vvp.getRangeAxisEdge() ); final double translatedEndPoint = yAxis.java2DToValue( (float) panEndPoint.getY(), scaledDataArea, vvp.getRangeAxisEdge() ); final double dY = translatedStartPoint - translatedEndPoint; final double oldMin = yAxis.getLowerBound(); final double newMin = oldMin + dY; final double oldMax = yAxis.getUpperBound(); final double newMax = oldMax + dY; // do not pan out of range if (newMin >= this.primYMinMax[0] && newMax <= this.primYMinMax[1]) { yAxis.setLowerBound(newMin); yAxis.setUpperBound(newMax); } } } // vertical pan (2. Y-Axis) if (plot instanceof XYPlot) { final XYPlot xyPlot = (XYPlot) plot; final ValueAxis yAxis = xyPlot.getRangeAxis(1); if (yAxis != null) { final double translatedStartPoint = yAxis.java2DToValue( (float) this.panStartPoint.getY(), scaledDataArea, xyPlot.getRangeAxisEdge(1) ); final double translatedEndPoint = yAxis.java2DToValue( (float) panEndPoint.getY(), scaledDataArea, xyPlot.getRangeAxisEdge(1) ); final double dY = translatedStartPoint - translatedEndPoint; final double oldMin = yAxis.getLowerBound(); final double newMin = oldMin + dY; final double oldMax = yAxis.getUpperBound(); final double newMax = oldMax + dY; if (newMin >= this.secondYMinMax[0] && newMax <= this.secondYMinMax[1]) { yAxis.setLowerBound(newMin); yAxis.setUpperBound(newMax); } } } // for the next time this.panStartPoint = panEndPoint; } } catch (Exception e) { e.printStackTrace(); } } /** * Handles a mouse clicked event, in this case by ignoring it. * * @param event the event. */ public void mouseClicked(final MouseEvent event) { // ignored } /** * Handles a mouse moved event, in this case by ignoring it. * * @param event the event. */ public void mouseMoved(final MouseEvent event) { // ignored } /** * Handles a mouse entered event, in this case by ignoring it. * * @param event the event. */ public void mouseEntered(final MouseEvent event) { // ignored } /** * Handles a mouse exited event, in this case by ignoring it. * * @param event the event. */ public void mouseExited(final MouseEvent event) { // ignored } /** * Starting point for the demo. * * @param args the command line arguments (ignored). */ public static void main(final String[] args) { try { final String lookAndFeelClassName = WindowsLookAndFeel.class.getName(); UIManager.setLookAndFeel(lookAndFeelClassName); } catch (Exception ex) { System.out.println(ex.getMessage()); } final PanScrollZoomDemo demo = new PanScrollZoomDemo("Pan & Scroll & Zoom - Demo"); demo.pack(); demo.setVisible(true); } // PRIVATE /** * Recalculates the scrollbar settings. * * @param plot the plot. */ private void recalcScrollBar(final Plot plot) { if (plot instanceof XYPlot) { final XYPlot hvp = (XYPlot) plot; final ValueAxis axis = hvp.getDomainAxis(); axis.setLowerMargin(0); axis.setUpperMargin(0); final Range rng = axis.getRange(); final BoundedRangeModel scrollBarModel = this.scrollBar.getModel(); final int len = scrollBarModel.getMaximum() - scrollBarModel.getMinimum(); if (rng.getLength() > 0) { this.scrollFactor = len / rng.getLength(); } final double dblow = rng.getLowerBound(); final int ilow = (int) (dblow * this.scrollFactor); scrollBarModel.setMinimum(ilow); final int val = ilow; scrollBarModel.setValue(val); final double dbup = rng.getUpperBound(); final int iup = (int) (dbup * this.scrollFactor); scrollBarModel.setMaximum(iup); final int ext = iup - ilow; scrollBarModel.setExtent(ext); scrollBarModel.addChangeListener(this); } } /** * Zooms in on an anchor point (measured in Java2D coordinates). * * @param x the x value. * @param y the y value. * @param zoomFactor the zoomFactor < 1 == zoom in; else out. */ private void zoomBoth(final double x, final double y, final double zoomFactor) { zoomHorizontal(x, zoomFactor); zoomVertical(y, zoomFactor); } /** * Decreases the range on the horizontal axis, centered about a Java2D x coordinate. * <P> * The range on the x axis is multiplied by zoomFactor * * @param x the x coordinate in Java2D space. * @param zoomFactor the zoomFactor < 1 == zoom in; else out. */ private void zoomHorizontal(final double x, final double zoomFactor) { final JFreeChart chart = this.chartPanel.getChart(); final ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo(); if (chart.getPlot() instanceof XYPlot) { final XYPlot hvp = (XYPlot) chart.getPlot(); final ValueAxis axis = hvp.getDomainAxis(); if (axis != null) { final double anchorValue = axis.java2DToValue( (float) x, info.getPlotInfo().getDataArea(), hvp.getDomainAxisEdge() ); if (zoomFactor < 1.0) { axis.resizeRange(zoomFactor, anchorValue); } else if (zoomFactor > 1.0) { final Range range = hvp.getDataRange(axis); adjustRange(axis, range, zoomFactor, anchorValue); } } } } /** * Decreases the range on the vertical axis, centered about a Java2D y coordinate. * <P> * The range on the y axis is multiplied by zoomFactor * * @param y the y coordinate in Java2D space. * @param zoomFactor the zoomFactor < 1 == zoom in; else out. */ private void zoomVertical(final double y, final double zoomFactor) { final JFreeChart chart = this.chartPanel.getChart(); final ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo(); // 1. (left) Y-Axis if (chart.getPlot() instanceof XYPlot) { final XYPlot vvp = (XYPlot) chart.getPlot(); final ValueAxis primYAxis = vvp.getRangeAxis(); if (primYAxis != null) { final double anchorValue = primYAxis.java2DToValue( (float) y, info.getPlotInfo().getDataArea(), vvp.getRangeAxisEdge() ); if (zoomFactor < 1.0) { // zoom in primYAxis.resizeRange(zoomFactor, anchorValue); } else if (zoomFactor > 1.0) { // zoom out final Range range = new Range(this.primYMinMax[0], this.primYMinMax[1]); adjustRange(primYAxis, range, zoomFactor, anchorValue); } } // 2. (right) Y-Axis if (chart.getPlot() instanceof XYPlot) { final XYPlot xyp = (XYPlot) chart.getPlot(); final ValueAxis secYAxis = xyp.getRangeAxis(1); if (secYAxis != null) { final double anchorValue = secYAxis.java2DToValue( (float) y, info.getPlotInfo().getDataArea(), xyp.getRangeAxisEdge(1)); if (zoomFactor < 1.0) { // zoom in secYAxis.resizeRange(zoomFactor, anchorValue); } else if (zoomFactor > 1.0) { // zoom out final Range range = new Range(this.secondYMinMax[0], this.secondYMinMax[1]); adjustRange(secYAxis, range, zoomFactor, anchorValue); } } } } } /** * used for zooming * * @param axis the axis. * @param range the range. * @param zoomFactor the zoom factor. * @param anchorValue the anchor value. */ private void adjustRange(final ValueAxis axis, final Range range, final double zoomFactor, final double anchorValue) { if (axis == null || range == null) { return; } final double rangeMinVal = range.getLowerBound() - range.getLength() * axis.getLowerMargin(); final double rangeMaxVal = range.getUpperBound() + range.getLength() * axis.getUpperMargin(); final double halfLength = axis.getRange().getLength() * zoomFactor / 2; double zoomedMinVal = anchorValue - halfLength; double zoomedMaxVal = anchorValue + halfLength; double adjMinVal = zoomedMinVal; if (zoomedMinVal < rangeMinVal) { adjMinVal = rangeMinVal; zoomedMaxVal += rangeMinVal - zoomedMinVal; } double adjMaxVal = zoomedMaxVal; if (zoomedMaxVal > rangeMaxVal) { adjMaxVal = rangeMaxVal; zoomedMinVal -= zoomedMaxVal - rangeMaxVal; adjMinVal = Math.max(zoomedMinVal, rangeMinVal); } final Range adjusted = new Range(adjMinVal, adjMaxVal); axis.setRange(adjusted); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -