Search in sources :

Example 1 with TextBlock

use of org.jfree.chart.text.TextBlock in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class CategoryAxis method refreshTicks.

/**
 * Creates a temporary list of ticks that can be used when drawing the axis.
 *
 * @param g2  the graphics device (used to get font measurements).
 * @param state  the axis state.
 * @param dataArea  the area inside the axes.
 * @param edge  the location of the axis.
 *
 * @return A list of ticks.
 */
@Override
public List refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) {
    // FIXME generics
    List ticks = new java.util.ArrayList();
    // sanity check for data area...
    if (dataArea.getHeight() <= 0.0 || dataArea.getWidth() < 0.0) {
        return ticks;
    }
    CategoryPlot plot = (CategoryPlot) getPlot();
    List categories = plot.getCategoriesForAxis(this);
    double max = 0.0;
    if (categories != null) {
        CategoryLabelPosition position = this.categoryLabelPositions.getLabelPosition(edge);
        float r = this.maximumCategoryLabelWidthRatio;
        if (r <= 0.0) {
            r = position.getWidthRatio();
        }
        float l;
        if (position.getWidthType() == CategoryLabelWidthType.CATEGORY) {
            l = (float) calculateCategorySize(categories.size(), dataArea, edge);
        } else {
            if (RectangleEdge.isLeftOrRight(edge)) {
                l = (float) dataArea.getWidth();
            } else {
                l = (float) dataArea.getHeight();
            }
        }
        int categoryIndex = 0;
        for (Object o : categories) {
            Comparable category = (Comparable) o;
            g2.setFont(getTickLabelFont(category));
            TextBlock label = createLabel(category, l * r, edge, g2);
            if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
                max = Math.max(max, calculateCategoryLabelHeight(label, position, getTickLabelInsets(), g2));
            } else if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
                max = Math.max(max, calculateCategoryLabelWidth(label, position, getTickLabelInsets(), g2));
            }
            Tick tick = new CategoryTick(category, label, position.getLabelAnchor(), position.getRotationAnchor(), position.getAngle());
            ticks.add(tick);
            categoryIndex = categoryIndex + 1;
        }
    }
    state.setMax(max);
    return ticks;
}
Also used : CategoryPlot(org.jfree.chart.plot.CategoryPlot) Paint(java.awt.Paint) List(java.util.List) TextBlock(org.jfree.chart.text.TextBlock)

Example 2 with TextBlock

use of org.jfree.chart.text.TextBlock in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class Plot method drawNoDataMessage.

/**
 * Draws a message to state that there is no data to plot.
 *
 * @param g2  the graphics device.
 * @param area  the area within which the plot should be drawn.
 */
protected void drawNoDataMessage(Graphics2D g2, Rectangle2D area) {
    Shape savedClip = g2.getClip();
    g2.clip(area);
    String message = this.noDataMessage;
    if (message != null) {
        g2.setFont(this.noDataMessageFont);
        g2.setPaint(this.noDataMessagePaint);
        TextBlock block = TextUtils.createTextBlock(this.noDataMessage, this.noDataMessageFont, this.noDataMessagePaint, 0.9f * (float) area.getWidth(), new G2TextMeasurer(g2));
        block.draw(g2, (float) area.getCenterX(), (float) area.getCenterY(), TextBlockAnchor.CENTER);
    }
    g2.setClip(savedClip);
}
Also used : Shape(java.awt.Shape) G2TextMeasurer(org.jfree.chart.text.G2TextMeasurer) TextBlock(org.jfree.chart.text.TextBlock)

Example 3 with TextBlock

use of org.jfree.chart.text.TextBlock in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class CategoryTickTest method testHashCode.

/**
 * Two objects that are equal are required to return the same hashCode.
 */
@Test
public void testHashCode() {
    Comparable<String> c1 = "C1";
    TextBlock tb1 = new TextBlock();
    tb1.addLine(new TextLine("Block 1"));
    tb1.addLine(new TextLine("Block 2"));
    TextBlockAnchor tba1 = TextBlockAnchor.CENTER;
    TextAnchor ta1 = TextAnchor.CENTER;
    CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
    CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
    assertEquals(t1, t2);
    int h1 = t1.hashCode();
    int h2 = t2.hashCode();
    assertEquals(h1, h2);
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) TextLine(org.jfree.chart.text.TextLine) TextBlockAnchor(org.jfree.chart.text.TextBlockAnchor) TextBlock(org.jfree.chart.text.TextBlock) Test(org.junit.jupiter.api.Test)

Example 4 with TextBlock

use of org.jfree.chart.text.TextBlock in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class ExtendedCategoryAxis method createLabel.

/**
 * Overrides the default behaviour by adding the sublabel to the text
 * block that is used for the category label.
 *
 * @param category  the category.
 * @param width  the width (not used yet).
 * @param edge  the location of the axis.
 * @param g2  the graphics device.
 *
 * @return A label.
 */
@Override
protected TextBlock createLabel(Comparable category, float width, RectangleEdge edge, Graphics2D g2) {
    TextBlock label = super.createLabel(category, width, edge, g2);
    String s = (String) this.sublabels.get(category);
    if (s != null) {
        if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
            TextLine line = new TextLine(s, this.sublabelFont, this.sublabelPaint);
            label.addLine(line);
        } else if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
            TextLine line = label.getLastLine();
            if (line != null) {
                line.addFragment(new TextFragment("  " + s, this.sublabelFont, this.sublabelPaint));
            }
        }
    }
    return label;
}
Also used : TextLine(org.jfree.chart.text.TextLine) TextFragment(org.jfree.chart.text.TextFragment) TextBlock(org.jfree.chart.text.TextBlock)

Example 5 with TextBlock

use of org.jfree.chart.text.TextBlock in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class CategoryAxis method drawCategoryLabels.

/**
 * Draws the category labels and returns the updated axis state.
 *
 * @param g2  the graphics device ({@code null} not permitted).
 * @param plotArea  the plot area ({@code null} not permitted).
 * @param dataArea  the area inside the axes ({@code null} not
 *                  permitted).
 * @param edge  the axis location ({@code null} not permitted).
 * @param state  the axis state ({@code null} not permitted).
 * @param plotState  collects information about the plot ({@code null}
 *                   permitted).
 *
 * @return The updated axis state (never {@code null}).
 */
protected AxisState drawCategoryLabels(Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state, PlotRenderingInfo plotState) {
    Args.nullNotPermitted(state, "state");
    if (!isTickLabelsVisible()) {
        return state;
    }
    List ticks = refreshTicks(g2, state, plotArea, edge);
    state.setTicks(ticks);
    int categoryIndex = 0;
    for (Object o : ticks) {
        CategoryTick tick = (CategoryTick) o;
        g2.setFont(getTickLabelFont(tick.getCategory()));
        g2.setPaint(getTickLabelPaint(tick.getCategory()));
        CategoryLabelPosition position = this.categoryLabelPositions.getLabelPosition(edge);
        double x0 = 0.0;
        double x1 = 0.0;
        double y0 = 0.0;
        double y1 = 0.0;
        if (edge == RectangleEdge.TOP) {
            x0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
            x1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
            y1 = state.getCursor() - this.categoryLabelPositionOffset;
            y0 = y1 - state.getMax();
        } else if (edge == RectangleEdge.BOTTOM) {
            x0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
            x1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
            y0 = state.getCursor() + this.categoryLabelPositionOffset;
            y1 = y0 + state.getMax();
        } else if (edge == RectangleEdge.LEFT) {
            y0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
            y1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
            x1 = state.getCursor() - this.categoryLabelPositionOffset;
            x0 = x1 - state.getMax();
        } else if (edge == RectangleEdge.RIGHT) {
            y0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge);
            y1 = getCategoryEnd(categoryIndex, ticks.size(), dataArea, edge);
            x0 = state.getCursor() + this.categoryLabelPositionOffset;
            x1 = x0 - state.getMax();
        }
        Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0));
        Point2D anchorPoint = position.getCategoryAnchor().getAnchorPoint(area);
        TextBlock block = tick.getLabel();
        block.draw(g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getLabelAnchor(), (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getAngle());
        Shape bounds = block.calculateBounds(g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getLabelAnchor(), (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getAngle());
        if (plotState != null && plotState.getOwner() != null) {
            EntityCollection entities = plotState.getOwner().getEntityCollection();
            if (entities != null) {
                String tooltip = getCategoryLabelToolTip(tick.getCategory());
                String url = getCategoryLabelURL(tick.getCategory());
                entities.add(new CategoryLabelEntity(tick.getCategory(), bounds, tooltip, url));
            }
        }
        categoryIndex++;
    }
    if (edge.equals(RectangleEdge.TOP)) {
        double h = state.getMax() + this.categoryLabelPositionOffset;
        state.cursorUp(h);
    } else if (edge.equals(RectangleEdge.BOTTOM)) {
        double h = state.getMax() + this.categoryLabelPositionOffset;
        state.cursorDown(h);
    } else if (edge == RectangleEdge.LEFT) {
        double w = state.getMax() + this.categoryLabelPositionOffset;
        state.cursorLeft(w);
    } else if (edge == RectangleEdge.RIGHT) {
        double w = state.getMax() + this.categoryLabelPositionOffset;
        state.cursorRight(w);
    }
    return state;
}
Also used : Shape(java.awt.Shape) Rectangle2D(java.awt.geom.Rectangle2D) Paint(java.awt.Paint) CategoryLabelEntity(org.jfree.chart.entity.CategoryLabelEntity) Point2D(java.awt.geom.Point2D) EntityCollection(org.jfree.chart.entity.EntityCollection) List(java.util.List) TextBlock(org.jfree.chart.text.TextBlock)

Aggregations

TextBlock (org.jfree.chart.text.TextBlock)8 Test (org.junit.jupiter.api.Test)4 TextLine (org.jfree.chart.text.TextLine)3 Paint (java.awt.Paint)2 Shape (java.awt.Shape)2 List (java.util.List)2 TextAnchor (org.jfree.chart.text.TextAnchor)2 TextBlockAnchor (org.jfree.chart.text.TextBlockAnchor)2 Point2D (java.awt.geom.Point2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 CategoryLabelEntity (org.jfree.chart.entity.CategoryLabelEntity)1 EntityCollection (org.jfree.chart.entity.EntityCollection)1 CategoryPlot (org.jfree.chart.plot.CategoryPlot)1 G2TextMeasurer (org.jfree.chart.text.G2TextMeasurer)1 TextFragment (org.jfree.chart.text.TextFragment)1