Example usage for org.jfree.chart.entity EntityCollection add

List of usage examples for org.jfree.chart.entity EntityCollection add

Introduction

In this page you can find the example usage for org.jfree.chart.entity EntityCollection add.

Prototype

public void add(ChartEntity entity);

Source Link

Document

Adds an entity to the collection.

Usage

From source file:genlab.gui.jfreechart.EnhancedSpiderWebPlot.java

/**
 * Draws a radar plot polygon./*from w ww.  j a v a2  s .  co m*/
 *
 * @param g2 the graphics device.
 * @param plotArea the area we are plotting in (already adjusted).
 * @param centre the centre point of the radar axes
 * @param info chart rendering info.
 * @param series the series within the dataset we are plotting
 * @param catCount the number of categories per radar plot
 * @param headH the data point height
 * @param headW the data point width
 */
protected void drawRadarPoly(Graphics2D g2, Rectangle2D plotArea, Point2D centre, PlotRenderingInfo info,
        int series, int catCount, double headH, double headW) {

    Polygon polygon = new Polygon();

    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

    // plot the data...
    for (int cat = 0; cat < catCount; cat++) {

        Number dataValue = getPlotValue(series, cat);

        if (dataValue != null) {
            double value = dataValue.doubleValue();

            if (value >= 0) { // draw the polygon series...

                // Finds our starting angle from the centre for this axis

                double angle = getStartAngle()
                        + (getDirection().getFactor() * cat * 360 / (catCount > 2 ? catCount : 3));

                // The following angle calc will ensure there isn't a top
                // vertical axis - this may be useful if you don't want any
                // given criteria to 'appear' move important than the
                // others..
                //  + (getDirection().getFactor()
                //        * (cat + 0.5) * 360 / catCount);

                // find the point at the appropriate distance end point
                // along the axis/angle identified above and add it to the
                // polygon

                Point2D point = getWebPoint(plotArea, angle, value / this.maxValue);
                polygon.addPoint((int) point.getX(), (int) point.getY());

                // put an elipse at the point being plotted..

                Paint paint = getSeriesPaint(series);
                Paint outlinePaint = getSeriesOutlinePaint(series);
                Stroke outlineStroke = getSeriesOutlineStroke(series);

                Ellipse2D head = new Ellipse2D.Double(point.getX() - headW / 2, point.getY() - headH / 2, headW,
                        headH);
                g2.setPaint(paint);
                g2.fill(head);
                g2.setStroke(outlineStroke);
                g2.setPaint(outlinePaint);
                g2.draw(head);

                if (entities != null) {
                    int row = 0;
                    int col = 0;
                    if (this.dataExtractOrder == TableOrder.BY_ROW) {
                        row = series;
                        col = cat;
                    } else {
                        row = cat;
                        col = series;
                    }
                    String tip = null;
                    if (this.toolTipGenerator != null) {
                        tip = this.toolTipGenerator.generateToolTip(this.dataset, row, col);
                    }

                    String url = null;
                    if (this.urlGenerator != null) {
                        url = this.urlGenerator.generateURL(this.dataset, row, col);
                    }

                    Shape area = new Rectangle((int) (point.getX() - headW), (int) (point.getY() - headH),
                            (int) (headW * 2), (int) (headH * 2));
                    CategoryItemEntity entity = new CategoryItemEntity(area, tip, url, this.dataset,
                            this.dataset.getRowKey(row), this.dataset.getColumnKey(col));
                    entities.add(entity);
                }

            }
        }
    }
    // Plot the polygon

    Paint paint = getSeriesPaint(series);
    g2.setPaint(paint);
    g2.setStroke(getSeriesOutlineStroke(series));
    g2.draw(polygon);

    // Lastly, fill the web polygon if this is required

    if (this.webFilled) {
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
        g2.fill(polygon);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
    }
}

From source file:org.trade.ui.chart.renderer.HeikinAshiRenderer.java

/**
 * Method drawItem./*  www. j a  v  a2 s .c  om*/
 * 
 * @param g2
 *            Graphics2D
 * @param state
 *            XYItemRendererState
 * @param dataArea
 *            Rectangle2D
 * @param info
 *            PlotRenderingInfo
 * @param plot
 *            XYPlot
 * @param domainAxis
 *            ValueAxis
 * @param rangeAxis
 *            ValueAxis
 * @param dataset
 *            XYDataset
 * @param series
 *            int
 * @param item
 *            int
 * @param crosshairState
 *            CrosshairState
 * @param pass
 *            int
 * @see org.jfree.chart.renderer.xy.XYItemRenderer#drawItem(Graphics2D,
 *      XYItemRendererState, Rectangle2D, PlotRenderingInfo, XYPlot,
 *      ValueAxis, ValueAxis, XYDataset, int, int, CrosshairState, int)
 */
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 (dataset instanceof HeikinAshiDataset) {

        // setup for collecting optional entity info...
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }

        HeikinAshiDataset highLowData = (HeikinAshiDataset) dataset;

        double x = highLowData.getXValue(series, item);
        double yHigh = highLowData.getHighValue(series, item);
        double yLow = highLowData.getLowValue(series, item);
        double yOpen = highLowData.getOpenValue(series, item);
        double yClose = highLowData.getCloseValue(series, item);

        RectangleEdge domainEdge = plot.getDomainAxisEdge();
        double xx = domainAxis.valueToJava2D(x, dataArea, domainEdge);

        RectangleEdge edge = plot.getRangeAxisEdge();
        double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, edge);
        double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, edge);
        double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea, edge);
        double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, edge);

        int itemCount = highLowData.getItemCount(series);
        double xxWidth = dataArea.getWidth() / itemCount;

        xxWidth -= 2 * this.getAutoWidthGap();
        xxWidth *= this.getAutoWidthFactor();
        xxWidth = Math.min(xxWidth, this.maxCandleWidth);
        double stickWidth = Math.max(Math.min(3, this.maxCandleWidth), xxWidth);

        Paint outlinePaint = null;
        outlinePaint = getItemOutlinePaint(series, item);
        g2.setStroke(getItemStroke(series, item));
        g2.setPaint(outlinePaint);

        double yyMaxOpenClose = Math.max(yyOpen, yyClose);
        double yyMinOpenClose = Math.min(yyOpen, yyClose);
        double maxOpenClose = Math.max(yOpen, yClose);
        double minOpenClose = Math.min(yOpen, yClose);

        Shape body = null;
        boolean highlight = highlight(series, item);
        /**********************************
         * draw the upper shadow START
         **********************************/

        if (yHigh > maxOpenClose) {
            if (highlight) {
                body = new Rectangle2D.Double(xx - (stickWidth / 2), yyHigh - 10, stickWidth,
                        (yyMaxOpenClose - yyHigh) + 10);
                g2.setPaint(Color.YELLOW);
                g2.fill(body);
                g2.draw(body);
            }
        }

        if (yHigh > maxOpenClose) {
            if (nightMode) {
                if (yClose > yOpen) {
                    g2.setPaint(upPaint);
                } else {
                    g2.setPaint(downPaint);
                }
            } else {
                g2.setPaint(Color.black);
            }

            g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose));
        }

        /**********************************
         * draw the lower shadow START
         **********************************/
        if (yLow < minOpenClose) {
            if (highlight) {
                body = new Rectangle2D.Double(xx - (stickWidth / 2), yyMinOpenClose, stickWidth,
                        (yyLow - yyMinOpenClose) + 10);
                g2.setPaint(Color.YELLOW);
                g2.fill(body);
                g2.draw(body);
            }
            if (yLow < minOpenClose) {
                if (nightMode) {
                    if (yClose > yOpen) {
                        g2.setPaint(upPaint);
                    } else {
                        g2.setPaint(downPaint);
                    }
                } else {
                    g2.setPaint(Color.BLACK);
                }
                g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose));
            }
        }

        /**********************************
         * draw the body
         **********************************/

        body = new Rectangle2D.Double(xx - (stickWidth / 2), yyMinOpenClose, stickWidth,
                yyMaxOpenClose - yyMinOpenClose);

        if (nightMode) {
            g2.setPaint(Color.white);
        } else {
            if (yClose > yOpen) {
                g2.setPaint(upPaint);
            } else {
                g2.setPaint(downPaint);
            }
        }

        g2.fill(body);
        g2.draw(body);

        if (nightMode) {
            if (yClose > yOpen) {
                g2.setPaint(upPaint);
            } else {
                g2.setPaint(downPaint);
            }
        } else {
            g2.setPaint(outlinePaint);
        }
        g2.draw(body);
        // add an entity for the item...
        if (entities != null) {
            String tip = null;
            XYToolTipGenerator generator = getToolTipGenerator(series, item);
            if (generator != null) {
                tip = generator.generateToolTip(dataset, series, item);
            }

            XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, null);

            entities.add(entity);
        }
    }
}

From source file:spinworld.gui.RadarPlot.java

/**
 * Draws a radar plot polygon./*from  w  w w  .j av a 2  s  . co  m*/
 *
 * @param g2 the graphics device.
 * @param plotArea the area we are plotting in (already adjusted).
 * @param centre the centre point of the radar axes
 * @param info chart rendering info.
 * @param series the series within the dataset we are plotting
 * @param catCount the number of categories per radar plot
 * @param headH the data point height
 * @param headW the data point width
 */
protected void drawRadarPoly(Graphics2D g2, Rectangle2D plotArea, Point2D centre, PlotRenderingInfo info,
        int series, int catCount, double headH, double headW) {

    Polygon polygon = new Polygon();

    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

    // plot the data...
    for (int cat = 0; cat < catCount; cat++) {

        Number dataValue = getPlotValue(series, cat);

        if (dataValue != null) {
            double value = dataValue.doubleValue();

            // Finds our starting angle from the centre for this axis

            double angle = getStartAngle() + (getDirection().getFactor() * cat * 360 / catCount);

            // The following angle calc will ensure there isn't a top
            // vertical axis - this may be useful if you don't want any
            // given criteria to 'appear' move important than the
            // others..
            //  + (getDirection().getFactor()
            //        * (cat + 0.5) * 360 / catCount);

            // find the point at the appropriate distance end point
            // along the axis/angle identified above and add it to the
            // polygon

            double _maxValue = getMaxValue(cat).doubleValue();
            double _origin = getOrigin(cat).doubleValue();
            double lowerBound = Math.min(_origin, _maxValue);
            double upperBound = Math.max(_origin, _maxValue);
            boolean lesser = value < lowerBound;
            boolean greater = value > upperBound;
            if ((lesser || greater) && !drawOutOfRangePoints) {
                continue;
            }
            if (lesser) {
                value = lowerBound;
            }
            if (greater) {
                value = upperBound;
            }
            double length = _maxValue == _origin ? 0 : (value - lowerBound) / (upperBound - lowerBound);
            if (_maxValue < _origin) { // inversed
                length = 1 - length;
            }
            Point2D point = getWebPoint(plotArea, angle, length);
            polygon.addPoint((int) point.getX(), (int) point.getY());

            Paint paint = getSeriesPaint(series);
            Paint outlinePaint = getSeriesOutlinePaint(series);

            double px = point.getX();
            double py = point.getY();
            g2.setPaint(paint);
            if (lesser || greater) {
                // user crosshair for out-of-range data points distinguish
                g2.setStroke(new BasicStroke(1.5f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL));
                double delta = 3;
                g2.draw(new Line2D.Double(px - delta, py, px + delta, py));
                g2.draw(new Line2D.Double(px, py - delta, px, py + delta));
            } else {
                // put an elipse at the point being plotted..
                Ellipse2D head = new Ellipse2D.Double(px - headW / 2, py - headH / 2, headW, headH);
                g2.fill(head);
                g2.setStroke(getHeadOutlineStroke(series));
                g2.setPaint(outlinePaint);
                g2.draw(head);
            }

            if (entities != null) {
                int row = 0;
                int col = 0;
                if (this.dataExtractOrder == TableOrder.BY_ROW) {
                    row = series;
                    col = cat;
                } else {
                    row = cat;
                    col = series;
                }
                String tip = null;
                if (this.toolTipGenerator != null) {
                    tip = this.toolTipGenerator.generateToolTip(this.dataset, row, col);
                }

                String url = null;
                if (this.urlGenerator != null) {
                    url = this.urlGenerator.generateURL(this.dataset, row, col);
                }

                Shape area = new Rectangle((int) (point.getX() - headW), (int) (point.getY() - headH),
                        (int) (headW * 2), (int) (headH * 2));
                CategoryItemEntity entity = new CategoryItemEntity(area, tip, url, this.dataset,
                        this.dataset.getRowKey(row), this.dataset.getColumnKey(col));
                entities.add(entity);
            }
        }
    }
    // Plot the polygon

    Paint paint = getSeriesPaint(series);
    g2.setPaint(paint);
    g2.setStroke(getSeriesOutlineStroke(series));
    g2.draw(polygon);

    // Lastly, fill the web polygon if this is required

    if (this.webFilled) {
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
        g2.fill(polygon);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
    }
}

From source file:org.trade.ui.chart.renderer.CandleRenderer.java

/**
 * Method drawItem./*from ww  w .jav a 2  s .c  o m*/
 * 
 * @param g2
 *            Graphics2D
 * @param state
 *            XYItemRendererState
 * @param dataArea
 *            Rectangle2D
 * @param info
 *            PlotRenderingInfo
 * @param plot
 *            XYPlot
 * @param domainAxis
 *            ValueAxis
 * @param rangeAxis
 *            ValueAxis
 * @param dataset
 *            XYDataset
 * @param series
 *            int
 * @param item
 *            int
 * @param crosshairState
 *            CrosshairState
 * @param pass
 *            int
 * @see org.jfree.chart.renderer.xy.XYItemRenderer#drawItem(Graphics2D,
 *      XYItemRendererState, Rectangle2D, PlotRenderingInfo, XYPlot,
 *      ValueAxis, ValueAxis, XYDataset, int, int, CrosshairState, int)
 */
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 (dataset instanceof OHLCVwapDataset) {

        // setup for collecting optional entity info...
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }

        CandleDataset candleDataset = (CandleDataset) dataset;
        CandleItem candle = (CandleItem) candleDataset.getSeries(series).getDataItem(item);

        double startX = candle.getPeriod().getFirstMillisecond();
        if (Double.isNaN(startX)) {
            return;
        }
        double endX = candle.getPeriod().getLastMillisecond();
        if (Double.isNaN(endX)) {
            return;
        }

        if (startX <= endX) {
            if (!domainAxis.getRange().intersects(startX, endX)) {
                return;
            }
        } else {
            if (!domainAxis.getRange().intersects(endX, startX)) {
                return;
            }
        }

        RectangleEdge location = plot.getDomainAxisEdge();
        double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, location);
        double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, location);

        double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));

        if (getMargin() > 0.0) {
            double cut = translatedWidth * getMargin();
            translatedWidth = translatedWidth - cut;
        }

        double x = candleDataset.getXValue(series, item);
        double yHigh = candleDataset.getHighValue(series, item);
        double yLow = candleDataset.getLowValue(series, item);
        double yOpen = candleDataset.getOpenValue(series, item);
        double yClose = candleDataset.getCloseValue(series, item);

        RectangleEdge domainEdge = plot.getDomainAxisEdge();
        double xx = domainAxis.valueToJava2D(x, dataArea, domainEdge);

        RectangleEdge edge = plot.getRangeAxisEdge();
        double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, edge);
        double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, edge);
        double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea, edge);
        double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, edge);

        Paint outlinePaint = null;
        outlinePaint = getItemOutlinePaint(series, item);
        g2.setStroke(getItemStroke(series, item));
        g2.setPaint(outlinePaint);

        double yyMaxOpenClose = Math.max(yyOpen, yyClose);
        double yyMinOpenClose = Math.min(yyOpen, yyClose);
        double maxOpenClose = Math.max(yOpen, yClose);
        double minOpenClose = Math.min(yOpen, yClose);

        Shape body = null;
        boolean highlight = highlight(series, item);
        /**********************************
         * draw the upper shadow START
         **********************************/

        if (yHigh > maxOpenClose) {
            if (highlight) {
                body = new Rectangle2D.Double(xx - (translatedWidth / 2), yyHigh - 10, translatedWidth,
                        (yyMaxOpenClose - yyHigh) + 10);
                g2.setPaint(Color.YELLOW);
                g2.fill(body);
                g2.draw(body);
            }
        }

        if (yHigh > maxOpenClose) {
            if (nightMode) {
                if (yClose > yOpen) {
                    g2.setPaint(upPaint);
                } else {
                    g2.setPaint(downPaint);
                }
            } else {
                g2.setPaint(Color.black);
            }

            g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose));
        }

        /**********************************
         * draw the lower shadow START
         **********************************/
        if (yLow < minOpenClose) {
            if (highlight) {
                body = new Rectangle2D.Double(xx - (translatedWidth / 2), yyMinOpenClose, translatedWidth,
                        (yyLow - yyMinOpenClose) + 10);
                g2.setPaint(Color.YELLOW);
                g2.fill(body);
                g2.draw(body);
            }
            if (yLow < minOpenClose) {
                if (nightMode) {
                    if (yClose > yOpen) {
                        g2.setPaint(upPaint);
                    } else {
                        g2.setPaint(downPaint);
                    }
                } else {
                    g2.setPaint(Color.BLACK);
                }
                g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose));
            }
        }

        /**********************************
         * draw the body
         **********************************/

        body = new Rectangle2D.Double(xx - (translatedWidth / 2), yyMinOpenClose, translatedWidth,
                yyMaxOpenClose - yyMinOpenClose);

        if (nightMode) {
            g2.setPaint(Color.white);
        } else {
            if (yClose > yOpen) {
                g2.setPaint(upPaint);
            } else {
                g2.setPaint(downPaint);
            }
        }

        g2.fill(body);
        g2.draw(body);

        if (nightMode) {
            if (yClose > yOpen) {
                g2.setPaint(upPaint);
            } else {
                g2.setPaint(downPaint);
            }
        } else {
            g2.setPaint(outlinePaint);
        }
        g2.draw(body);
        // add an entity for the item...
        if (entities != null) {
            String tip = null;
            XYToolTipGenerator generator = getToolTipGenerator(series, item);
            if (generator != null) {
                tip = generator.generateToolTip(dataset, series, item);
            }

            XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, null);

            entities.add(entity);
        }

        // update the cross hair point
        double x1 = dataset.getXValue(series, item);
        double y1 = dataset.getYValue(series, item);
        double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
        double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
                plot.getOrientation());
    }
}