📄 jfreechart.java
字号:
} /** * Returns the alpha-transparency for the chart's background image. * * @return The alpha-transparency. */ public float getBackgroundImageAlpha() { return this.backgroundImageAlpha; } /** * Sets the alpha-transparency for the chart's background image. * Registered listeners are notified that the chart has been changed. * * @param alpha the alpha value. */ public void setBackgroundImageAlpha(float alpha) { if (this.backgroundImageAlpha != alpha) { this.backgroundImageAlpha = alpha; fireChartChanged(); } } /** * Returns a flag that controls whether or not change events are sent to * registered listeners. * * @return A boolean. */ public boolean isNotify() { return this.notify; } /** * Sets a flag that controls whether or not listeners receive * {@link ChartChangeEvent} notifications. * * @param notify a boolean. */ public void setNotify(boolean notify) { this.notify = notify; // if the flag is being set to true, there may be queued up changes... if (notify) { notifyListeners(new ChartChangeEvent(this)); } } /** * Draws the chart on a Java 2D graphics device (such as the screen or a * printer). * <P> * This method is the focus of the entire JFreeChart library. * * @param g2 the graphics device. * @param area the area within which the chart should be drawn. */ public void draw(Graphics2D g2, Rectangle2D area) { draw(g2, area, null, null); } /** * Draws the chart on a Java 2D graphics device (such as the screen or a * printer). This method is the focus of the entire JFreeChart library. * * @param g2 the graphics device. * @param area the area within which the chart should be drawn. * @param info records info about the drawing (null means collect no info). */ public void draw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) { draw(g2, area, null, info); } /** * Draws the chart on a Java 2D graphics device (such as the screen or a * printer). * <P> * This method is the focus of the entire JFreeChart library. * * @param g2 the graphics device. * @param chartArea the area within which the chart should be drawn. * @param anchor the anchor point (in Java2D space) for the chart * (<code>null</code> permitted). * @param info records info about the drawing (null means collect no info). */ public void draw(Graphics2D g2, Rectangle2D chartArea, Point2D anchor, ChartRenderingInfo info) { notifyListeners( new ChartProgressEvent( this, this, ChartProgressEvent.DRAWING_STARTED, 0 ) ); // record the chart area, if info is requested... if (info != null) { info.clear(); info.setChartArea(chartArea); } // ensure no drawing occurs outside chart area... Shape savedClip = g2.getClip(); g2.clip(chartArea); g2.addRenderingHints(this.renderingHints); // draw the chart background... if (this.backgroundPaint != null) { g2.setPaint(this.backgroundPaint); g2.fill(chartArea); } if (this.backgroundImage != null) { Composite originalComposite = g2.getComposite(); g2.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, this.backgroundImageAlpha ) ); Rectangle2D dest = new Rectangle2D.Double( 0.0, 0.0, this.backgroundImage.getWidth(null), this.backgroundImage.getHeight(null) ); Align.align(dest, chartArea, this.backgroundImageAlignment); g2.drawImage( this.backgroundImage, (int) dest.getX(), (int) dest.getY(), (int) dest.getWidth(), (int) dest.getHeight(), null ); g2.setComposite(originalComposite); } if (isBorderVisible()) { Paint paint = getBorderPaint(); Stroke stroke = getBorderStroke(); if (paint != null && stroke != null) { Rectangle2D borderArea = new Rectangle2D.Double( chartArea.getX(), chartArea.getY(), chartArea.getWidth() - 1.0, chartArea.getHeight() - 1.0 ); g2.setPaint(paint); g2.setStroke(stroke); g2.draw(borderArea); } } // draw the title and subtitles... Rectangle2D nonTitleArea = new Rectangle2D.Double(); nonTitleArea.setRect(chartArea); EntityCollection entities = null; if (info != null) { entities = info.getEntityCollection(); } if (this.title != null) { EntityCollection e = drawTitle( this.title, g2, nonTitleArea, (entities != null) ); if (e != null) { entities.addAll(e); } } Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title currentTitle = (Title) iterator.next(); EntityCollection e = drawTitle( currentTitle, g2, nonTitleArea, (entities != null) ); if (e != null) { entities.addAll(e); } } // draw the legend - the draw method will return the remaining area // after the legend steals a chunk of the non-title area for itself Rectangle2D plotArea = nonTitleArea; if (this.oldLegend != null) { plotArea.setRect(this.oldLegend.draw(g2, nonTitleArea, info)); } // draw the plot (axes and data visualisation) PlotRenderingInfo plotInfo = null; if (info != null) { plotInfo = info.getPlotInfo(); } this.plot.draw(g2, plotArea, anchor, null, plotInfo); g2.setClip(savedClip); notifyListeners( new ChartProgressEvent( this, this, ChartProgressEvent.DRAWING_FINISHED, 100 ) ); } /** * Creates a rectangle that is aligned to the frame. * * @param dimensions * @param frame * @param hAlign * @param vAlign * * @return A rectangle. */ private Rectangle2D createAlignedRectangle2D(Size2D dimensions, Rectangle2D frame, HorizontalAlignment hAlign, VerticalAlignment vAlign) { double x = Double.NaN; double y = Double.NaN; if (hAlign == HorizontalAlignment.LEFT) { x = frame.getX(); } else if (hAlign == HorizontalAlignment.CENTER) { x = frame.getCenterX() - (dimensions.width / 2.0); } else if (hAlign == HorizontalAlignment.RIGHT) { x = frame.getMaxX() - dimensions.width; } if (vAlign == VerticalAlignment.TOP) { y = frame.getY(); } else if (vAlign == VerticalAlignment.CENTER) { y = frame.getCenterY() - (dimensions.height / 2.0); } else if (vAlign == VerticalAlignment.BOTTOM) { y = frame.getMaxY() - dimensions.height; } return new Rectangle2D.Double( x, y, dimensions.width, dimensions.height ); } /** * Draws a title. The title should be drawn at the top, bottom, left or * right of the specified area, and the area should be updated to reflect * the amount of space used by the title. * * @param t the title (<code>null</code> not permitted). * @param g2 the graphics device (<code>null</code> not permitted). * @param area the chart area, excluding any existing titles * (<code>null</code> not permitted). * @param entities a flag that controls whether or not an entity * collection is returned for the title. * * @return An entity collection for the title (possibly <code>null</code>). */ protected EntityCollection drawTitle(Title t, Graphics2D g2, Rectangle2D area, boolean entities) { if (t == null) { throw new IllegalArgumentException("Null 't' argument."); } if (area == null) { throw new IllegalArgumentException("Null 'area' argument."); } Rectangle2D titleArea = new Rectangle2D.Double(); RectangleEdge position = t.getPosition(); double ww = area.getWidth(); double hh = area.getHeight(); RectangleConstraint constraint = new RectangleConstraint( ww, new Range(0.0, ww), LengthConstraintType.RANGE, hh, new Range(0.0, hh), LengthConstraintType.RANGE ); Object retValue = null; BlockParams p = new BlockParams(); p.setGenerateEntities(entities); if (position == RectangleEdge.TOP) { Size2D size = t.arrange(g2, constraint); titleArea = createAlignedRectangle2D( size, area, t.getHorizontalAlignment(), VerticalAlignment.TOP ); retValue = t.draw(g2, titleArea, p); area.setRect( area.getX(), Math.min(area.getY() + size.height, area.getMaxY()), area.getWidth(), Math.max(area.getHeight() - size.height, 0) ); } else if (position == RectangleEdge.BOTTOM) { Size2D size = t.arrange(g2, constraint); titleArea = createAlignedRectangle2D( size, area, t.getHorizontalAlignment(), VerticalAlignment.BOTTOM ); retValue = t.draw(g2, titleArea, p); area.setRect( area.getX(), area.getY(), area.getWidth(), area.getHeight() - size.height ); } else if (position == RectangleEdge.RIGHT) { Size2D size = t.arrange(g2, constraint); titleArea = createAlignedRectangle2D( size, area, HorizontalAlignment.RIGHT, t.getVerticalAlignment() ); retValue = t.draw(g2, titleArea, p); area.setRect( area.getX(), area.getY(), area.getWidth() - size.width, area.getHeight() ); } else if (position == RectangleEdge.LEFT) { Size2D size = t.arrange(g2, constraint); titleArea = createAlignedRectangle2D( size, area, HorizontalAlignment.LEFT, t.getVerticalAlignment() ); retValue = t.draw(g2, titleArea, p); area.setRect( area.getX() + size.width, area.getY(), area.getWidth() - size.width, area.getHeight() ); } else { throw new RuntimeException("Unrecognised title position."); } EntityCollection result = null; if (retValue instanceof EntityBlockResult) { EntityBlockResult ebr = (EntityBlockResult) retValue; result = ebr.getEntityCollection(); } return result; } /** * Creates and returns a buffered image into which the chart has been drawn. * * @param width the width. * @param height the height. * * @return A buffered image. */ public BufferedImage createBufferedImage(int width, int height) { return createBufferedImage(width, height, null); } /** * Creates and returns a buffered image into which the chart has been drawn. * * @param width the width. * @param height the height. * @param info carries back chart state information (<code>null</code> * permitted). * * @return A buffered image. */ public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info) { return createBufferedImage( width, height, BufferedImage.TYPE_INT_RGB, info ); } /** * Creates and returns a buffered image into which the chart has been drawn. * * @param width the width. * @param height the height. * @param imageType the image type. * @param info carries back chart state information (<code>null</code> * permitted). * * @return A buffered image. */ public BufferedImage createBufferedImage(int width, int height, int imageType, ChartRenderingInfo info) { BufferedImage image = new BufferedImage(width, height, imageType); Graphics2D g2 = image.createGraphics(); draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info); g2.dispose(); return image; } /** * Creates and returns a buffered image into which the chart has been drawn. * * @param imageWidth the image width. * @param imageHeight the image height. * @param drawWidth the width for drawing the chart (will be scaled to * fit image). * @param drawHeight the height for drawing the chart (will be scaled to * fit image). * @param info optional object for collection chart dimension and entity * information. * * @return A buffered image. */ public BufferedImage createBufferedImage(int imageWidth, int imageHeight, double drawWidth, double drawHeight, ChartRenderingInfo info) { BufferedImage image = new BufferedImage( imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB ); Graphics2D g2 = image.createGraphics(); double scaleX = imageWidth / drawWidth; double scaleY = imageHeight / drawHeight; AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY); g2.transform(st); draw( g2, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), null, info ); g2.dispose(); return image; } /** * Handles a 'click' on the chart. * <P> * JFreeChart is not a UI component, so some other object (e.g. ChartPanel) * needs to capture the click event and pass it onto the JFreeChart object. * If you are not using JFreeChart in a client application, then this * method is not required (and hopefully it doesn't get in the way). * * @param x x-coordinate of the click (in Java2D space). * @param y y-coordinate of the click (in Java2D space). * @param info contains chart dimension and entity information. */ public void handleClick(int x, int y, ChartRenderingInfo info) { // pass the click on to the plot... // rely on the plot to post a plot change event and redraw the chart... this.plot.handleClick(x, y, info.getPlotInfo());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -