package org.jfree.panning;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PlotRenderingInfo;
/**
* A Swing GUI component for displaying a {@link JFreeChart} object.
* <P>
* The panel registers with the chart to receive notification of changes to any component of the
* chart. The chart is redrawn automatically whenever this notification is received.
*/
public class PanningChartPanel
extends ChartPanel
implements MouseWheelListener
{
public static final int MOUSE_ZOOM = 1;
public static final int MOUSE_PAN = 2;
private int mouseMode = MOUSE_ZOOM;
private int lastPanX = -1;
private int lastPanY = -1;
private Point2D panStartPoint = null;
double zoomFactor = 0.9d;
/**
* Constructs a panel that displays the specified chart.
*
* @param chart the chart.
*/
public PanningChartPanel(JFreeChart chart)
{
super(chart);
addMouseWheelListener(this);
}
/**
* Constructs a panel containing a chart.
*
* @param chart the chart.
* @param useBuffer a flag controlling whether or not an off-screen buffer is used.
*/
public PanningChartPanel(JFreeChart chart, boolean useBuffer)
{
super(chart, useBuffer);
addMouseWheelListener(this);
}
/**
* Constructs a JFreeChart panel.
*
* @param chart the chart.
* @param properties a flag indicating whether or not the chart property editor should be
* available via the popup menu.
* @param save a flag indicating whether or not save options should be available via the popup
* menu.
* @param print a flag indicating whether or not the print option should be available via the
* popup menu.
* @param zoom a flag indicating whether or not zoom options should be added to the popup menu.
* @param tooltips a flag indicating whether or not tooltips should be enabled for the chart.
*/
public PanningChartPanel(JFreeChart chart, boolean properties, boolean save, boolean print,
boolean zoom, boolean tooltips)
{
super(chart, properties, save, print, zoom, tooltips);
addMouseWheelListener(this);
}
/**
* Constructs a JFreeChart panel.
*
* @param chart the chart.
* @param width the preferred width of the panel.
* @param height the preferred height of the panel.
* @param minimumDrawWidth the minimum drawing width.
* @param minimumDrawHeight the minimum drawing height.
* @param maximumDrawWidth the maximum drawing width.
* @param maximumDrawHeight the maximum drawing height.
* @param useBuffer a flag that indicates whether to use the off-screen buffer to improve
* performance (at the expense of memory).
* @param properties a flag indicating whether or not the chart property editor should be
* available via the popup menu.
* @param save a flag indicating whether or not save options should be available via the popup
* menu.
* @param print a flag indicating whether or not the print option should be available via the
* popup menu.
* @param zoom a flag indicating whether or not zoom options should be added to the popup menu.
* @param tooltips a flag indicating whether or not tooltips should be enabled for the chart.
*/
public PanningChartPanel(JFreeChart chart, int width, int height, int minimumDrawWidth,
int minimumDrawHeight, int maximumDrawWidth, int maximumDrawHeight, boolean useBuffer,
boolean properties, boolean save, boolean print, boolean zoom, boolean tooltips)
{
super(chart, width, height, minimumDrawWidth, minimumDrawHeight, maximumDrawWidth,
maximumDrawHeight, useBuffer, properties, save, print, zoom, tooltips);
addMouseWheelListener(this);
}
/**
* Returns a point based on (x, y) but constrained to be within the bounds of the given
* rectangle. This method could be moved to JCommon.
*
* @param x the x-coordinate.
* @param y the y-coordinate.
* @param area the rectangle (<code>null</code> not permitted).
* @return A point within the rectangle.
*/
private Point getPointInRectangle(int x, int y, Rectangle2D area)
{
x = (int)Math.max(Math.ceil(area.getMinX()), Math.min(x, Math.floor(area.getMaxX())));
y = (int)Math.max(Math.ceil(area.getMinY()), Math.min(y, Math.floor(area.getMaxY())));
return new Point(x, y);
}
/**
* Handles a 'mouse pressed' event.
* <P>
* This event is the popup trigger on Unix/Linux. For Windows, the popup trigger is the 'mouse
* released' event.
*
* @param e The mouse event.
*/
@Override
public void mousePressed(MouseEvent e)
{
if (e.isControlDown())
{
this.mouseMode = MOUSE_PAN;
Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());
if (screenDataArea != null)
{
Point point = getPointInRectangle(e.getX(), e.getY(), screenDataArea);
panStartPoint = point;
lastPanX = point.x;
lastPanY = point.y;
}
}
else
{
this.mouseMode = MOUSE_ZOOM;
}
super.mousePressed(e);
}
/**
* Handles a 'mouse dragged' event.
*
* @param e the mouse event.
*/
@Override
public void mouseDragged(MouseEvent e)
{
if (this.mouseMode == MOUSE_PAN)
{
// panStartPoint might be null if the mouse was pressed between
// two plots.
if (panStartPoint == null)
{
// try to get a new start point if the user drags inside a plot
// use the first point in the plot as start point
Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());
if (screenDataArea != null)
{
Point point = getPointInRectangle(e.getX(), e.getY(), screenDataArea);
panStartPoint = point;
}
}
else
{
move(panStartPoint, lastPanX, lastPanY, e.getX(), e.getY());
}
lastPanX = e.getX();
lastPanY = e.getY();
return;
}
super.mouseDragged(e);
}
private void move(Point2D startPoint, int oldX, int oldY, int newX, int newY)
{
/*
* this is selection of move direction. e.g. dragged to right -> what does this mean ? move
* selection to right or left ? value of -1 is the typical graphic program behaviour
*/
double moveDirection = -1;
double diffX = moveDirection * (newX - oldX); // *
double diffY = -1 * moveDirection * (newY - oldY); // *
// check for change
if (diffX == 0 && diffY == 0)
return;
try
{
getChart().setNotify(false);
if (getChart().getPlot() instanceof Pannable)
{
Pannable panningPlot = (Pannable)getChart().getPlot();
if (panningPlot.isDomainPannable())
{
if (panningPlot.getOrientation() == PlotOrientation.VERTICAL)
{
JFreeChart放大缩小移动
需积分: 38 32 浏览量
2018-10-27
14:29:54
上传
评论
收藏 6KB RAR 举报
guanggg4
- 粉丝: 0
- 资源: 2
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第39题组合总和.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第38题外观数列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第37题解数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第36题有效的数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第35题搜索插入位置.zip
- index.wxml
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈