📄 jfreechart.java
字号:
public AbstractTitle getSubtitle(int index) {
// check arguments...
if ((index < 0) || (index == getSubtitleCount())) {
throw new IllegalArgumentException("JFreeChart.getSubtitle(...): index out of range.");
}
return (AbstractTitle) this.subtitles.get(index);
}
/**
* Adds a chart subtitle, and notifies registered listeners that the chart has been modified.
*
* @param subtitle the subtitle.
*/
public void addSubtitle(AbstractTitle subtitle) {
if (subtitle != null) {
this.subtitles.add(subtitle);
subtitle.addChangeListener(this);
fireChartChanged();
}
}
/**
* Returns the chart legend.
*
* @return the chart legend (possibly <code>null</code>).
*/
public Legend getLegend() {
return legend;
}
/**
* Sets the chart legend. Registered listeners are notified that the chart
* has been modified.
*
* @param legend the new chart legend (null permitted).
*/
public void setLegend(Legend legend) {
// if there is an existing legend, remove the chart from the list of
// change listeners...
Legend existing = this.legend;
if (existing != null) {
existing.removeChangeListener(this);
}
// set the new legend, and register the chart as a change listener...
this.legend = legend;
if (legend != null) {
legend.addChangeListener(this);
}
// notify chart change listeners...
fireChartChanged();
}
/**
* Returns the plot for the chart. The plot is a class responsible for
* coordinating the visual representation of the data, including the axes
* (if any).
*
* @return the plot.
*/
public Plot getPlot() {
return this.plot;
}
/**
* Returns the plot cast as a {@link CategoryPlot}.
* <p>
* NOTE: if the plot is not an instance of {@link CategoryPlot}, then a
* <code>ClassCastException</code> is thrown.
*
* @return the plot.
*/
public CategoryPlot getCategoryPlot() {
return (CategoryPlot) this.plot;
}
/**
* Returns the plot cast as an {@link XYPlot}.
* <p>
* NOTE: if the plot is not an instance of {@link XYPlot}, then a
* <code>ClassCastException</code> is thrown.
*
* @return the plot.
*/
public XYPlot getXYPlot() {
return (XYPlot) this.plot;
}
/**
* Returns a flag that indicates whether or not anti-aliasing is used when
* the chart is drawn.
*
* @return the flag.
*/
public boolean getAntiAlias() {
return antialias;
}
/**
* Sets a flag that indicates whether or not anti-aliasing is used when the
* chart is drawn.
* <P>
* Anti-aliasing usually improves the appearance of charts.
*
* @param flag the new value of the flag.
*/
public void setAntiAlias(boolean flag) {
if (this.antialias != flag) {
this.antialias = flag;
fireChartChanged();
}
}
/**
* Returns the color/shade used to fill the chart background.
*
* @return the color/shade used to fill the chart background.
*/
public Paint getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Sets the color/shade used to fill the chart background. All registered
* listeners are notified that the chart has been changed.
*
* @param paint the new background color/shade.
*/
public void setBackgroundPaint(Paint paint) {
if (this.backgroundPaint != null) {
if (!this.backgroundPaint.equals(paint)) {
this.backgroundPaint = paint;
fireChartChanged();
}
}
else {
if (paint != null) {
this.backgroundPaint = paint;
fireChartChanged();
}
}
}
/**
* Returns the chart's background image (possibly null).
*
* @return the image.
*/
public Image getBackgroundImage() {
return this.backgroundImage;
}
/**
* Sets the chart's background image (null permitted). Registered listeners
* are notified that the chart has been changed.
*
* @param image the image.
*/
public void setBackgroundImage(Image image) {
if (this.backgroundImage != null) {
if (!this.backgroundImage.equals(image)) {
this.backgroundImage = image;
fireChartChanged();
}
}
else {
if (image != null) {
this.backgroundImage = image;
fireChartChanged();
}
}
}
/**
* Returns the background image alignment. Alignment constants are defined in the
* <code>com.jrefinery.ui.Align</code> class in the JCommon class library.
*
* @return The alignment.
*/
public int getBackgroundImageAlignment() {
return this.backgroundImageAlignment;
}
/**
* Sets the background alignment.
* <p>
* Alignment options are defined by the {@link org.jfree.ui.Align} class.
*
* @param alignment the alignment.
*/
public void setBackgroundImageAlignment(int alignment) {
if (this.backgroundImageAlignment != alignment) {
this.backgroundImageAlignment = alignment;
fireChartChanged();
}
}
/**
* 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 <code>true</code> or <code>false</code>.
*/
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);
}
/**
* 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 info records info about the drawing (null means collect no info).
*/
public void draw(Graphics2D g2, Rectangle2D chartArea, 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);
// set anti-alias...
if (antialias) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
else {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
// draw the chart background...
if (backgroundPaint != null) {
g2.setPaint(backgroundPaint);
g2.fill(chartArea);
}
if (backgroundImage != null) {
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
this.backgroundImageAlpha));
Rectangle2D dest = new Rectangle2D.Double(0.0, 0.0,
backgroundImage.getWidth(null),
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);
if (this.title != null) {
drawTitle(this.title, g2, nonTitleArea);
}
Iterator iterator = this.subtitles.iterator();
while (iterator.hasNext()) {
AbstractTitle currentTitle = (AbstractTitle) iterator.next();
drawTitle(currentTitle, g2, nonTitleArea);
}
// 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 (legend != null) {
plotArea.setRect(legend.draw(g2, nonTitleArea, info));
}
// draw the plot (axes and data visualisation)
plot.draw(g2, plotArea, info);
g2.setClip(savedClip);
notifyListeners(
new ChartProgressEvent(this, this, ChartProgressEvent.DRAWING_FINISHED, 100)
);
}
/**
* Draws a title.
* <P>
* The title should be drawn at the top, bottom, left or right of the nonTitleArea, and
* the area should be updated to reflect the amount of space used by the title.
*
* @param title the title.
* @param g2 the graphics device.
* @param nonTitleArea the area.
*/
public void drawTitle(AbstractTitle title, Graphics2D g2, Rectangle2D nonTitleArea) {
Rectangle2D titleArea = new Rectangle2D.Double();
double availableHeight = 0.0;
double availableWidth = 0.0;
switch (title.getPosition()) {
case AbstractTitle.TOP :
availableHeight = Math.min(title.getPreferredHeight(g2), nonTitleArea.getHeight());
availableWidth = nonTitleArea.getWidth();
titleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
availableWidth, availableHeight);
title.draw(g2, titleArea);
nonTitleArea.setRect(nonTitleArea.getX(),
Math.min(nonTitleArea.getY() + availableHeight,
nonTitleArea.getMaxY()),
availableWidth,
Math.max(nonTitleArea.getHeight() - availableHeight, 0));
break;
case AbstractTitle.BOTTOM :
availableHeight = Math.min(title.getPreferredHeight(g2), nonTitleArea.getHeight());
availableWidth = nonTitleArea.getWidth();
titleArea.setRect(nonTitleArea.getX(),
nonTitleArea.getMaxY() - availableHeight,
availableWidth, availableHeight);
title.draw(g2, titleArea);
nonTitleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
availableWidth,
nonTitleArea.getHeight() - availableHeight);
break;
case AbstractTitle.RIGHT :
availableHeight = nonTitleArea.getHeight();
availableWidth = Math.min(title.getPreferredWidth(g2), nonTitleArea.getWidth());
titleArea.setRect(nonTitleArea.getMaxX() - availableWidth,
nonTitleArea.getY(), availableWidth, availableHeight);
title.draw(g2, titleArea);
nonTitleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
nonTitleArea.getWidth() - availableWidth,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -