📄 chartpanel.java
字号:
* 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.
*/
public void mousePressed(MouseEvent e) {
if (this.zoomRectangle == null) {
this.zoomPoint = RefineryUtilities.getPointInRectangle(
e.getX(), e.getY(), getScaledDataArea()
);
LOGGER.debug("In mousePressed()");
LOGGER.debug("getScaledDataArea() = " + getScaledDataArea());
LOGGER.debug("this.zoomPoint = " + this.zoomPoint);
// check for popup trigger...
if (e.isPopupTrigger()) {
if (this.popup != null) {
displayPopupMenu(e.getX(), e.getY());
}
}
}
}
/**
* Handles a 'mouse released' event.
* <P>
* On Windows, we need to check if this is a popup trigger, but only if we
* haven't already been tracking a zoom rectangle.
*
* @param e Information about the event.
*/
public void mouseReleased(MouseEvent e) {
LOGGER.debug("In mouseReleased()");
LOGGER.debug("this.zoomRectangle = " + this.zoomRectangle);
if (this.zoomRectangle != null) {
boolean zoomTrigger1 = this.horizontalZoom
&& Math.abs(e.getX() - this.zoomPoint.getX()) >= this.zoomTriggerDistance;
boolean zoomTrigger2 = this.verticalZoom
&& Math.abs(e.getY() - this.zoomPoint.getY()) >= this.zoomTriggerDistance;
if (zoomTrigger1 || zoomTrigger2) {
if ((this.horizontalZoom && (e.getX() < this.zoomPoint.getX()))
|| (this.verticalZoom && (e.getY() < this.zoomPoint.getY()))) {
autoRangeBoth();
}
else {
double x, y, w, h;
Rectangle2D scaledDataArea = getScaledDataArea();
//for a mouseReleased event, (horizontalZoom || verticalZoom)
//will be true, so we can just test for either being false;
//otherwise both are true
if (!this.verticalZoom) {
x = this.zoomPoint.getX();
y = scaledDataArea.getMinY();
w = Math.min(
this.zoomRectangle.getWidth(),
scaledDataArea.getMaxX() - this.zoomPoint.getX()
);
h = scaledDataArea.getHeight();
}
else if (!this.horizontalZoom) {
x = scaledDataArea.getMinX();
y = this.zoomPoint.getY();
w = scaledDataArea.getWidth();
h = Math.min(
this.zoomRectangle.getHeight(),
scaledDataArea.getMaxY() - this.zoomPoint.getY()
);
}
else {
x = this.zoomPoint.getX();
y = this.zoomPoint.getY();
w = Math.min(
this.zoomRectangle.getWidth(),
scaledDataArea.getMaxX() - this.zoomPoint.getX()
);
h = Math.min(
this.zoomRectangle.getHeight(),
scaledDataArea.getMaxY() - this.zoomPoint.getY()
);
}
Rectangle2D zoomArea = new Rectangle2D.Double(x, y, w, h);
Log.debug("zoomArea = " + zoomArea);
zoom(zoomArea);
}
this.zoomPoint = null;
this.zoomRectangle = null;
}
else {
Graphics2D g2 = (Graphics2D) getGraphics();
g2.setXORMode(java.awt.Color.gray);
if (this.fillZoomRectangle) {
g2.fill(this.zoomRectangle);
}
else {
g2.draw(this.zoomRectangle);
}
g2.dispose();
this.zoomRectangle = null;
}
}
else if (e.isPopupTrigger()) {
if (this.popup != null) {
displayPopupMenu(e.getX(), e.getY());
}
}
}
/**
* Receives notification of mouse clicks on the panel. These are
* translated and passed on to any registered chart mouse click listeners.
*
* @param event Information about the mouse event.
*/
public void mouseClicked(MouseEvent event) {
Insets insets = getInsets();
int x = (int) ((event.getX() - insets.left) / this.scaleX);
int y = (int) ((event.getY() - insets.top) / this.scaleY);
// old 'handle click' code...
//chart.handleClick(x, y, this.info);
this.anchor = new Point2D.Double(x, y);
this.chart.setTitle(this.chart.getTitle()); // force a redraw
// new entity code...
if (this.chartMouseListeners.isEmpty()) {
return;
}
ChartEntity entity = null;
if (this.info != null) {
EntityCollection entities = this.info.getEntityCollection();
if (entities != null) {
entity = entities.getEntity(x, y);
}
}
ChartMouseEvent chartEvent = new ChartMouseEvent(getChart(), event, entity);
Iterator iterator = this.chartMouseListeners.iterator();
while (iterator.hasNext()) {
ChartMouseListener listener = (ChartMouseListener) iterator.next();
listener.chartMouseClicked(chartEvent);
}
}
/**
* Implementation of the MouseMotionListener's method
*
* @param e the event.
*/
public void mouseMoved(MouseEvent e) {
if (this.horizontalAxisTrace) {
drawHorizontalAxisTrace(e.getX());
}
if (this.verticalAxisTrace) {
drawVerticalAxisTrace(e.getY());
}
if (this.chartMouseListeners.isEmpty()) {
return;
}
Insets insets = getInsets();
int x = (int) ((e.getX() - insets.left) / this.scaleX);
int y = (int) ((e.getY() - insets.top) / this.scaleY);
ChartEntity entity = null;
if (this.info != null) {
EntityCollection entities = this.info.getEntityCollection();
if (entities != null) {
entity = entities.getEntity(x, y);
}
}
ChartMouseEvent event = new ChartMouseEvent(getChart(), e, entity);
Iterator iterator = this.chartMouseListeners.iterator();
while (iterator.hasNext()) {
ChartMouseListener listener = (ChartMouseListener) iterator.next();
listener.chartMouseMoved(event);
}
}
/**
* Handles a 'mouse dragged' event.
*
* @param e the mouse event.
*/
public void mouseDragged(MouseEvent e) {
// if the popup menu has already been triggered, then ignore dragging...
if (this.popup != null && this.popup.isShowing()) {
return;
}
Graphics2D g2 = (Graphics2D) getGraphics();
// use XOR to erase the previous zoom rectangle (if any)...
g2.setXORMode(java.awt.Color.gray);
if (this.zoomRectangle != null) {
if (this.fillZoomRectangle) {
g2.fill(this.zoomRectangle);
}
else {
g2.draw(this.zoomRectangle);
}
}
Rectangle2D scaledDataArea = getScaledDataArea();
if (this.horizontalZoom && this.verticalZoom) {
// selected rectangle shouldn't extend outside the data area...
double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());
double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());
this.zoomRectangle = new Rectangle2D.Double(
this.zoomPoint.getX(), this.zoomPoint.getY(),
xmax - this.zoomPoint.getX(), ymax - this.zoomPoint.getY()
);
}
else if (this.horizontalZoom) {
double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());
this.zoomRectangle = new Rectangle2D.Double(
this.zoomPoint.getX(), scaledDataArea.getMinY(),
xmax - this.zoomPoint.getX(), scaledDataArea.getHeight()
);
}
else if (this.verticalZoom) {
double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());
this.zoomRectangle = new Rectangle2D.Double(
scaledDataArea.getMinX(), this.zoomPoint.getY(),
scaledDataArea.getWidth(), ymax - this.zoomPoint.getY()
);
}
if (this.zoomRectangle != null) {
// use XOR to draw the new zoom rectangle...
if (this.fillZoomRectangle) {
g2.fill(this.zoomRectangle);
}
else {
g2.draw(this.zoomRectangle);
}
}
g2.dispose();
}
/**
* Zooms in on an anchor point (measured in Java2D coordinates).
*
* @param x The x value.
* @param y The y value.
*/
public void zoomInBoth(double x, double y) {
zoomInHorizontal(x);
zoomInVertical(y);
}
/**
* Returns a reference to the 'horizontal' value axis, if there is one.
*
* @param plot the plot.
*
* @return The axis.
*/
private ValueAxis getHorizontalValueAxis(Plot plot) {
if (plot == null) {
return null;
}
ValueAxis axis = null;
if (plot instanceof CategoryPlot) {
CategoryPlot cp = (CategoryPlot) plot;
if (cp.getOrientation() == PlotOrientation.HORIZONTAL) {
axis = cp.getRangeAxis();
}
}
if (plot instanceof XYPlot) {
XYPlot xyp = (XYPlot) plot;
if (xyp.getOrientation() == PlotOrientation.HORIZONTAL) {
axis = xyp.getRangeAxis();
}
else if (xyp.getOrientation() == PlotOrientation.VERTICAL) {
axis = xyp.getDomainAxis();
}
}
if (plot instanceof FastScatterPlot) {
FastScatterPlot fsp = (FastScatterPlot) plot;
axis = fsp.getDomainAxis();
}
return axis;
}
/**
* Returns a reference to the 'vertical' value axis, if there is one.
*
* @param plot the plot.
*
* @return The axis.
*/
private ValueAxis getVerticalValueAxis(Plot plot) {
if (plot == null) {
return null;
}
ValueAxis axis = null;
if (plot instanceof CategoryPlot) {
CategoryPlot cp = (CategoryPlot) plot;
if (cp.getOrientation() == PlotOrientation.VERTICAL) {
axis = cp.getRangeAxis();
}
}
if (plot instanceof XYPlot) {
XYPlot xyp = (XYPlot) plot;
if (xyp.getOrientation() == PlotOrientation.HORIZONTAL) {
axis = xyp.getDomainAxis();
}
else if (xyp.getOrientation() == PlotOrientation.VERTICAL) {
axis = xyp.getRangeAxis();
}
}
if (plot instanceof FastScatterPlot) {
FastScatterPlot fsp = (FastScatterPlot) plot;
axis = fsp.getRangeAxis();
}
return axis;
}
/**
* Decreases the range on the horizontal axis, centered about a Java2D
* x coordinate.
* <P>
* The range on the x axis is halved.
*
* @param x The x coordinate in Java2D space.
*/
public void zoomInHorizontal(double x) {
Plot p = this.chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomHorizontalAxes(this.zoomInFactor);
}
}
/**
* Decreases the range on the vertical axis, centered about a Java2D
* y coordinate.
* <P>
* The range on the y axis is halved.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -