Search in sources :

Example 86 with RectangleEdge

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

the class StackedXYBarRenderer method drawItem.

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the area within which the plot is being drawn.
 * @param info  collects information about the drawing.
 * @param plot  the plot (can be used to obtain standard color information
 *              etc).
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param crosshairState  crosshair information for the plot
 *                        ({@code null} permitted).
 * @param pass  the pass index.
 */
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) {
    if (!getItemVisible(series, item)) {
        return;
    }
    if (!(dataset instanceof IntervalXYDataset && dataset instanceof TableXYDataset)) {
        String message = "dataset (type " + dataset.getClass().getName() + ") has wrong type:";
        boolean and = false;
        if (!IntervalXYDataset.class.isAssignableFrom(dataset.getClass())) {
            message += " it is no IntervalXYDataset";
            and = true;
        }
        if (!TableXYDataset.class.isAssignableFrom(dataset.getClass())) {
            if (and) {
                message += " and";
            }
            message += " it is no TableXYDataset";
        }
        throw new IllegalArgumentException(message);
    }
    IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
    double value = intervalDataset.getYValue(series, item);
    if (Double.isNaN(value)) {
        return;
    }
    // if we are rendering the values as percentages, we need to calculate
    // the total for the current item.  Unfortunately here we end up
    // repeating the calculation more times than is strictly necessary -
    // hopefully I'll come back to this and find a way to add the
    // total(s) to the renderer state.  The other problem is we implicitly
    // assume the dataset has no negative values...perhaps that can be
    // fixed too.
    double total = 0.0;
    if (this.renderAsPercentages) {
        total = DatasetUtils.calculateStackTotal((TableXYDataset) dataset, item);
        value = value / total;
    }
    double positiveBase = 0.0;
    double negativeBase = 0.0;
    for (int i = 0; i < series; i++) {
        double v = dataset.getYValue(i, item);
        if (!Double.isNaN(v) && isSeriesVisible(i)) {
            if (this.renderAsPercentages) {
                v = v / total;
            }
            if (v > 0) {
                positiveBase = positiveBase + v;
            } else {
                negativeBase = negativeBase + v;
            }
        }
    }
    double translatedBase;
    double translatedValue;
    RectangleEdge edgeR = plot.getRangeAxisEdge();
    if (value > 0.0) {
        translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, edgeR);
        translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, edgeR);
    } else {
        translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, edgeR);
        translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, edgeR);
    }
    RectangleEdge edgeD = plot.getDomainAxisEdge();
    double startX = intervalDataset.getStartXValue(series, item);
    if (Double.isNaN(startX)) {
        return;
    }
    double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, edgeD);
    double endX = intervalDataset.getEndXValue(series, item);
    if (Double.isNaN(endX)) {
        return;
    }
    double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, edgeD);
    double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
    double translatedHeight = Math.abs(translatedValue - translatedBase);
    if (getMargin() > 0.0) {
        double cut = translatedWidth * getMargin();
        translatedWidth = translatedWidth - cut;
        translatedStartX = translatedStartX + cut / 2;
    }
    Rectangle2D bar = null;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(Math.min(translatedBase, translatedValue), Math.min(translatedEndX, translatedStartX), translatedHeight, translatedWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        bar = new Rectangle2D.Double(Math.min(translatedStartX, translatedEndX), Math.min(translatedBase, translatedValue), translatedWidth, translatedHeight);
    } else {
        throw new IllegalStateException();
    }
    boolean positive = (value > 0.0);
    boolean inverted = rangeAxis.isInverted();
    RectangleEdge barBase;
    if (orientation == PlotOrientation.HORIZONTAL) {
        if (positive && inverted || !positive && !inverted) {
            barBase = RectangleEdge.RIGHT;
        } else {
            barBase = RectangleEdge.LEFT;
        }
    } else {
        if (positive && !inverted || !positive && inverted) {
            barBase = RectangleEdge.BOTTOM;
        } else {
            barBase = RectangleEdge.TOP;
        }
    }
    if (pass == 0) {
        if (getShadowsVisible()) {
            getBarPainter().paintBarShadow(g2, this, series, item, bar, barBase, false);
        }
    } else if (pass == 1) {
        getBarPainter().paintBar(g2, this, series, item, bar, barBase);
        // add an entity for the item...
        if (info != null) {
            EntityCollection entities = info.getOwner().getEntityCollection();
            if (entities != null) {
                addEntity(entities, bar, dataset, series, item, bar.getCenterX(), bar.getCenterY());
            }
        }
    } else if (pass == 2) {
        // been drawn...
        if (isItemLabelVisible(series, item)) {
            XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
            drawItemLabel(g2, dataset, series, item, plot, generator, bar, value < 0.0);
        }
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) TableXYDataset(org.jfree.data.xy.TableXYDataset) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) Rectangle2D(java.awt.geom.Rectangle2D) EntityCollection(org.jfree.chart.entity.EntityCollection) XYItemLabelGenerator(org.jfree.chart.labels.XYItemLabelGenerator) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Example 87 with RectangleEdge

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

the class XYErrorRenderer method drawItem.

/**
 * Draws the visual representation for one data item.
 *
 * @param g2  the graphics output target.
 * @param state  the renderer state.
 * @param dataArea  the data area.
 * @param info  the plot rendering info.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param crosshairState  the crosshair state.
 * @param pass  the pass index.
 */
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) {
    if (pass == 0 && dataset instanceof IntervalXYDataset && getItemVisible(series, item)) {
        IntervalXYDataset ixyd = (IntervalXYDataset) dataset;
        PlotOrientation orientation = plot.getOrientation();
        if (this.drawXError) {
            // draw the error bar for the x-interval
            double x0 = ixyd.getStartXValue(series, item);
            double x1 = ixyd.getEndXValue(series, item);
            double y = ixyd.getYValue(series, item);
            RectangleEdge edge = plot.getDomainAxisEdge();
            double xx0 = domainAxis.valueToJava2D(x0, dataArea, edge);
            double xx1 = domainAxis.valueToJava2D(x1, dataArea, edge);
            double yy = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge());
            Line2D line;
            Line2D cap1;
            Line2D cap2;
            double adj = this.capLength / 2.0;
            if (orientation == PlotOrientation.VERTICAL) {
                line = new Line2D.Double(xx0, yy, xx1, yy);
                cap1 = new Line2D.Double(xx0, yy - adj, xx0, yy + adj);
                cap2 = new Line2D.Double(xx1, yy - adj, xx1, yy + adj);
            } else {
                // PlotOrientation.HORIZONTAL
                line = new Line2D.Double(yy, xx0, yy, xx1);
                cap1 = new Line2D.Double(yy - adj, xx0, yy + adj, xx0);
                cap2 = new Line2D.Double(yy - adj, xx1, yy + adj, xx1);
            }
            if (this.errorPaint != null) {
                g2.setPaint(this.errorPaint);
            } else {
                g2.setPaint(getItemPaint(series, item));
            }
            if (this.errorStroke != null) {
                g2.setStroke(this.errorStroke);
            } else {
                g2.setStroke(getItemStroke(series, item));
            }
            g2.draw(line);
            g2.draw(cap1);
            g2.draw(cap2);
        }
        if (this.drawYError) {
            // draw the error bar for the y-interval
            double y0 = ixyd.getStartYValue(series, item);
            double y1 = ixyd.getEndYValue(series, item);
            double x = ixyd.getXValue(series, item);
            RectangleEdge edge = plot.getRangeAxisEdge();
            double yy0 = rangeAxis.valueToJava2D(y0, dataArea, edge);
            double yy1 = rangeAxis.valueToJava2D(y1, dataArea, edge);
            double xx = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
            Line2D line;
            Line2D cap1;
            Line2D cap2;
            double adj = this.capLength / 2.0;
            if (orientation == PlotOrientation.VERTICAL) {
                line = new Line2D.Double(xx, yy0, xx, yy1);
                cap1 = new Line2D.Double(xx - adj, yy0, xx + adj, yy0);
                cap2 = new Line2D.Double(xx - adj, yy1, xx + adj, yy1);
            } else {
                // PlotOrientation.HORIZONTAL
                line = new Line2D.Double(yy0, xx, yy1, xx);
                cap1 = new Line2D.Double(yy0, xx - adj, yy0, xx + adj);
                cap2 = new Line2D.Double(yy1, xx - adj, yy1, xx + adj);
            }
            if (this.errorPaint != null) {
                g2.setPaint(this.errorPaint);
            } else {
                g2.setPaint(getItemPaint(series, item));
            }
            if (this.errorStroke != null) {
                g2.setStroke(this.errorStroke);
            } else {
                g2.setStroke(getItemStroke(series, item));
            }
            g2.draw(line);
            g2.draw(cap1);
            g2.draw(cap2);
        }
    }
    super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) Line2D(java.awt.geom.Line2D) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Aggregations

RectangleEdge (org.jfree.chart.api.RectangleEdge)87 Rectangle2D (java.awt.geom.Rectangle2D)47 PlotOrientation (org.jfree.chart.plot.PlotOrientation)44 Paint (java.awt.Paint)40 EntityCollection (org.jfree.chart.entity.EntityCollection)33 Stroke (java.awt.Stroke)18 Line2D (java.awt.geom.Line2D)17 Shape (java.awt.Shape)16 AxisSpace (org.jfree.chart.axis.AxisSpace)12 CategoryItemLabelGenerator (org.jfree.chart.labels.CategoryItemLabelGenerator)11 ValueAxis (org.jfree.chart.axis.ValueAxis)10 GeneralPath (java.awt.geom.GeneralPath)9 IntervalXYDataset (org.jfree.data.xy.IntervalXYDataset)8 Point2D (java.awt.geom.Point2D)7 GradientPaint (java.awt.GradientPaint)6 RectangleInsets (org.jfree.chart.api.RectangleInsets)6 CrosshairState (org.jfree.chart.plot.CrosshairState)6 Ellipse2D (java.awt.geom.Ellipse2D)5 AxisState (org.jfree.chart.axis.AxisState)5 AxisLocation (org.jfree.chart.axis.AxisLocation)4