Search in sources :

Example 56 with RectangleInsets

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

the class XYPlot method draw.

/**
 * Draws the plot within the specified area on a graphics device.
 *
 * @param g2  the graphics device.
 * @param area  the plot area (in Java2D space).
 * @param anchor  an anchor point in Java2D space ({@code null}
 *                permitted).
 * @param parentState  the state from the parent plot, if there is one
 *                     ({@code null} permitted).
 * @param info  collects chart drawing information ({@code null}
 *              permitted).
 */
@Override
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
    // if the plot area is too small, just return...
    boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
    boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
    if (b1 || b2) {
        return;
    }
    // record the plot area...
    if (info != null) {
        info.setPlotArea(area);
    }
    // adjust the drawing area for the plot insets (if any)...
    RectangleInsets insets = getInsets();
    insets.trim(area);
    AxisSpace space = calculateAxisSpace(g2, area);
    Rectangle2D dataArea = space.shrink(area, null);
    this.axisOffset.trim(dataArea);
    dataArea = integerise(dataArea);
    if (dataArea.isEmpty()) {
        return;
    }
    createAndAddEntity((Rectangle2D) dataArea.clone(), info, null, null);
    if (info != null) {
        info.setDataArea(dataArea);
    }
    // draw the plot background and axes...
    drawBackground(g2, dataArea);
    Map<Axis, AxisState> axisStateMap = drawAxes(g2, area, dataArea, info);
    PlotOrientation orient = getOrientation();
    // clicked - the crosshairs will be driven off this point...
    if (anchor != null && !dataArea.contains(anchor)) {
        anchor = null;
    }
    CrosshairState crosshairState = new CrosshairState();
    crosshairState.setCrosshairDistance(Double.POSITIVE_INFINITY);
    crosshairState.setAnchor(anchor);
    crosshairState.setAnchorX(Double.NaN);
    crosshairState.setAnchorY(Double.NaN);
    if (anchor != null) {
        ValueAxis domainAxis = getDomainAxis();
        if (domainAxis != null) {
            double x;
            if (orient == PlotOrientation.VERTICAL) {
                x = domainAxis.java2DToValue(anchor.getX(), dataArea, getDomainAxisEdge());
            } else {
                x = domainAxis.java2DToValue(anchor.getY(), dataArea, getDomainAxisEdge());
            }
            crosshairState.setAnchorX(x);
        }
        ValueAxis rangeAxis = getRangeAxis();
        if (rangeAxis != null) {
            double y;
            if (orient == PlotOrientation.VERTICAL) {
                y = rangeAxis.java2DToValue(anchor.getY(), dataArea, getRangeAxisEdge());
            } else {
                y = rangeAxis.java2DToValue(anchor.getX(), dataArea, getRangeAxisEdge());
            }
            crosshairState.setAnchorY(y);
        }
    }
    crosshairState.setCrosshairX(getDomainCrosshairValue());
    crosshairState.setCrosshairY(getRangeCrosshairValue());
    Shape originalClip = g2.getClip();
    Composite originalComposite = g2.getComposite();
    g2.clip(dataArea);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
    AxisState domainAxisState = axisStateMap.get(getDomainAxis());
    if (domainAxisState == null) {
        if (parentState != null) {
            domainAxisState = parentState.getSharedAxisStates().get(getDomainAxis());
        }
    }
    AxisState rangeAxisState = axisStateMap.get(getRangeAxis());
    if (rangeAxisState == null) {
        if (parentState != null) {
            rangeAxisState = parentState.getSharedAxisStates().get(getRangeAxis());
        }
    }
    if (domainAxisState != null) {
        drawDomainTickBands(g2, dataArea, domainAxisState.getTicks());
    }
    if (rangeAxisState != null) {
        drawRangeTickBands(g2, dataArea, rangeAxisState.getTicks());
    }
    if (domainAxisState != null) {
        drawDomainGridlines(g2, dataArea, domainAxisState.getTicks());
        drawZeroDomainBaseline(g2, dataArea);
    }
    if (rangeAxisState != null) {
        drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks());
        drawZeroRangeBaseline(g2, dataArea);
    }
    Graphics2D savedG2 = g2;
    BufferedImage dataImage = null;
    boolean suppressShadow = Boolean.TRUE.equals(g2.getRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION));
    if (this.shadowGenerator != null && !suppressShadow) {
        dataImage = new BufferedImage((int) dataArea.getWidth(), (int) dataArea.getHeight(), BufferedImage.TYPE_INT_ARGB);
        g2 = dataImage.createGraphics();
        g2.translate(-dataArea.getX(), -dataArea.getY());
        g2.setRenderingHints(savedG2.getRenderingHints());
    }
    // draw the markers that are associated with a specific dataset...
    for (XYDataset<S> dataset : this.datasets.values()) {
        int datasetIndex = indexOf(dataset);
        drawDomainMarkers(g2, dataArea, datasetIndex, Layer.BACKGROUND);
    }
    for (XYDataset<S> dataset : this.datasets.values()) {
        int datasetIndex = indexOf(dataset);
        drawRangeMarkers(g2, dataArea, datasetIndex, Layer.BACKGROUND);
    }
    // now draw annotations and render data items...
    boolean foundData = false;
    DatasetRenderingOrder order = getDatasetRenderingOrder();
    List<Integer> rendererIndices = getRendererIndices(order);
    List<Integer> datasetIndices = getDatasetIndices(order);
    // draw background annotations
    for (int i : rendererIndices) {
        XYItemRenderer renderer = getRenderer(i);
        if (renderer != null) {
            ValueAxis domainAxis = getDomainAxisForDataset(i);
            ValueAxis rangeAxis = getRangeAxisForDataset(i);
            renderer.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info);
        }
    }
    // render data items...
    for (int datasetIndex : datasetIndices) {
        foundData = render(g2, dataArea, datasetIndex, info, crosshairState) || foundData;
    }
    // draw foreground annotations
    for (int i : rendererIndices) {
        XYItemRenderer renderer = getRenderer(i);
        if (renderer != null) {
            ValueAxis domainAxis = getDomainAxisForDataset(i);
            ValueAxis rangeAxis = getRangeAxisForDataset(i);
            renderer.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info);
        }
    }
    // draw domain crosshair if required...
    int datasetIndex = crosshairState.getDatasetIndex();
    ValueAxis xAxis = getDomainAxisForDataset(datasetIndex);
    RectangleEdge xAxisEdge = getDomainAxisEdge(getDomainAxisIndex(xAxis));
    if (!this.domainCrosshairLockedOnData && anchor != null) {
        double xx;
        if (orient == PlotOrientation.VERTICAL) {
            xx = xAxis.java2DToValue(anchor.getX(), dataArea, xAxisEdge);
        } else {
            xx = xAxis.java2DToValue(anchor.getY(), dataArea, xAxisEdge);
        }
        crosshairState.setCrosshairX(xx);
    }
    setDomainCrosshairValue(crosshairState.getCrosshairX(), false);
    if (isDomainCrosshairVisible()) {
        double x = getDomainCrosshairValue();
        Paint paint = getDomainCrosshairPaint();
        Stroke stroke = getDomainCrosshairStroke();
        drawDomainCrosshair(g2, dataArea, orient, x, xAxis, stroke, paint);
    }
    // draw range crosshair if required...
    ValueAxis yAxis = getRangeAxisForDataset(datasetIndex);
    RectangleEdge yAxisEdge = getRangeAxisEdge(getRangeAxisIndex(yAxis));
    if (!this.rangeCrosshairLockedOnData && anchor != null) {
        double yy;
        if (orient == PlotOrientation.VERTICAL) {
            yy = yAxis.java2DToValue(anchor.getY(), dataArea, yAxisEdge);
        } else {
            yy = yAxis.java2DToValue(anchor.getX(), dataArea, yAxisEdge);
        }
        crosshairState.setCrosshairY(yy);
    }
    setRangeCrosshairValue(crosshairState.getCrosshairY(), false);
    if (isRangeCrosshairVisible()) {
        double y = getRangeCrosshairValue();
        Paint paint = getRangeCrosshairPaint();
        Stroke stroke = getRangeCrosshairStroke();
        drawRangeCrosshair(g2, dataArea, orient, y, yAxis, stroke, paint);
    }
    if (!foundData) {
        drawNoDataMessage(g2, dataArea);
    }
    for (int i : rendererIndices) {
        drawDomainMarkers(g2, dataArea, i, Layer.FOREGROUND);
    }
    for (int i : rendererIndices) {
        drawRangeMarkers(g2, dataArea, i, Layer.FOREGROUND);
    }
    drawAnnotations(g2, dataArea, info);
    if (this.shadowGenerator != null && !suppressShadow) {
        BufferedImage shadowImage = this.shadowGenerator.createDropShadow(dataImage);
        g2 = savedG2;
        g2.drawImage(shadowImage, (int) dataArea.getX() + this.shadowGenerator.calculateOffsetX(), (int) dataArea.getY() + this.shadowGenerator.calculateOffsetY(), null);
        g2.drawImage(dataImage, (int) dataArea.getX(), (int) dataArea.getY(), null);
    }
    g2.setClip(originalClip);
    g2.setComposite(originalComposite);
    drawOutline(g2, dataArea);
}
Also used : BufferedImage(java.awt.image.BufferedImage) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.chart.api.RectangleInsets) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Example 57 with RectangleInsets

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

the class MultiplePiePlot method draw.

/**
 * Draws the plot on a Java 2D graphics device (such as the screen or a
 * printer).
 *
 * @param g2  the graphics device.
 * @param area  the area within which the plot should be drawn.
 * @param anchor  the anchor point ({@code null} permitted).
 * @param parentState  the state from the parent plot, if there is one.
 * @param info  collects info about the drawing.
 */
@Override
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
    // adjust the drawing area for the plot insets (if any)...
    RectangleInsets insets = getInsets();
    insets.trim(area);
    drawBackground(g2, area);
    drawOutline(g2, area);
    // check that there is some data to display...
    if (DatasetUtils.isEmptyOrNull(this.dataset)) {
        drawNoDataMessage(g2, area);
        return;
    }
    int pieCount;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        pieCount = this.dataset.getRowCount();
    } else {
        pieCount = this.dataset.getColumnCount();
    }
    // the columns variable is always >= rows
    int displayCols = (int) Math.ceil(Math.sqrt(pieCount));
    int displayRows = (int) Math.ceil((double) pieCount / (double) displayCols);
    // swap rows and columns to match plotArea shape
    if (displayCols > displayRows && area.getWidth() < area.getHeight()) {
        int temp = displayCols;
        displayCols = displayRows;
        displayRows = temp;
    }
    prefetchSectionPaints();
    int x = (int) area.getX();
    int y = (int) area.getY();
    int width = ((int) area.getWidth()) / displayCols;
    int height = ((int) area.getHeight()) / displayRows;
    int row = 0;
    int column = 0;
    int diff = (displayRows * displayCols) - pieCount;
    int xoffset = 0;
    Rectangle rect = new Rectangle();
    for (int pieIndex = 0; pieIndex < pieCount; pieIndex++) {
        rect.setBounds(x + xoffset + (width * column), y + (height * row), width, height);
        String title;
        if (this.dataExtractOrder == TableOrder.BY_ROW) {
            title = this.dataset.getRowKey(pieIndex).toString();
        } else {
            title = this.dataset.getColumnKey(pieIndex).toString();
        }
        this.pieChart.setTitle(title);
        PieDataset piedataset;
        PieDataset dd = new CategoryToPieDataset(this.dataset, this.dataExtractOrder, pieIndex);
        if (this.limit > 0.0) {
            piedataset = DatasetUtils.createConsolidatedPieDataset(dd, this.aggregatedItemsKey, this.limit);
        } else {
            piedataset = dd;
        }
        PiePlot piePlot = (PiePlot) this.pieChart.getPlot();
        piePlot.setDataset(piedataset);
        piePlot.setPieIndex(pieIndex);
        // update the section colors to match the global colors...
        for (int i = 0; i < piedataset.getItemCount(); i++) {
            Comparable key = piedataset.getKey(i);
            Paint p;
            if (key.equals(this.aggregatedItemsKey)) {
                p = this.aggregatedItemsPaint;
            } else {
                p = (Paint) this.sectionPaints.get(key);
            }
            piePlot.setSectionPaint(key, p);
        }
        ChartRenderingInfo subinfo = null;
        if (info != null) {
            subinfo = new ChartRenderingInfo();
        }
        this.pieChart.draw(g2, rect, subinfo);
        if (info != null) {
            assert subinfo != null;
            info.getOwner().getEntityCollection().addAll(subinfo.getEntityCollection());
            info.addSubplotInfo(subinfo.getPlotInfo());
        }
        ++column;
        if (column == displayCols) {
            column = 0;
            ++row;
            if (row == displayRows - 1 && diff != 0) {
                xoffset = (diff * width) / 2;
            }
        }
    }
}
Also used : CategoryToPieDataset(org.jfree.data.category.CategoryToPieDataset) PieDataset(org.jfree.data.general.PieDataset) Rectangle(java.awt.Rectangle) ChartRenderingInfo(org.jfree.chart.ChartRenderingInfo) RectangleInsets(org.jfree.chart.api.RectangleInsets) CategoryToPieDataset(org.jfree.data.category.CategoryToPieDataset) Paint(java.awt.Paint) Paint(java.awt.Paint)

Example 58 with RectangleInsets

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

the class PiePlot method draw.

/**
 * Draws the plot on a Java 2D graphics device (such as the screen or a
 * printer).
 *
 * @param g2  the graphics device.
 * @param area  the area within which the plot should be drawn.
 * @param anchor  the anchor point ({@code null} permitted).
 * @param parentState  the state from the parent plot, if there is one.
 * @param info  collects info about the drawing
 *              ({@code null} permitted).
 */
@Override
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
    // adjust for insets...
    RectangleInsets insets = getInsets();
    insets.trim(area);
    if (info != null) {
        info.setPlotArea(area);
        info.setDataArea(area);
    }
    drawBackground(g2, area);
    drawOutline(g2, area);
    Shape savedClip = g2.getClip();
    g2.clip(area);
    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
    if (!DatasetUtils.isEmptyOrNull(this.dataset)) {
        Graphics2D savedG2 = g2;
        boolean suppressShadow = Boolean.TRUE.equals(g2.getRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION));
        BufferedImage dataImage = null;
        if (this.shadowGenerator != null && !suppressShadow) {
            dataImage = new BufferedImage((int) area.getWidth(), (int) area.getHeight(), BufferedImage.TYPE_INT_ARGB);
            g2 = dataImage.createGraphics();
            g2.translate(-area.getX(), -area.getY());
            g2.setRenderingHints(savedG2.getRenderingHints());
        }
        drawPie(g2, area, info);
        if (this.shadowGenerator != null && !suppressShadow) {
            BufferedImage shadowImage = this.shadowGenerator.createDropShadow(dataImage);
            g2 = savedG2;
            g2.drawImage(shadowImage, (int) area.getX() + this.shadowGenerator.calculateOffsetX(), (int) area.getY() + this.shadowGenerator.calculateOffsetY(), null);
            g2.drawImage(dataImage, (int) area.getX(), (int) area.getY(), null);
        }
    } else {
        drawNoDataMessage(g2, area);
    }
    g2.setClip(savedClip);
    g2.setComposite(originalComposite);
    drawOutline(g2, area);
}
Also used : RectangleInsets(org.jfree.chart.api.RectangleInsets) BufferedImage(java.awt.image.BufferedImage)

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