📄 standardlegend.java
字号:
LegendItemCollection legendItems = getChart().getPlot().getLegendItems();
if (legendItems == null || legendItems.getItemCount() == 0) {
return available;
}
// else...
DrawableLegendItem legendTitle = null;
LegendItem titleItem = null;
if (this.title != null && !this.title.equals("")) {
titleItem = new LegendItem(
this.title, this.title, null, true, Color.black,
new BasicStroke(1.0f), Color.black, new BasicStroke(1.0f)
);
}
RectangularShape legendArea;
double availableWidth = available.getWidth();
// the translation point for the origin of the drawing system
Point2D translation;
// Create buffer for individual items within the legend
List items = new ArrayList();
// Compute individual rectangles in the legend, translation point as well
// as the bounding box for the legend.
if (horizontal) {
double xstart = available.getX() + getOuterGap().getLeftSpace(availableWidth);
double xlimit = available.getMaxX() - getOuterGap().getRightSpace(availableWidth);
double maxRowWidth = 0;
double xoffset = 0;
double rowHeight = 0;
double totalHeight = 0;
boolean wrappingAllowed = true;
if (titleItem != null) {
g2.setFont(getTitleFont());
legendTitle = createDrawableLegendItem(
g2, titleItem, xoffset, totalHeight
);
rowHeight = Math.max(0, legendTitle.getHeight());
xoffset += legendTitle.getWidth();
}
g2.setFont(this.itemFont);
for (int i = 0; i < legendItems.getItemCount(); i++) {
DrawableLegendItem item;
if (this.renderingOrder == LegendRenderingOrder.STANDARD) {
item = createDrawableLegendItem(
g2, legendItems.get(i), xoffset, totalHeight
);
}
else if (this.renderingOrder == LegendRenderingOrder.REVERSE) {
item = createDrawableLegendItem(
g2, legendItems.get(legendItems.getItemCount() - i - 1), xoffset,
totalHeight
);
}
else {
// we're not supposed to get here, will cause NullPointerException
item = null;
}
if (item.getMaxX() + xstart > xlimit && wrappingAllowed) {
// start a new row
maxRowWidth = Math.max(maxRowWidth, xoffset);
xoffset = 0;
totalHeight += rowHeight;
i--; // redo this item in the next row
// if item to big to fit, we dont want to attempt wrapping endlessly.
// we therefore disable wrapping for at least one item.
wrappingAllowed = false;
}
else {
// continue current row
rowHeight = Math.max(rowHeight, item.getHeight());
xoffset += item.getWidth();
// we placed an item in this row, re-allow wrapping for next item.
wrappingAllowed = true;
items.add(item);
}
}
maxRowWidth = Math.max(maxRowWidth, xoffset);
totalHeight += rowHeight;
// Create the bounding box
legendArea = new RoundRectangle2D.Double(
0, 0, maxRowWidth, totalHeight, this.boundingBoxArcWidth, this.boundingBoxArcHeight
);
translation = createTranslationPointForHorizontalDraw(
available, inverted, maxRowWidth, totalHeight
);
}
else { // vertical...
double totalHeight = 0;
double maxWidth = (this.preferredWidth == NO_PREFERRED_WIDTH) ? 0 : this.preferredWidth;
if (titleItem != null) {
g2.setFont(getTitleFont());
legendTitle = createDrawableLegendItem(g2, titleItem, 0, totalHeight);
totalHeight += legendTitle.getHeight();
maxWidth = Math.max(maxWidth, legendTitle.getWidth());
}
g2.setFont(this.itemFont);
int legendItemsLength = legendItems.getItemCount();
for (int i = 0; i < legendItemsLength; i++) {
List drawableParts;
if (this.renderingOrder == LegendRenderingOrder.STANDARD) {
drawableParts = createAllDrawableLinesForItem(g2,
legendItems.get(i), 0, totalHeight, maxWidth);
}
else if (this.renderingOrder == LegendRenderingOrder.REVERSE) {
drawableParts = createAllDrawableLinesForItem(
g2, legendItems.get(legendItemsLength - i - 1), 0, totalHeight, maxWidth
);
}
else {
// we're not supposed to get here, will cause NullPointerException
drawableParts = null;
}
for (Iterator j = drawableParts.iterator(); j.hasNext();) {
DrawableLegendItem item = (DrawableLegendItem) j.next();
totalHeight += item.getHeight();
maxWidth = Math.max(maxWidth, item.getWidth());
items.add(item);
}
}
// Create the bounding box
legendArea = new RoundRectangle2D.Float(
0, 0, (float) maxWidth, (float) totalHeight,
this.boundingBoxArcWidth, this.boundingBoxArcHeight
);
translation = createTranslationPointForVerticalDraw(
available, inverted, totalHeight, maxWidth
);
}
// Move the origin of the drawing to the appropriate location
g2.translate(translation.getX(), translation.getY());
LOGGER.debug("legendArea = " + legendArea.getWidth() + ", " + legendArea.getHeight());
drawLegendBox(g2, legendArea);
drawLegendTitle(g2, legendTitle);
drawSeriesElements(g2, items, translation, info);
// translate the origin back to what it was prior to drawing the legend
g2.translate(-translation.getX(), -translation.getY());
return calcRemainingDrawingArea(available, horizontal, inverted, legendArea);
}
/**
* ???
*
* @param available the available area.
* @param inverted inverted?
* @param maxRowWidth the maximum row width.
* @param totalHeight the total height.
*
* @return The translation point.
*/
private Point2D createTranslationPointForHorizontalDraw(Rectangle2D available,
boolean inverted, double maxRowWidth, double totalHeight) {
// The yloc point is the variable part of the translation point
// for horizontal legends xloc can be: left, center or right.
double yloc = (inverted)
? available.getMaxY() - totalHeight
- getOuterGap().getBottomSpace(available.getHeight())
: available.getY() + getOuterGap().getTopSpace(available.getHeight());
double xloc;
if (isAnchoredToLeft()) {
xloc = available.getX() + getChart().getPlot().getInsets().left;
}
else if (isAnchoredToCenter()) {
xloc = available.getX() + available.getWidth() / 2 - maxRowWidth / 2;
}
else if (isAnchoredToRight()) {
xloc = available.getX() + available.getWidth()
- maxRowWidth - getChart().getPlot().getInsets().left;
}
else {
throw new IllegalStateException(UNEXPECTED_LEGEND_ANCHOR);
}
// Create the translation point
return new Point2D.Double(xloc, yloc);
}
/**
* ???
*
* @param available the available area.
* @param inverted inverted?
* @param totalHeight the total height.
* @param maxWidth the maximum width.
*
* @return The translation point.
*/
private Point2D createTranslationPointForVerticalDraw(Rectangle2D available,
boolean inverted, double totalHeight, double maxWidth) {
// The xloc point is the variable part of the translation point
// for vertical legends yloc can be: top, middle or bottom.
double xloc = (inverted)
? available.getMaxX() - maxWidth - getOuterGap().getRightSpace(available.getWidth())
: available.getX() + getOuterGap().getLeftSpace(available.getWidth());
double yloc;
if (isAnchoredToTop()) {
yloc = available.getY() + getChart().getPlot().getInsets().top;
}
else if (isAnchoredToMiddle()) {
yloc = available.getY() + (available.getHeight() / 2) - (totalHeight / 2);
}
else if (isAnchoredToBottom()) {
yloc = available.getY() + available.getHeight()
- getChart().getPlot().getInsets().bottom - totalHeight;
}
else {
throw new IllegalStateException(UNEXPECTED_LEGEND_ANCHOR);
}
// Create the translation point
return new Point2D.Double(xloc, yloc);
}
/**
* Draws the legend title.
*
* @param g2 the graphics device (<code>null</code> not permitted).
* @param legendTitle the title (<code>null</code> permitted, in which case the method
* does nothing).
*/
private void drawLegendTitle(Graphics2D g2, DrawableLegendItem legendTitle) {
if (legendTitle != null) {
// XXX dsm - make title bold?
g2.setPaint(legendTitle.getItem().getPaint());
g2.setPaint(this.itemPaint);
g2.setFont(getTitleFont());
TextUtilities.drawAlignedString(
legendTitle.getItem().getLabel(), g2,
(float) legendTitle.getLabelPosition().getX(),
(float) legendTitle.getLabelPosition().getY(), TextAnchor.CENTER_LEFT
);
LOGGER.debug("Title x = " + legendTitle.getLabelPosition().getX());
LOGGER.debug("Title y = " + legendTitle.getLabelPosition().getY());
}
}
/**
* Draws the bounding box for the legend.
*
* @param g2 the graphics device.
* @param legendArea the legend area.
*/
private void drawLegendBox(Graphics2D g2, RectangularShape legendArea) {
// Draw the legend's bounding box
g2.setPaint(this.backgroundPaint);
g2.fill(legendArea);
g2.setPaint(this.outlinePaint);
g2.setStroke(this.outlineStroke);
g2.draw(legendArea);
}
/**
* Draws the series elements.
*
* @param g2 the graphics device.
* @param items the items.
* @param translation the translation point.
* @param info optional carrier for rendering info.
*/
private void drawSeriesElements(Graphics2D g2, List items, Point2D translation,
ChartRenderingInfo info) {
EntityCollection entities = null;
if (info != null) {
entities = info.getEntityCollection();
}
// Draw individual series elements
for (int i = 0; i < items.size(); i++) {
DrawableLegendItem item = (DrawableLegendItem) items.get(i);
g2.setPaint(item.getItem().getPaint());
Shape keyBox = item.getMarker();
if (this.displaySeriesLines) {
g2.setStroke(item.getLineStroke());
g2.draw(item.getLine());
if (this.displaySeriesShapes) {
if (item.isMarkerFilled()) {
g2.fill(keyBox);
}
else {
g2.draw(keyBox);
}
}
}
else {
if (item.isMarkerFilled()) {
g2.fill(keyBox);
}
else {
g2.draw(keyBox);
}
}
if (getOutlineShapes()) {
g2.setPaint(this.shapeOutlinePaint);
g2.setStroke(this.shapeOutlineStroke);
g2.draw(keyBox);
}
g2.setPaint(this.itemPaint);
g2.setFont(this.itemFont);
TextUtilities.drawAlignedString(
item.getItem().getLabel(), g2,
(float) item.getLabelPosition().getX(), (float) item.getLabelPosition().getY(),
TextAnchor.CENTER_LEFT
);
LOGGER.debug("Item x = " + item.getLabelPosition().getX());
LOGGER.debug("Item y = " + item.getLabelPosition().getY());
if (entities != null) {
Rectangle2D area = new Rectangle2D.Double(
translation.getX() + item.getX(),
translation.getY() + item.getY(),
item.getWidth(), item.getHeight()
);
LegendItemEntity entity = new LegendItemEntity(area);
entity.setSeriesIndex(i);
entities.addEntity(entity);
}
}
}
/**
* Calculates the remaining drawing area.
*
* @param available the available area.
* @param horizontal horizontal?
* @param inverted inverted?
* @param legendArea the legend area.
*
* @return The remaining drawing area.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -