Search in sources :

Example 11 with RectangleInsets

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

the class Axis method getLabelEnclosure.

/**
 * Returns a rectangle that encloses the axis label.  This is typically
 * used for layout purposes (it gives the maximum dimensions of the label).
 *
 * @param g2  the graphics device.
 * @param edge  the edge of the plot area along which the axis is measuring.
 *
 * @return The enclosing rectangle.
 */
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
    Rectangle2D result = new Rectangle2D.Double();
    Rectangle2D bounds = null;
    if (this.attributedLabel != null) {
        TextLayout layout = new TextLayout(this.attributedLabel.getIterator(), g2.getFontRenderContext());
        bounds = layout.getBounds();
    } else {
        String axisLabel = getLabel();
        if (axisLabel != null && !axisLabel.equals("")) {
            FontMetrics fm = g2.getFontMetrics(getLabelFont());
            bounds = TextUtils.getTextBounds(axisLabel, g2, fm);
        }
    }
    if (bounds != null) {
        RectangleInsets insets = getLabelInsets();
        bounds = insets.createOutsetRectangle(bounds);
        double angle = getLabelAngle();
        if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
            angle = angle - Math.PI / 2.0;
        }
        double x = bounds.getCenterX();
        double y = bounds.getCenterY();
        AffineTransform transformer = AffineTransform.getRotateInstance(angle, x, y);
        Shape labelBounds = transformer.createTransformedShape(bounds);
        result = labelBounds.getBounds2D();
    }
    return result;
}
Also used : Shape(java.awt.Shape) FontMetrics(java.awt.FontMetrics) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.chart.api.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) AttributedString(java.text.AttributedString) TextLayout(java.awt.font.TextLayout)

Example 12 with RectangleInsets

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

the class CategoryPlotTest method testEquals.

/**
 * Test that the equals() method differentiates all the required fields.
 */
@Test
public void testEquals() {
    CategoryPlot<String, String> plot1 = new CategoryPlot<>();
    CategoryPlot<String, String> plot2 = new CategoryPlot<>();
    assertEquals(plot1, plot2);
    assertEquals(plot2, plot1);
    // orientation...
    plot1.setOrientation(PlotOrientation.HORIZONTAL);
    assertNotEquals(plot1, plot2);
    plot2.setOrientation(PlotOrientation.HORIZONTAL);
    assertEquals(plot1, plot2);
    // axisOffset...
    plot1.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
    assertNotEquals(plot1, plot2);
    plot2.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
    assertEquals(plot1, plot2);
    // domainAxis - no longer a separate field but test anyway...
    plot1.setDomainAxis(new CategoryAxis("Category Axis"));
    assertNotEquals(plot1, plot2);
    plot2.setDomainAxis(new CategoryAxis("Category Axis"));
    assertEquals(plot1, plot2);
    // domainAxes...
    plot1.setDomainAxis(11, new CategoryAxis("Secondary Axis"));
    assertNotEquals(plot1, plot2);
    plot2.setDomainAxis(11, new CategoryAxis("Secondary Axis"));
    assertEquals(plot1, plot2);
    // domainAxisLocation - no longer a separate field but test anyway...
    plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
    assertNotEquals(plot1, plot2);
    plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
    assertEquals(plot1, plot2);
    // domainAxisLocations...
    plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
    assertNotEquals(plot1, plot2);
    plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
    assertEquals(plot1, plot2);
    // draw shared domain axis...
    plot1.setDrawSharedDomainAxis(!plot1.getDrawSharedDomainAxis());
    assertNotEquals(plot1, plot2);
    plot2.setDrawSharedDomainAxis(!plot2.getDrawSharedDomainAxis());
    assertEquals(plot1, plot2);
    // rangeAxis - no longer a separate field but test anyway...
    plot1.setRangeAxis(new NumberAxis("Range Axis"));
    assertNotEquals(plot1, plot2);
    plot2.setRangeAxis(new NumberAxis("Range Axis"));
    assertEquals(plot1, plot2);
    // rangeAxes...
    plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
    assertNotEquals(plot1, plot2);
    plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
    assertEquals(plot1, plot2);
    // rangeAxisLocation - no longer a separate field but test anyway...
    plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
    assertNotEquals(plot1, plot2);
    plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
    assertEquals(plot1, plot2);
    // rangeAxisLocations...
    plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
    assertNotEquals(plot1, plot2);
    plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
    assertEquals(plot1, plot2);
    // datasetToDomainAxisMap...
    plot1.mapDatasetToDomainAxis(11, 11);
    assertNotEquals(plot1, plot2);
    plot2.mapDatasetToDomainAxis(11, 11);
    assertEquals(plot1, plot2);
    // datasetToRangeAxisMap...
    plot1.mapDatasetToRangeAxis(11, 11);
    assertNotEquals(plot1, plot2);
    plot2.mapDatasetToRangeAxis(11, 11);
    assertEquals(plot1, plot2);
    // renderer - no longer a separate field but test anyway...
    plot1.setRenderer(new AreaRenderer());
    assertNotEquals(plot1, plot2);
    plot2.setRenderer(new AreaRenderer());
    assertEquals(plot1, plot2);
    // renderers...
    plot1.setRenderer(11, new AreaRenderer());
    assertNotEquals(plot1, plot2);
    plot2.setRenderer(11, new AreaRenderer());
    assertEquals(plot1, plot2);
    // rendering order...
    plot1.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    assertNotEquals(plot1, plot2);
    plot2.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    assertEquals(plot1, plot2);
    // columnRenderingOrder...
    plot1.setColumnRenderingOrder(SortOrder.DESCENDING);
    assertNotEquals(plot1, plot2);
    plot2.setColumnRenderingOrder(SortOrder.DESCENDING);
    assertEquals(plot1, plot2);
    // rowRenderingOrder...
    plot1.setRowRenderingOrder(SortOrder.DESCENDING);
    assertNotEquals(plot1, plot2);
    plot2.setRowRenderingOrder(SortOrder.DESCENDING);
    assertEquals(plot1, plot2);
    // domainGridlinesVisible
    plot1.setDomainGridlinesVisible(true);
    assertNotEquals(plot1, plot2);
    plot2.setDomainGridlinesVisible(true);
    assertEquals(plot1, plot2);
    // domainGridlinePosition
    plot1.setDomainGridlinePosition(CategoryAnchor.END);
    assertNotEquals(plot1, plot2);
    plot2.setDomainGridlinePosition(CategoryAnchor.END);
    assertEquals(plot1, plot2);
    // domainGridlineStroke
    Stroke stroke = new BasicStroke(2.0f);
    plot1.setDomainGridlineStroke(stroke);
    assertNotEquals(plot1, plot2);
    plot2.setDomainGridlineStroke(stroke);
    assertEquals(plot1, plot2);
    // domainGridlinePaint
    plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.BLUE, 3.0f, 4.0f, Color.YELLOW));
    assertNotEquals(plot1, plot2);
    plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.BLUE, 3.0f, 4.0f, Color.YELLOW));
    assertEquals(plot1, plot2);
    // rangeGridlinesVisible
    plot1.setRangeGridlinesVisible(false);
    assertNotEquals(plot1, plot2);
    plot2.setRangeGridlinesVisible(false);
    assertEquals(plot1, plot2);
    // rangeGridlineStroke
    plot1.setRangeGridlineStroke(stroke);
    assertNotEquals(plot1, plot2);
    plot2.setRangeGridlineStroke(stroke);
    assertEquals(plot1, plot2);
    // rangeGridlinePaint
    plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.GREEN, 3.0f, 4.0f, Color.YELLOW));
    assertNotEquals(plot1, plot2);
    plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.GREEN, 3.0f, 4.0f, Color.YELLOW));
    assertEquals(plot1, plot2);
    // anchorValue
    plot1.setAnchorValue(100.0);
    assertNotEquals(plot1, plot2);
    plot2.setAnchorValue(100.0);
    assertEquals(plot1, plot2);
    // rangeCrosshairVisible
    plot1.setRangeCrosshairVisible(true);
    assertNotEquals(plot1, plot2);
    plot2.setRangeCrosshairVisible(true);
    assertEquals(plot1, plot2);
    // rangeCrosshairValue
    plot1.setRangeCrosshairValue(100.0);
    assertNotEquals(plot1, plot2);
    plot2.setRangeCrosshairValue(100.0);
    assertEquals(plot1, plot2);
    // rangeCrosshairStroke
    plot1.setRangeCrosshairStroke(stroke);
    assertNotEquals(plot1, plot2);
    plot2.setRangeCrosshairStroke(stroke);
    assertEquals(plot1, plot2);
    // rangeCrosshairPaint
    plot1.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.WHITE, 3.0f, 4.0f, Color.YELLOW));
    assertNotEquals(plot1, plot2);
    plot2.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.WHITE, 3.0f, 4.0f, Color.YELLOW));
    assertEquals(plot1, plot2);
    // rangeCrosshairLockedOnData
    plot1.setRangeCrosshairLockedOnData(false);
    assertNotEquals(plot1, plot2);
    plot2.setRangeCrosshairLockedOnData(false);
    assertEquals(plot1, plot2);
    // foreground domain markers
    plot1.addDomainMarker(new CategoryMarker("C1"), Layer.FOREGROUND);
    assertNotEquals(plot1, plot2);
    plot2.addDomainMarker(new CategoryMarker("C1"), Layer.FOREGROUND);
    assertEquals(plot1, plot2);
    // background domain markers
    plot1.addDomainMarker(new CategoryMarker("C2"), Layer.BACKGROUND);
    assertNotEquals(plot1, plot2);
    plot2.addDomainMarker(new CategoryMarker("C2"), Layer.BACKGROUND);
    assertEquals(plot1, plot2);
    // range markers - no longer separate fields but test anyway...
    plot1.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
    assertNotEquals(plot1, plot2);
    plot2.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
    assertEquals(plot1, plot2);
    plot1.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND);
    assertNotEquals(plot1, plot2);
    plot2.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND);
    assertEquals(plot1, plot2);
    // foreground range markers...
    plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
    assertNotEquals(plot1, plot2);
    plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
    assertEquals(plot1, plot2);
    // background range markers...
    plot1.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND);
    assertNotEquals(plot1, plot2);
    plot2.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND);
    assertEquals(plot1, plot2);
    // annotations
    plot1.addAnnotation(new CategoryTextAnnotation("Text", "Category", 43.0));
    assertNotEquals(plot1, plot2);
    plot2.addAnnotation(new CategoryTextAnnotation("Text", "Category", 43.0));
    assertEquals(plot1, plot2);
    // weight
    plot1.setWeight(3);
    assertNotEquals(plot1, plot2);
    plot2.setWeight(3);
    assertEquals(plot1, plot2);
    // fixed domain axis space...
    plot1.setFixedDomainAxisSpace(new AxisSpace());
    assertNotEquals(plot1, plot2);
    plot2.setFixedDomainAxisSpace(new AxisSpace());
    assertEquals(plot1, plot2);
    // fixed range axis space...
    plot1.setFixedRangeAxisSpace(new AxisSpace());
    assertNotEquals(plot1, plot2);
    plot2.setFixedRangeAxisSpace(new AxisSpace());
    assertEquals(plot1, plot2);
    // fixed legend items
    plot1.setFixedLegendItems(new LegendItemCollection());
    assertNotEquals(plot1, plot2);
    plot2.setFixedLegendItems(new LegendItemCollection());
    assertEquals(plot1, plot2);
    // crosshairDatasetIndex
    plot1.setCrosshairDatasetIndex(99);
    assertNotEquals(plot1, plot2);
    plot2.setCrosshairDatasetIndex(99);
    assertEquals(plot1, plot2);
    // domainCrosshairColumnKey
    plot1.setDomainCrosshairColumnKey("A");
    assertNotEquals(plot1, plot2);
    plot2.setDomainCrosshairColumnKey("A");
    assertEquals(plot1, plot2);
    // domainCrosshairRowKey
    plot1.setDomainCrosshairRowKey("B");
    assertNotEquals(plot1, plot2);
    plot2.setDomainCrosshairRowKey("B");
    assertEquals(plot1, plot2);
    // domainCrosshairVisible
    plot1.setDomainCrosshairVisible(true);
    assertNotEquals(plot1, plot2);
    plot2.setDomainCrosshairVisible(true);
    assertEquals(plot1, plot2);
    // domainCrosshairPaint
    plot1.setDomainCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.BLUE));
    assertNotEquals(plot1, plot2);
    plot2.setDomainCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.BLUE));
    assertEquals(plot1, plot2);
    // domainCrosshairStroke
    plot1.setDomainCrosshairStroke(new BasicStroke(1.23f));
    assertNotEquals(plot1, plot2);
    plot2.setDomainCrosshairStroke(new BasicStroke(1.23f));
    assertEquals(plot1, plot2);
    plot1.setRangeMinorGridlinesVisible(true);
    assertNotEquals(plot1, plot2);
    plot2.setRangeMinorGridlinesVisible(true);
    assertEquals(plot1, plot2);
    plot1.setRangeMinorGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.BLUE));
    assertNotEquals(plot1, plot2);
    plot2.setRangeMinorGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.BLUE));
    assertEquals(plot1, plot2);
    plot1.setRangeMinorGridlineStroke(new BasicStroke(1.23f));
    assertNotEquals(plot1, plot2);
    plot2.setRangeMinorGridlineStroke(new BasicStroke(1.23f));
    assertEquals(plot1, plot2);
    plot1.setRangeZeroBaselineVisible(!plot1.isRangeZeroBaselineVisible());
    assertNotEquals(plot1, plot2);
    plot2.setRangeZeroBaselineVisible(!plot2.isRangeZeroBaselineVisible());
    assertEquals(plot1, plot2);
    plot1.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.BLUE));
    assertNotEquals(plot1, plot2);
    plot2.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.BLUE));
    assertEquals(plot1, plot2);
    plot1.setRangeZeroBaselineStroke(new BasicStroke(1.23f));
    assertNotEquals(plot1, plot2);
    plot2.setRangeZeroBaselineStroke(new BasicStroke(1.23f));
    assertEquals(plot1, plot2);
    // shadowGenerator
    plot1.setShadowGenerator(new DefaultShadowGenerator(5, Color.GRAY, 0.6f, 4, -Math.PI / 4));
    assertNotEquals(plot1, plot2);
    plot2.setShadowGenerator(new DefaultShadowGenerator(5, Color.GRAY, 0.6f, 4, -Math.PI / 4));
    assertEquals(plot1, plot2);
    plot1.setShadowGenerator(null);
    assertNotEquals(plot1, plot2);
    plot2.setShadowGenerator(null);
    assertEquals(plot1, plot2);
}
Also used : BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) NumberAxis(org.jfree.chart.axis.NumberAxis) AreaRenderer(org.jfree.chart.renderer.category.AreaRenderer) LegendItemCollection(org.jfree.chart.legend.LegendItemCollection) GradientPaint(java.awt.GradientPaint) DefaultShadowGenerator(org.jfree.chart.util.DefaultShadowGenerator) CategoryAxis(org.jfree.chart.axis.CategoryAxis) RectangleInsets(org.jfree.chart.api.RectangleInsets) AxisSpace(org.jfree.chart.axis.AxisSpace) CategoryTextAnnotation(org.jfree.chart.annotations.CategoryTextAnnotation) Test(org.junit.jupiter.api.Test)

Example 13 with RectangleInsets

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

the class MarkerTest method testGetSetLabelOffset.

/**
 * Some checks for the getLabelOffset() and setLabelOffset() methods.
 */
@Test
public void testGetSetLabelOffset() {
    // we use ValueMarker for the tests, because we need a concrete
    // subclass...
    ValueMarker m = new ValueMarker(1.1);
    m.addChangeListener(this);
    this.lastEvent = null;
    assertEquals(new RectangleInsets(3, 3, 3, 3), m.getLabelOffset());
    m.setLabelOffset(new RectangleInsets(1, 2, 3, 4));
    assertEquals(new RectangleInsets(1, 2, 3, 4), m.getLabelOffset());
    assertEquals(m, this.lastEvent.getMarker());
    // check null argument...
    try {
        m.setLabelOffset(null);
        fail("Expected an IllegalArgumentException for null.");
    } catch (IllegalArgumentException e) {
        assertTrue(true);
    }
}
Also used : RectangleInsets(org.jfree.chart.api.RectangleInsets) Test(org.junit.jupiter.api.Test)

Example 14 with RectangleInsets

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

the class ThermometerPlotTest method testEquals.

/**
 * Some checks for the equals() method.
 */
@Test
public void testEquals() {
    ThermometerPlot p1 = new ThermometerPlot();
    ThermometerPlot p2 = new ThermometerPlot();
    assertEquals(p1, p2);
    assertEquals(p2, p1);
    // padding
    p1.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
    assertNotEquals(p1, p2);
    p2.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
    assertEquals(p2, p1);
    // thermometerStroke
    BasicStroke s = new BasicStroke(1.23f);
    p1.setThermometerStroke(s);
    assertNotEquals(p1, p2);
    p2.setThermometerStroke(s);
    assertEquals(p2, p1);
    // thermometerPaint
    p1.setThermometerPaint(new GradientPaint(1.0f, 2.0f, Color.BLUE, 3.0f, 4.0f, Color.RED));
    assertNotEquals(p1, p2);
    p2.setThermometerPaint(new GradientPaint(1.0f, 2.0f, Color.BLUE, 3.0f, 4.0f, Color.RED));
    assertEquals(p2, p1);
    // units
    p1.setUnits(ThermometerPlot.UNITS_KELVIN);
    assertNotEquals(p1, p2);
    p2.setUnits(ThermometerPlot.UNITS_KELVIN);
    assertEquals(p2, p1);
    // valueLocation
    p1.setValueLocation(ThermometerPlot.LEFT);
    assertNotEquals(p1, p2);
    p2.setValueLocation(ThermometerPlot.LEFT);
    assertEquals(p2, p1);
    // axisLocation
    p1.setAxisLocation(ThermometerPlot.RIGHT);
    assertNotEquals(p1, p2);
    p2.setAxisLocation(ThermometerPlot.RIGHT);
    assertEquals(p2, p1);
    // valueFont
    p1.setValueFont(new Font("Serif", Font.PLAIN, 9));
    assertNotEquals(p1, p2);
    p2.setValueFont(new Font("Serif", Font.PLAIN, 9));
    assertEquals(p2, p1);
    // valuePaint
    p1.setValuePaint(new GradientPaint(4.0f, 5.0f, Color.RED, 6.0f, 7.0f, Color.WHITE));
    assertNotEquals(p1, p2);
    p2.setValuePaint(new GradientPaint(4.0f, 5.0f, Color.RED, 6.0f, 7.0f, Color.WHITE));
    assertEquals(p2, p1);
    // valueFormat
    p1.setValueFormat(new DecimalFormat("0.0000"));
    assertNotEquals(p1, p2);
    p2.setValueFormat(new DecimalFormat("0.0000"));
    assertEquals(p2, p1);
    // mercuryPaint
    p1.setMercuryPaint(new GradientPaint(9.0f, 8.0f, Color.RED, 7.0f, 6.0f, Color.BLUE));
    assertNotEquals(p1, p2);
    p2.setMercuryPaint(new GradientPaint(9.0f, 8.0f, Color.RED, 7.0f, 6.0f, Color.BLUE));
    assertEquals(p2, p1);
    p1.setSubrange(1, 1.0, 2.0);
    assertNotEquals(p1, p2);
    p2.setSubrange(1, 1.0, 2.0);
    assertEquals(p2, p1);
    p1.setSubrangePaint(1, new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.YELLOW));
    assertNotEquals(p1, p2);
    p2.setSubrangePaint(1, new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.YELLOW));
    assertEquals(p2, p1);
    p1.setBulbRadius(9);
    assertNotEquals(p1, p2);
    p2.setBulbRadius(9);
    assertEquals(p2, p1);
    p1.setColumnRadius(8);
    assertNotEquals(p1, p2);
    p2.setColumnRadius(8);
    assertEquals(p2, p1);
    p1.setGap(7);
    assertNotEquals(p1, p2);
    p2.setGap(7);
    assertEquals(p2, p1);
}
Also used : BasicStroke(java.awt.BasicStroke) DecimalFormat(java.text.DecimalFormat) RectangleInsets(org.jfree.chart.api.RectangleInsets) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font) Test(org.junit.jupiter.api.Test)

Example 15 with RectangleInsets

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

the class FlowPlot method draw.

/**
 * Draws the flow plot within the specified area of the supplied graphics
 * target {@code g2}.
 *
 * @param g2  the graphics target ({@code null} not permitted).
 * @param area  the plot area ({@code null} not permitted).
 * @param anchor  the anchor point (ignored).
 * @param parentState  the parent state (ignored).
 * @param info  the plot rendering info.
 */
@Override
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
    Args.nullNotPermitted(g2, "g2");
    Args.nullNotPermitted(area, "area");
    EntityCollection entities = null;
    if (info != null) {
        info.setPlotArea(area);
        entities = info.getOwner().getEntityCollection();
    }
    RectangleInsets insets = getInsets();
    insets.trim(area);
    if (info != null) {
        info.setDataArea(area);
    }
    // use default JFreeChart background handling
    drawBackground(g2, area);
    // we need to ensure there is space to show all the inflows and all
    // the outflows at each node group, so first we calculate the max
    // flow space required - for each node in the group, consider the
    // maximum of the inflow and the outflow
    double flow2d = Double.POSITIVE_INFINITY;
    double nodeMargin2d = this.nodeMargin * area.getHeight();
    int stageCount = this.dataset.getStageCount();
    for (int stage = 0; stage < this.dataset.getStageCount(); stage++) {
        List<Comparable> sources = this.dataset.getSources(stage);
        int nodeCount = sources.size();
        double flowTotal = 0.0;
        for (Comparable source : sources) {
            double inflow = FlowDatasetUtils.calculateInflow(this.dataset, source, stage);
            double outflow = FlowDatasetUtils.calculateOutflow(this.dataset, source, stage);
            flowTotal = flowTotal + Math.max(inflow, outflow);
        }
        if (flowTotal > 0.0) {
            double availableH = area.getHeight() - (nodeCount - 1) * nodeMargin2d;
            flow2d = Math.min(availableH / flowTotal, flow2d);
        }
        if (stage == this.dataset.getStageCount() - 1) {
            // check inflows to the final destination nodes...
            List<Comparable> destinations = this.dataset.getDestinations(stage);
            int destinationCount = destinations.size();
            flowTotal = 0.0;
            for (Comparable destination : destinations) {
                double inflow = FlowDatasetUtils.calculateInflow(this.dataset, destination, stage + 1);
                flowTotal = flowTotal + inflow;
            }
            if (flowTotal > 0.0) {
                double availableH = area.getHeight() - (destinationCount - 1) * nodeMargin2d;
                flow2d = Math.min(availableH / flowTotal, flow2d);
            }
        }
    }
    double stageWidth = (area.getWidth() - ((stageCount + 1) * this.nodeWidth)) / stageCount;
    double flowOffset = area.getWidth() * this.flowMargin;
    Map<NodeKey, Rectangle2D> nodeRects = new HashMap<>();
    boolean hasNodeSelections = FlowDatasetUtils.hasNodeSelections(this.dataset);
    boolean hasFlowSelections = FlowDatasetUtils.hasFlowSelections(this.dataset);
    // in a final pass add the labels
    for (int stage = 0; stage < this.dataset.getStageCount(); stage++) {
        double stageLeft = area.getX() + (stage + 1) * this.nodeWidth + (stage * stageWidth);
        double stageRight = stageLeft + stageWidth;
        // calculate the source node and flow rectangles
        Map<FlowKey, Rectangle2D> sourceFlowRects = new HashMap<>();
        double nodeY = area.getY();
        for (Object s : this.dataset.getSources(stage)) {
            Comparable source = (Comparable) s;
            double inflow = FlowDatasetUtils.calculateInflow(dataset, source, stage);
            double outflow = FlowDatasetUtils.calculateOutflow(dataset, source, stage);
            double nodeHeight = (Math.max(inflow, outflow) * flow2d);
            Rectangle2D nodeRect = new Rectangle2D.Double(stageLeft - nodeWidth, nodeY, nodeWidth, nodeHeight);
            if (entities != null) {
                entities.add(new NodeEntity(new NodeKey<>(stage, source), nodeRect, source.toString()));
            }
            nodeRects.put(new NodeKey<>(stage, source), nodeRect);
            double y = nodeY;
            for (Object d : this.dataset.getDestinations(stage)) {
                Comparable destination = (Comparable) d;
                Number flow = this.dataset.getFlow(stage, source, destination);
                if (flow != null) {
                    double height = flow.doubleValue() * flow2d;
                    Rectangle2D rect = new Rectangle2D.Double(stageLeft - nodeWidth, y, nodeWidth, height);
                    sourceFlowRects.put(new FlowKey<>(stage, source, destination), rect);
                    y = y + height;
                }
            }
            nodeY = nodeY + nodeHeight + nodeMargin2d;
        }
        // calculate the destination rectangles
        Map<FlowKey, Rectangle2D> destFlowRects = new HashMap<>();
        nodeY = area.getY();
        for (Object d : this.dataset.getDestinations(stage)) {
            Comparable destination = (Comparable) d;
            double inflow = FlowDatasetUtils.calculateInflow(dataset, destination, stage + 1);
            double outflow = FlowDatasetUtils.calculateOutflow(dataset, destination, stage + 1);
            double nodeHeight = Math.max(inflow, outflow) * flow2d;
            nodeRects.put(new NodeKey<>(stage + 1, destination), new Rectangle2D.Double(stageRight, nodeY, nodeWidth, nodeHeight));
            double y = nodeY;
            for (Object s : this.dataset.getSources(stage)) {
                Comparable source = (Comparable) s;
                Number flow = this.dataset.getFlow(stage, source, destination);
                if (flow != null) {
                    double height = flow.doubleValue() * flow2d;
                    Rectangle2D rect = new Rectangle2D.Double(stageRight, y, nodeWidth, height);
                    y = y + height;
                    destFlowRects.put(new FlowKey<>(stage, source, destination), rect);
                }
            }
            nodeY = nodeY + nodeHeight + nodeMargin2d;
        }
        for (Object s : this.dataset.getSources(stage)) {
            Comparable source = (Comparable) s;
            NodeKey nodeKey = new NodeKey<>(stage, source);
            Rectangle2D nodeRect = nodeRects.get(nodeKey);
            Color ncol = lookupNodeColor(nodeKey);
            if (hasNodeSelections) {
                if (!Boolean.TRUE.equals(dataset.getNodeProperty(nodeKey, NodeKey.SELECTED_PROPERTY_KEY))) {
                    int g = (ncol.getRed() + ncol.getGreen() + ncol.getBlue()) / 3;
                    ncol = new Color(g, g, g, ncol.getAlpha());
                }
            }
            g2.setPaint(ncol);
            g2.fill(nodeRect);
            for (Object d : this.dataset.getDestinations(stage)) {
                Comparable destination = (Comparable) d;
                FlowKey flowKey = new FlowKey<>(stage, source, destination);
                Rectangle2D sourceRect = sourceFlowRects.get(flowKey);
                if (sourceRect == null) {
                    continue;
                }
                Rectangle2D destRect = destFlowRects.get(flowKey);
                Path2D connect = new Path2D.Double();
                connect.moveTo(sourceRect.getMaxX() + flowOffset, sourceRect.getMinY());
                connect.curveTo(stageLeft + stageWidth / 2.0, sourceRect.getMinY(), stageLeft + stageWidth / 2.0, destRect.getMinY(), destRect.getX() - flowOffset, destRect.getMinY());
                connect.lineTo(destRect.getX() - flowOffset, destRect.getMaxY());
                connect.curveTo(stageLeft + stageWidth / 2.0, destRect.getMaxY(), stageLeft + stageWidth / 2.0, sourceRect.getMaxY(), sourceRect.getMaxX() + flowOffset, sourceRect.getMaxY());
                connect.closePath();
                Color nc = lookupNodeColor(nodeKey);
                if (hasFlowSelections) {
                    if (!Boolean.TRUE.equals(dataset.getFlowProperty(flowKey, FlowKey.SELECTED_PROPERTY_KEY))) {
                        int g = (ncol.getRed() + ncol.getGreen() + ncol.getBlue()) / 3;
                        nc = new Color(g, g, g, ncol.getAlpha());
                    }
                }
                GradientPaint gp = new GradientPaint((float) sourceRect.getMaxX(), 0, nc, (float) destRect.getMinX(), 0, new Color(nc.getRed(), nc.getGreen(), nc.getBlue(), 128));
                Composite saved = g2.getComposite();
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
                g2.setPaint(gp);
                g2.fill(connect);
                if (entities != null) {
                    String toolTip = null;
                    if (this.toolTipGenerator != null) {
                        toolTip = this.toolTipGenerator.generateLabel(this.dataset, flowKey);
                    }
                    entities.add(new FlowEntity(flowKey, connect, toolTip, ""));
                }
                g2.setComposite(saved);
            }
        }
    }
    // now draw the destination nodes
    int lastStage = this.dataset.getStageCount() - 1;
    for (Object d : this.dataset.getDestinations(lastStage)) {
        Comparable destination = (Comparable) d;
        NodeKey nodeKey = new NodeKey<>(lastStage + 1, destination);
        Rectangle2D nodeRect = nodeRects.get(nodeKey);
        if (nodeRect != null) {
            Color ncol = lookupNodeColor(nodeKey);
            if (hasNodeSelections) {
                if (!Boolean.TRUE.equals(dataset.getNodeProperty(nodeKey, NodeKey.SELECTED_PROPERTY_KEY))) {
                    int g = (ncol.getRed() + ncol.getGreen() + ncol.getBlue()) / 3;
                    ncol = new Color(g, g, g, ncol.getAlpha());
                }
            }
            g2.setPaint(ncol);
            g2.fill(nodeRect);
            if (entities != null) {
                entities.add(new NodeEntity(new NodeKey<>(lastStage + 1, destination), nodeRect, destination.toString()));
            }
        }
    }
    // now draw all the labels over top of everything else
    g2.setFont(this.defaultNodeLabelFont);
    g2.setPaint(this.defaultNodeLabelPaint);
    for (NodeKey key : nodeRects.keySet()) {
        Rectangle2D r = nodeRects.get(key);
        if (key.getStage() < this.dataset.getStageCount()) {
            TextUtils.drawAlignedString(key.getNode().toString(), g2, (float) (r.getMaxX() + flowOffset + this.nodeLabelOffsetX), (float) labelY(r), TextAnchor.CENTER_LEFT);
        } else {
            TextUtils.drawAlignedString(key.getNode().toString(), g2, (float) (r.getX() - flowOffset - this.nodeLabelOffsetX), (float) labelY(r), TextAnchor.CENTER_RIGHT);
        }
    }
}
Also used : HashMap(java.util.HashMap) Path2D(java.awt.geom.Path2D) GradientPaint(java.awt.GradientPaint) FlowEntity(org.jfree.chart.entity.FlowEntity) NodeKey(org.jfree.data.flow.NodeKey) FlowKey(org.jfree.data.flow.FlowKey) AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) EntityCollection(org.jfree.chart.entity.EntityCollection) RectangleInsets(org.jfree.chart.api.RectangleInsets) NodeEntity(org.jfree.chart.entity.NodeEntity)

Aggregations

RectangleInsets (org.jfree.chart.api.RectangleInsets)58 Rectangle2D (java.awt.geom.Rectangle2D)19 Test (org.junit.jupiter.api.Test)17 Font (java.awt.Font)16 BasicStroke (java.awt.BasicStroke)14 GradientPaint (java.awt.GradientPaint)13 Paint (java.awt.Paint)9 Shape (java.awt.Shape)9 AxisState (org.jfree.chart.axis.AxisState)7 FontMetrics (java.awt.FontMetrics)6 Stroke (java.awt.Stroke)6 FontRenderContext (java.awt.font.FontRenderContext)6 LineMetrics (java.awt.font.LineMetrics)6 RectangleEdge (org.jfree.chart.api.RectangleEdge)6 AxisSpace (org.jfree.chart.axis.AxisSpace)6 ValueAxis (org.jfree.chart.axis.ValueAxis)6 StandardPieSectionLabelGenerator (org.jfree.chart.labels.StandardPieSectionLabelGenerator)6 StandardPieToolTipGenerator (org.jfree.chart.labels.StandardPieToolTipGenerator)6 AlphaComposite (java.awt.AlphaComposite)5 Composite (java.awt.Composite)5