Search in sources :

Example 66 with RectangleEdge

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

the class CrosshairOverlay method paintOverlay.

/**
 * Renders the crosshairs in the overlay on top of the chart that has just
 * been rendered in the specified {@code chartPanel}.  This method is
 * called by the JFreeChart framework, you won't normally call it from
 * user code.
 *
 * @param g2  the graphics target.
 * @param chartPanel  the chart panel.
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    Shape savedClip = g2.getClip();
    Rectangle2D dataArea = chartPanel.getScreenDataArea();
    g2.clip(dataArea);
    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ValueAxis xAxis = plot.getDomainAxis();
    RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
    for (Crosshair ch : getDomainCrosshairs()) {
        if (ch.isVisible()) {
            double x = ch.getValue();
            double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                drawVerticalCrosshair(g2, dataArea, xx, ch);
            } else {
                drawHorizontalCrosshair(g2, dataArea, xx, ch);
            }
        }
    }
    ValueAxis yAxis = plot.getRangeAxis();
    RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
    for (Crosshair ch : getRangeCrosshairs()) {
        if (ch.isVisible()) {
            double y = ch.getValue();
            double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                drawHorizontalCrosshair(g2, dataArea, yy, ch);
            } else {
                drawVerticalCrosshair(g2, dataArea, yy, ch);
            }
        }
    }
    g2.setClip(savedClip);
}
Also used : Shape(java.awt.Shape) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) Crosshair(org.jfree.chart.plot.Crosshair) Rectangle2D(java.awt.geom.Rectangle2D) JFreeChart(org.jfree.chart.JFreeChart) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Example 67 with RectangleEdge

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

the class XYLineAndShapeRenderer method drawPrimaryLine.

/**
 * Draws the item (first pass). This method draws the lines
 * connecting the items.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the area within which the data is being drawn.
 * @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 pass  the pass.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 */
protected void drawPrimaryLine(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {
    if (item == 0) {
        return;
    }
    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(y1) || Double.isNaN(x1)) {
        return;
    }
    double x0 = dataset.getXValue(series, item - 1);
    double y0 = dataset.getYValue(series, item - 1);
    if (Double.isNaN(y0) || Double.isNaN(x0)) {
        return;
    }
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
    // only draw if we have good values
    if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1) || Double.isNaN(transY1)) {
        return;
    }
    PlotOrientation orientation = plot.getOrientation();
    boolean visible;
    if (orientation == PlotOrientation.HORIZONTAL) {
        state.workingLine.setLine(transY0, transX0, transY1, transX1);
    } else if (orientation == PlotOrientation.VERTICAL) {
        state.workingLine.setLine(transX0, transY0, transX1, transY1);
    }
    visible = LineUtils.clipLine(state.workingLine, dataArea);
    if (visible) {
        drawFirstPassShape(g2, pass, series, item, state.workingLine);
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Example 68 with RectangleEdge

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

the class XYLineAndShapeRenderer method drawPrimaryLineAsPath.

/**
 * Draws the item (first pass). This method draws the lines
 * connecting the items. Instead of drawing separate lines,
 * a {@code GeneralPath} is constructed and drawn at the end of
 * the series painting.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param plot  the plot (can be used to obtain standard color information
 *              etc).
 * @param dataset  the dataset.
 * @param pass  the pass.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataArea  the area within which the data is being drawn.
 */
protected void drawPrimaryLineAsPath(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
    State s = (State) state;
    // update path to reflect latest point
    if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) {
        float x = (float) transX1;
        float y = (float) transY1;
        PlotOrientation orientation = plot.getOrientation();
        if (orientation == PlotOrientation.HORIZONTAL) {
            x = (float) transY1;
            y = (float) transX1;
        }
        if (s.isLastPointGood()) {
            s.seriesPath.lineTo(x, y);
        } else {
            s.seriesPath.moveTo(x, y);
        }
        s.setLastPointGood(true);
    } else {
        s.setLastPointGood(false);
    }
    // if this is the last item, draw the path ...
    if (item == s.getLastItemIndex()) {
        // draw path
        drawFirstPassShape(g2, pass, series, item, s.seriesPath);
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) CrosshairState(org.jfree.chart.plot.CrosshairState) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Example 69 with RectangleEdge

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

the class XYLineAndShapeRenderer method drawSecondaryPass.

/**
 * Draws the item shapes and adds chart entities (second pass). This method
 * draws the shapes which mark the item positions. If {@code entities}
 * is not {@code null} it will be populated with entity information
 * for points that fall within the data area.
 *
 * @param g2  the graphics device.
 * @param plot  the plot (can be used to obtain standard color
 *              information etc).
 * @param domainAxis  the domain axis.
 * @param dataArea  the area within which the data is being drawn.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param pass  the pass.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param crosshairState  the crosshair state.
 * @param entities the entity collection.
 */
protected void drawSecondaryPass(Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item, ValueAxis domainAxis, Rectangle2D dataArea, ValueAxis rangeAxis, CrosshairState crosshairState, EntityCollection entities) {
    Shape entityArea = null;
    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(y1) || Double.isNaN(x1)) {
        return;
    }
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
    if (getItemShapeVisible(series, item)) {
        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtils.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtils.createTranslatedShape(shape, transX1, transY1);
        }
        entityArea = shape;
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                if (this.useFillPaint) {
                    g2.setPaint(getItemFillPaint(series, item));
                } else {
                    g2.setPaint(getItemPaint(series, item));
                }
                g2.fill(shape);
            }
            if (this.drawOutlines) {
                if (getUseOutlinePaint()) {
                    g2.setPaint(getItemOutlinePaint(series, item));
                } else {
                    g2.setPaint(getItemPaint(series, item));
                }
                g2.setStroke(getItemOutlineStroke(series, item));
                g2.draw(shape);
            }
        }
    }
    double xx = transX1;
    double yy = transY1;
    if (orientation == PlotOrientation.HORIZONTAL) {
        xx = transY1;
        yy = transX1;
    }
    // draw the item label if there is one...
    if (isItemLabelVisible(series, item)) {
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, (y1 < 0.0));
    }
    int datasetIndex = plot.indexOf(dataset);
    updateCrosshairValues(crosshairState, x1, y1, datasetIndex, transX1, transY1, orientation);
    // area...
    if (entities != null && ShapeUtils.isPointInRect(dataArea, xx, yy)) {
        addEntity(entities, entityArea, dataset, series, item, xx, yy);
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) Shape(java.awt.Shape) Paint(java.awt.Paint) RectangleEdge(org.jfree.chart.api.RectangleEdge)

Example 70 with RectangleEdge

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

the class XYSplineRenderer method drawPrimaryLineAsPath.

/**
 * Draws the item (first pass). This method draws the lines
 * connecting the items. Instead of drawing separate lines,
 * a GeneralPath is constructed and drawn at the end of
 * the series painting.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param plot  the plot (can be used to obtain standard color information
 *              etc).
 * @param dataset  the dataset.
 * @param pass  the pass.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param xAxis  the domain axis.
 * @param yAxis  the range axis.
 * @param dataArea  the area within which the data is being drawn.
 */
@Override
protected void drawPrimaryLineAsPath(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item, ValueAxis xAxis, ValueAxis yAxis, Rectangle2D dataArea) {
    XYSplineState s = (XYSplineState) state;
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    // get the data points
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    double transX1 = xAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = yAxis.valueToJava2D(y1, dataArea, yAxisLocation);
    // Collect points
    if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) {
        Point2D p = plot.getOrientation() == PlotOrientation.HORIZONTAL ? new Point2D.Float((float) transY1, (float) transX1) : new Point2D.Float((float) transX1, (float) transY1);
        if (!s.points.contains(p))
            s.points.add(p);
    }
    if (item == dataset.getItemCount(series) - 1) {
        // construct path
        if (s.points.size() > 1) {
            Point2D origin;
            if (this.fillType == FillType.TO_ZERO) {
                float xz = (float) xAxis.valueToJava2D(0, dataArea, yAxisLocation);
                float yz = (float) yAxis.valueToJava2D(0, dataArea, yAxisLocation);
                origin = plot.getOrientation() == PlotOrientation.HORIZONTAL ? new Point2D.Float(yz, xz) : new Point2D.Float(xz, yz);
            } else if (this.fillType == FillType.TO_LOWER_BOUND) {
                float xlb = (float) xAxis.valueToJava2D(xAxis.getLowerBound(), dataArea, xAxisLocation);
                float ylb = (float) yAxis.valueToJava2D(yAxis.getLowerBound(), dataArea, yAxisLocation);
                origin = plot.getOrientation() == PlotOrientation.HORIZONTAL ? new Point2D.Float(ylb, xlb) : new Point2D.Float(xlb, ylb);
            } else {
                // fillType == TO_UPPER_BOUND
                float xub = (float) xAxis.valueToJava2D(xAxis.getUpperBound(), dataArea, xAxisLocation);
                float yub = (float) yAxis.valueToJava2D(yAxis.getUpperBound(), dataArea, yAxisLocation);
                origin = plot.getOrientation() == PlotOrientation.HORIZONTAL ? new Point2D.Float(yub, xub) : new Point2D.Float(xub, yub);
            }
            // we need at least two points to draw something
            Point2D cp0 = s.points.get(0);
            s.seriesPath.moveTo(cp0.getX(), cp0.getY());
            if (this.fillType != FillType.NONE) {
                if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
                    s.fillArea.moveTo(origin.getX(), cp0.getY());
                } else {
                    s.fillArea.moveTo(cp0.getX(), origin.getY());
                }
                s.fillArea.lineTo(cp0.getX(), cp0.getY());
            }
            if (s.points.size() == 2) {
                // we need at least 3 points to spline. Draw simple line
                // for two points
                Point2D cp1 = s.points.get(1);
                if (this.fillType != FillType.NONE) {
                    s.fillArea.lineTo(cp1.getX(), cp1.getY());
                    s.fillArea.lineTo(cp1.getX(), origin.getY());
                    s.fillArea.closePath();
                }
                s.seriesPath.lineTo(cp1.getX(), cp1.getY());
            } else {
                // construct spline
                // number of points
                int np = s.points.size();
                // Newton form coefficients
                float[] d = new float[np];
                // x-coordinates of nodes
                float[] x = new float[np];
                float y, oldy;
                float t, oldt;
                float[] a = new float[np];
                float t1;
                float t2;
                float[] h = new float[np];
                for (int i = 0; i < np; i++) {
                    Point2D.Float cpi = (Point2D.Float) s.points.get(i);
                    x[i] = cpi.x;
                    d[i] = cpi.y;
                }
                for (int i = 1; i <= np - 1; i++) h[i] = x[i] - x[i - 1];
                float[] sub = new float[np - 1];
                float[] diag = new float[np - 1];
                float[] sup = new float[np - 1];
                for (int i = 1; i <= np - 2; i++) {
                    diag[i] = (h[i] + h[i + 1]) / 3;
                    sup[i] = h[i + 1] / 6;
                    sub[i] = h[i] / 6;
                    a[i] = (d[i + 1] - d[i]) / h[i + 1] - (d[i] - d[i - 1]) / h[i];
                }
                solveTridiag(sub, diag, sup, a, np - 2);
                // note that a[0]=a[np-1]=0
                oldt = x[0];
                oldy = d[0];
                for (int i = 1; i <= np - 1; i++) {
                    // loop over intervals between nodes
                    for (int j = 1; j <= this.precision; j++) {
                        t1 = (h[i] * j) / this.precision;
                        t2 = h[i] - t1;
                        y = ((-a[i - 1] / 6 * (t2 + h[i]) * t1 + d[i - 1]) * t2 + (-a[i] / 6 * (t1 + h[i]) * t2 + d[i]) * t1) / h[i];
                        t = x[i - 1] + t1;
                        s.seriesPath.lineTo(t, y);
                        if (this.fillType != FillType.NONE) {
                            s.fillArea.lineTo(t, y);
                        }
                    }
                }
            }
            // Add last point @ y=0 for fillPath and close path
            if (this.fillType != FillType.NONE) {
                if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
                    s.fillArea.lineTo(origin.getX(), s.points.get(s.points.size() - 1).getY());
                } else {
                    s.fillArea.lineTo(s.points.get(s.points.size() - 1).getX(), origin.getY());
                }
                s.fillArea.closePath();
            }
            // fill under the curve...
            if (this.fillType != FillType.NONE) {
                Paint fp = getSeriesFillPaint(series);
                if (this.gradientPaintTransformer != null && fp instanceof GradientPaint) {
                    GradientPaint gp = this.gradientPaintTransformer.transform((GradientPaint) fp, s.fillArea);
                    g2.setPaint(gp);
                } else {
                    g2.setPaint(fp);
                }
                g2.fill(s.fillArea);
                s.fillArea.reset();
            }
            // then draw the line...
            drawFirstPassShape(g2, pass, series, item, s.seriesPath);
        }
        // reset points vector
        s.points = new ArrayList<>();
    }
}
Also used : Point2D(java.awt.geom.Point2D) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) 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