📄 chartpanel.java
字号:
}
}
}
/**
* 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) / scaleX);
int y = (int) ((event.getY() - insets.top) / scaleY);
// old 'handle click' code...
chart.handleClick(x, y, this.info);
// new entity code...
if (this.chartMouseListeners.isEmpty()) {
return;
}
ChartEntity entity = this.info.getEntityCollection().getEntity(x, y);
ChartMouseEvent chartEvent = new ChartMouseEvent(getChart(), event, entity);
Iterator iterator = 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) / scaleX);
int y = (int) ((e.getY() - insets.top) / scaleY);
ChartEntity entity = this.info.getEntityCollection().getEntity(x, y);
ChartMouseEvent event = new ChartMouseEvent(getChart(), e, entity);
Iterator iterator = 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 (popup != null && popup.isShowing()) {
return;
}
Graphics2D g2 = (Graphics2D) getGraphics();
// use XOR to erase the previous zoom rectangle (if any)...
g2.setXORMode(java.awt.Color.gray);
if (zoomRectangle != null) {
if (fillZoomRectangle) {
g2.fill(zoomRectangle);
}
else {
g2.draw(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());
zoomRectangle = new Rectangle2D.Double(zoomPoint.getX(), zoomPoint.getY(),
xmax - zoomPoint.getX(),
ymax - zoomPoint.getY());
}
else if (this.horizontalZoom) {
double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());
zoomRectangle = new Rectangle2D.Double(zoomPoint.getX(), scaledDataArea.getMinY(),
xmax - zoomPoint.getX(),
scaledDataArea.getHeight());
}
else if (this.verticalZoom) {
double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());
zoomRectangle = new Rectangle2D.Double(scaledDataArea.getMinX(), zoomPoint.getY(),
scaledDataArea.getWidth(),
ymax - zoomPoint.getY());
}
if (zoomRectangle != null) {
// use XOR to draw the new zoom rectangle...
if (fillZoomRectangle) {
g2.fill(zoomRectangle);
}
else {
g2.draw(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();
}
}
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();
}
}
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 = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomHorizontalAxes(0.5);
}
}
/**
* Decreases the range on the vertical axis, centered about a Java2D
* y coordinate.
* <P>
* The range on the y axis is halved.
*
* @param y The y coordinate in Java2D space.
*/
public void zoomInVertical(double y) {
Plot p = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomVerticalAxes(0.5);
}
}
/**
* Zooms out on an anchor point (measured in Java2D coordinates).
*
* @param x The x value.
* @param y The y value.
*/
public void zoomOutBoth(double x, double y) {
zoomOutHorizontal(x);
zoomOutVertical(y);
}
/**
* Increases the range on the horizontal axis, centered about a Java2D
* x coordinate.
* <P>
* The range on the x axis is doubled.
*
* @param x The x coordinate in Java2D space.
*/
public void zoomOutHorizontal(double x) {
Plot p = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomHorizontalAxes(2.0);
}
}
/**
* Increases the range on the vertical axis, centered about a Java2D y coordinate.
* <P>
* The range on the y axis is doubled.
*
* @param y the y coordinate in Java2D space.
*/
public void zoomOutVertical(double y) {
Plot p = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomVerticalAxes(2.0);
}
}
/**
* Zooms in on a selected region.
*
* @param selection the selected region.
*/
public void zoom(Rectangle2D selection) {
double hLower = 0.0;
double hUpper = 0.0;
double vLower = 0.0;
double vUpper = 0.0;
if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) {
Rectangle2D scaledDataArea = getScaledDataArea();
hLower = (selection.getMinX() - scaledDataArea.getMinX()) / scaledDataArea.getWidth();
hUpper = (selection.getMaxX() - scaledDataArea.getMinX()) / scaledDataArea.getWidth();
vLower = (scaledDataArea.getMaxY() - selection.getMaxY()) / scaledDataArea.getHeight();
vUpper = (scaledDataArea.getMaxY() - selection.getMinY()) / scaledDataArea.getHeight();
Plot p = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomHorizontalAxes(hLower, hUpper);
plot.zoomVerticalAxes(vLower, vUpper);
}
}
}
/**
* Restores the auto-range calculation on both axes.
*/
public void autoRangeBoth() {
autoRangeHorizontal();
autoRangeVertical();
}
/**
* Restores the auto-range calculation on the horizontal axis.
*/
public void autoRangeHorizontal() {
Plot p = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomHorizontalAxes(0.0);
}
}
/**
* Restores the auto-range calculation on the vertical axis.
*/
public void autoRangeVertical() {
Plot p = chart.getPlot();
if (p instanceof ValueAxisPlot) {
ValueAxisPlot plot = (ValueAxisPlot) p;
plot.zoomVerticalAxes(0.0);
}
}
/**
* Returns the data area for the chart (the area inside the axes) with the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -