Search in sources :

Example 41 with RectangleInsets

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

the class ChartFactory method createRingChart.

/**
 * Creates a ring chart with default settings.
 * <P>
 * The chart object returned by this method uses a {@link RingPlot}
 * instance as the plot.
 *
 * @param title  the chart title ({@code null} permitted).
 * @param dataset  the dataset for the chart ({@code null} permitted).
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param locale  the locale ({@code null} not permitted).
 *
 * @return A ring chart.
 */
public static JFreeChart createRingChart(String title, PieDataset dataset, boolean legend, boolean tooltips, Locale locale) {
    RingPlot plot = new RingPlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    currentTheme.apply(chart);
    return chart;
}
Also used : RingPlot(org.jfree.chart.plot.RingPlot) StandardPieToolTipGenerator(org.jfree.chart.labels.StandardPieToolTipGenerator) RectangleInsets(org.jfree.chart.api.RectangleInsets) StandardPieSectionLabelGenerator(org.jfree.chart.labels.StandardPieSectionLabelGenerator)

Example 42 with RectangleInsets

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

the class ChartFactory method createPieChart.

/**
 * Creates a pie chart with default settings that compares 2 datasets.
 * The colour of each section will be determined by the move from the value
 * for the same key in {@code previousDataset}. ie if value1 &gt;
 * value2 then the section will be in green (unless
 * {@code greenForIncrease} is {@code false}, in which case it
 * would be {@code red}). Each section can have a shade of red or
 * green as the difference can be tailored between 0% (black) and
 * percentDiffForMaxScale% (bright red/green).
 * <p>
 * For instance if {@code percentDiffForMaxScale} is 10 (10%), a
 * difference of 5% will have a half shade of red/green, a difference of
 * 10% or more will have a maximum shade/brightness of red/green.
 * <P>
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 * <p>
 * Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
 * Xhenseval</a>.
 *
 * @param title  the chart title ({@code null} permitted).
 * @param dataset  the dataset for the chart ({@code null} permitted).
 * @param previousDataset  the dataset for the last run, this will be used
 *                         to compare each key in the dataset
 * @param percentDiffForMaxScale scale goes from bright red/green to black,
 *                               percentDiffForMaxScale indicate the change
 *                               required to reach top scale.
 * @param greenForIncrease  an increase since previousDataset will be
 *                          displayed in green (decrease red) if true.
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param locale  the locale ({@code null} not permitted).
 * @param subTitle displays a subtitle with colour scheme if true
 * @param showDifference  create a new dataset that will show the %
 *                        difference between the two datasets.
 *
 * @return A pie chart.
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset, int percentDiffForMaxScale, boolean greenForIncrease, boolean legend, boolean tooltips, Locale locale, boolean subTitle, boolean showDifference) {
    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
    }
    List keys = dataset.getKeys();
    DefaultPieDataset series = null;
    if (showDifference) {
        series = new DefaultPieDataset();
    }
    double colorPerPercent = 255.0 / percentDiffForMaxScale;
    for (Iterator it = keys.iterator(); it.hasNext(); ) {
        Comparable key = (Comparable) it.next();
        Number newValue = dataset.getValue(key);
        Number oldValue = previousDataset.getValue(key);
        if (oldValue == null) {
            if (greenForIncrease) {
                plot.setSectionPaint(key, Color.GREEN);
            } else {
                plot.setSectionPaint(key, Color.RED);
            }
            if (showDifference) {
                // suppresses compiler warning
                assert series != null;
                series.setValue(key + " (+100%)", newValue);
            }
        } else {
            double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
            double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255 : Math.abs(percentChange) * colorPerPercent);
            if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue() || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
                plot.setSectionPaint(key, new Color(0, (int) shade, 0));
            } else {
                plot.setSectionPaint(key, new Color((int) shade, 0, 0));
            }
            if (showDifference) {
                // suppresses compiler warning
                assert series != null;
                series.setValue(key + " (" + (percentChange >= 0 ? "+" : "") + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")", newValue);
            }
        }
    }
    if (showDifference) {
        plot.setDataset(series);
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    if (subTitle) {
        TextTitle subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-" + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+" + percentDiffForMaxScale + "%", new Font("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}
Also used : DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) Color(java.awt.Color) StandardPieSectionLabelGenerator(org.jfree.chart.labels.StandardPieSectionLabelGenerator) Font(java.awt.Font) TextTitle(org.jfree.chart.title.TextTitle) StandardPieToolTipGenerator(org.jfree.chart.labels.StandardPieToolTipGenerator) Iterator(java.util.Iterator) MultiplePiePlot(org.jfree.chart.plot.pie.MultiplePiePlot) PiePlot(org.jfree.chart.plot.pie.PiePlot) RectangleInsets(org.jfree.chart.api.RectangleInsets) List(java.util.List)

Example 43 with RectangleInsets

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

the class Axis method drawLabel.

/**
 * Draws the axis label.
 *
 * @param label  the label text.
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param dataArea  the area inside the axes.
 * @param edge  the location of the axis.
 * @param state  the axis state ({@code null} not permitted).
 *
 * @return Information about the axis.
 */
protected AxisState drawLabel(String label, Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state) {
    // it is unlikely that 'state' will be null, but check anyway...
    Args.nullNotPermitted(state, "state");
    if ((label == null) || (label.equals(""))) {
        return state;
    }
    Font font = getLabelFont();
    RectangleInsets insets = getLabelInsets();
    g2.setFont(font);
    g2.setPaint(getLabelPaint());
    FontMetrics fm = g2.getFontMetrics();
    Rectangle2D labelBounds = TextUtils.getTextBounds(label, g2, fm);
    if (edge == RectangleEdge.TOP) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() - insets.getBottom() - labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorUp(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.BOTTOM) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() + insets.getTop() + labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorDown(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.LEFT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() - Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() - insets.getRight() - labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() - Math.PI / 2.0, anchor);
        state.cursorLeft(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    } else if (edge == RectangleEdge.RIGHT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() + Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() + insets.getLeft() + labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() + Math.PI / 2.0, anchor);
        state.cursorRight(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    }
    return state;
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) Shape(java.awt.Shape) FontMetrics(java.awt.FontMetrics) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.chart.api.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) Font(java.awt.Font)

Example 44 with RectangleInsets

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

the class Axis method drawAttributedLabel.

/**
 * Draws the axis label.
 *
 * @param label  the label text.
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param dataArea  the area inside the axes.
 * @param edge  the location of the axis.
 * @param state  the axis state ({@code null} not permitted).
 *
 * @return Information about the axis.
 */
protected AxisState drawAttributedLabel(AttributedString label, Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state) {
    // it is unlikely that 'state' will be null, but check anyway...
    Args.nullNotPermitted(state, "state");
    if (label == null) {
        return state;
    }
    RectangleInsets insets = getLabelInsets();
    g2.setFont(getLabelFont());
    g2.setPaint(getLabelPaint());
    TextLayout layout = new TextLayout(this.attributedLabel.getIterator(), g2.getFontRenderContext());
    Rectangle2D labelBounds = layout.getBounds();
    if (edge == RectangleEdge.TOP) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() - insets.getBottom() - labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorUp(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.BOTTOM) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() + insets.getTop() + labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorDown(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.LEFT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() - Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() - insets.getRight() - labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() - Math.PI / 2.0, anchor);
        state.cursorLeft(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    } else if (edge == RectangleEdge.RIGHT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() + Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() + insets.getLeft() + labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() + Math.PI / 2.0, anchor);
        state.cursorRight(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    }
    return state;
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) Shape(java.awt.Shape) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.chart.api.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) TextLayout(java.awt.font.TextLayout)

Example 45 with RectangleInsets

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

the class BlockBorderTest method testSerialization.

/**
 * Serialize an instance, restore it, and check for equality.
 */
@Test
public void testSerialization() {
    BlockBorder b1 = new BlockBorder(new RectangleInsets(1.0, 2.0, 3.0, 4.0), new GradientPaint(1.0f, 2.0f, Color.RED, 3.0f, 4.0f, Color.YELLOW));
    BlockBorder b2 = TestUtils.serialised(b1);
    assertEquals(b1, b2);
}
Also used : RectangleInsets(org.jfree.chart.api.RectangleInsets) GradientPaint(java.awt.GradientPaint) Test(org.junit.jupiter.api.Test)

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