📄 defaultoldlegend.java
字号:
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()); 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 - getMargin().calculateBottomOutset(available.getHeight()) : available.getY() + getMargin().calculateTopOutset(available.getHeight()); double xloc; if (isAnchoredToLeft()) { xloc = available.getX() + getMargin().calculateLeftOutset(available.getWidth()); } else if (isAnchoredToCenter()) { xloc = available.getX() + available.getWidth() / 2 - maxRowWidth / 2; } else if (isAnchoredToRight()) { xloc = available.getX() + available.getWidth() - maxRowWidth - getChart().getPlot().getInsets().getLeft(); } 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 - getMargin().calculateRightOutset(available.getWidth()) : available.getX() + getMargin().calculateLeftOutset(available.getWidth()); double yloc; if (isAnchoredToTop()) { yloc = available.getY() + getChart().getPlot().getInsets().getTop(); } else if (isAnchoredToMiddle()) { yloc = available.getY() + (available.getHeight() / 2) - (totalHeight / 2); } else if (isAnchoredToBottom()) { yloc = available.getY() + available.getHeight() - getChart().getPlot().getInsets().getBottom() - 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().getFillPaint()); g2.setPaint(this.itemPaint); g2.setFont(getTitleFont()); TextUtilities.drawAlignedString( legendTitle.getItem().getLabel(), g2, (float) legendTitle.getLabelPosition().getX(), (float) legendTitle.getLabelPosition().getY(), TextAnchor.CENTER_LEFT ); } } /** * 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().getFillPaint()); Shape keyBox = item.getMarker(); if (item.getItem().isLineVisible()) { g2.setStroke(item.getItem().getLineStroke()); g2.draw(item.getLine()); if (item.getItem().isShapeVisible()) { if (item.getItem().isShapeFilled()) { g2.fill(keyBox); } else { g2.draw(keyBox); } } } else { if (item.getItem().isShapeFilled()) { g2.fill(keyBox); } if (item.getItem().isShapeOutlineVisible()) { g2.setPaint(item.getItem().getOutlinePaint()); g2.setStroke(item.getItem().getOutlineStroke()); 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 ); 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.add(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. */ private Rectangle2D calcRemainingDrawingArea(Rectangle2D available, boolean horizontal, boolean inverted, RectangularShape legendArea) { if (horizontal) { // The remaining drawing area bounding box will have the same // x origin, width and height independent of the anchor's // location. The variable is the y coordinate. If the anchor is // SOUTH, the y coordinate is simply the original y coordinate // of the available area. If it is NORTH, we adjust original y // by the total height of the legend and the initial gap. double yy = available.getY(); double yloc = (inverted) ? yy : yy + legendArea.getHeight() + getMargin().calculateBottomOutset(available.getHeight()); // return the remaining available drawing area return new Rectangle2D.Double( available.getX(), yloc, available.getWidth(), available.getHeight() - legendArea.getHeight() - getMargin().calculateTopOutset(available.getHeight()) - getMargin().calculateBottomOutset(available.getHeight()) ); } else { // The remaining drawing area bounding box will have the same // y origin, width and height independent of the anchor's // location. The variable is the x coordinate. If the anchor is // EAST, the x coordinate is simply the original x coordinate
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -