Example usage for org.jfree.data.xy OHLCDataset getXValue

List of usage examples for org.jfree.data.xy OHLCDataset getXValue

Introduction

In this page you can find the example usage for org.jfree.data.xy OHLCDataset getXValue.

Prototype

public double getXValue(int series, int item);

Source Link

Document

Returns the x-value for an item within a series.

Usage

From source file:edu.dlnu.liuwenpeng.render.CandlestickRenderer.java

/**    
* Draws the visual representation of a single data item.    
*    //w w w .j a v a2 s.  c  o m
* @param g2  the graphics device.    
* @param state  the renderer state.    
* @param dataArea  the area within which the plot is being drawn.    
* @param info  collects info 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</code> permitted).    
* @param pass  the pass index.    
*/
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) {

    boolean horiz;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        horiz = true;
    } else if (orientation == PlotOrientation.VERTICAL) {
        horiz = false;
    } else {
        return;
    }

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

    OHLCDataset highLowData = (OHLCDataset) 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);

    double volumeWidth;
    double stickWidth;
    if (this.candleWidth > 0) {
        // These are deliberately not bounded to minimums/maxCandleWidth to    
        //  retain old behaviour.    
        volumeWidth = this.candleWidth;
        stickWidth = this.candleWidth;
    } else {
        double xxWidth = 0;
        int itemCount;
        switch (this.autoWidthMethod) {

        case WIDTHMETHOD_AVERAGE:
            itemCount = highLowData.getItemCount(series);
            if (horiz) {
                xxWidth = dataArea.getHeight() / itemCount;
            } else {
                xxWidth = dataArea.getWidth() / itemCount;
            }
            break;

        case WIDTHMETHOD_SMALLEST:
            // Note: It would be nice to pre-calculate this per series    
            itemCount = highLowData.getItemCount(series);
            double lastPos = -1;
            xxWidth = dataArea.getWidth();
            for (int i = 0; i < itemCount; i++) {
                double pos = domainAxis.valueToJava2D(highLowData.getXValue(series, i), dataArea, domainEdge);
                if (lastPos != -1) {
                    xxWidth = Math.min(xxWidth, Math.abs(pos - lastPos));
                }
                lastPos = pos;
            }
            break;

        case WIDTHMETHOD_INTERVALDATA:
            IntervalXYDataset intervalXYData = (IntervalXYDataset) dataset;
            double startPos = domainAxis.valueToJava2D(intervalXYData.getStartXValue(series, item), dataArea,
                    plot.getDomainAxisEdge());
            double endPos = domainAxis.valueToJava2D(intervalXYData.getEndXValue(series, item), dataArea,
                    plot.getDomainAxisEdge());
            xxWidth = Math.abs(endPos - startPos);
            break;

        }
        xxWidth -= 2 * this.autoWidthGap;
        xxWidth *= this.autoWidthFactor;
        xxWidth = Math.min(xxWidth, this.maxCandleWidth);
        volumeWidth = Math.max(Math.min(1, this.maxCandleWidth), xxWidth);
        stickWidth = Math.max(Math.min(3, this.maxCandleWidth), xxWidth);
    }

    Paint p = getItemPaint(series, item);
    Paint outlinePaint = null;
    if (this.useOutlinePaint) {
        outlinePaint = getItemOutlinePaint(series, item);
    }
    Stroke s = getItemStroke(series, item);

    g2.setStroke(s);

    if (this.drawVolume) {
        int volume = (int) highLowData.getVolumeValue(series, item);
        double volumeHeight = volume / this.maxVolume;

        double min, max;
        if (horiz) {
            min = dataArea.getMinX();
            max = dataArea.getMaxX();
        } else {
            min = dataArea.getMinY();
            max = dataArea.getMaxY();
        }

        double zzVolume = volumeHeight * (max - min);

        g2.setPaint(getVolumePaint());
        Composite originalComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));

        if (horiz) {
            g2.fill(new Rectangle2D.Double(min, xx - volumeWidth / 2, zzVolume, volumeWidth));
        } else {
            g2.fill(new Rectangle2D.Double(xx - volumeWidth / 2, max - zzVolume, volumeWidth, zzVolume));
        }

        g2.setComposite(originalComposite);
    }

    if (this.useOutlinePaint) {
        g2.setPaint(outlinePaint);
    } else {
        g2.setPaint(p);
    }

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

    // draw the upper shadow    
    if (yHigh > maxOpenClose) {
        if (yClose > yOpen) {
            g2.setPaint(Color.green);
        } else {
            g2.setPaint(Color.red);

        }
        if (horiz) {
            g2.draw(new Line2D.Double(yyHigh, xx, yyMaxOpenClose, xx));
        } else {
            g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose));
        }
    }

    // draw the lower shadow    
    if (yLow < minOpenClose) {
        if (yClose > yOpen) {
            g2.setPaint(Color.green);
        } else {
            g2.setPaint(Color.red);

        }
        if (horiz) {
            g2.draw(new Line2D.Double(yyLow, xx, yyMinOpenClose, xx));
        } else {
            g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose));
        }
    }

    // draw the body    
    Rectangle2D body = null;
    Rectangle2D hotspot = null;
    double length = Math.abs(yyHigh - yyLow);
    double base = Math.min(yyHigh, yyLow);
    if (horiz) {
        body = new Rectangle2D.Double(yyMinOpenClose, xx - stickWidth / 2, yyMaxOpenClose - yyMinOpenClose,
                stickWidth);

        hotspot = new Rectangle2D.Double(base, xx - stickWidth / 2, length, stickWidth);
    } else {
        body = new Rectangle2D.Double(xx - stickWidth / 2, yyMinOpenClose, stickWidth,
                yyMaxOpenClose - yyMinOpenClose);
        hotspot = new Rectangle2D.Double(xx - stickWidth / 2, base, stickWidth, length);

    }
    if (yClose > yOpen) {
        if (this.upPaint != null) {
            g2.setPaint(this.upPaint);
        } else {
            g2.setPaint(p);
        }
        g2.fill(body);
    } else {
        if (this.downPaint != null) {
            g2.setPaint(this.downPaint);
        } else {
            g2.setPaint(p);
        }
        g2.fill(body);
    }
    if (this.useOutlinePaint) {
        g2.setPaint(outlinePaint);

    } else {
        if (yClose > yOpen) {
            g2.setPaint(Color.green);

        } else {
            g2.setPaint(p);
        }
    }
    g2.draw(body);

    // add an entity for the item...    
    if (entities != null) {
        addEntity(entities, hotspot, dataset, series, item, 0.0, 0.0);
    }

}

From source file:org.jfree.data.general.DatasetUtils.java

/**
 * Returns the range of y-values in the specified dataset for the
 * data items belonging to the visible series and with x-values in the
 * given range./* w ww .  jav  a2s  .  c o m*/
 *
 * @param dataset  the dataset ({@code null} not permitted).
 * @param visibleSeriesKeys  the visible series keys ({@code null} not
 *     permitted).
 * @param xRange  the x-range ({@code null} not permitted).
 * @param includeInterval  a flag that determines whether or not the
 *     y-interval for the dataset is included (this only applies if the
 *     dataset is an instance of IntervalXYDataset).
 *
 * @return The y-range (possibly {@code null}).
 *
 * @since 1.0.13
 */
public static Range iterateToFindRangeBounds(XYDataset dataset, List visibleSeriesKeys, Range xRange,
        boolean includeInterval) {

    Args.nullNotPermitted(dataset, "dataset");
    Args.nullNotPermitted(visibleSeriesKeys, "visibleSeriesKeys");
    Args.nullNotPermitted(xRange, "xRange");

    double minimum = Double.POSITIVE_INFINITY;
    double maximum = Double.NEGATIVE_INFINITY;

    // handle three cases by dataset type
    if (includeInterval && dataset instanceof OHLCDataset) {
        // handle special case of OHLCDataset
        OHLCDataset ohlc = (OHLCDataset) dataset;
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = ohlc.getXValue(series, item);
                if (xRange.contains(x)) {
                    double lvalue = ohlc.getLowValue(series, item);
                    double uvalue = ohlc.getHighValue(series, item);
                    if (!Double.isNaN(lvalue)) {
                        minimum = Math.min(minimum, lvalue);
                    }
                    if (!Double.isNaN(uvalue)) {
                        maximum = Math.max(maximum, uvalue);
                    }
                }
            }
        }
    } else if (includeInterval && dataset instanceof BoxAndWhiskerXYDataset) {
        // handle special case of BoxAndWhiskerXYDataset
        BoxAndWhiskerXYDataset bx = (BoxAndWhiskerXYDataset) dataset;
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = bx.getXValue(series, item);
                if (xRange.contains(x)) {
                    Number lvalue = bx.getMinRegularValue(series, item);
                    Number uvalue = bx.getMaxRegularValue(series, item);
                    if (lvalue != null) {
                        minimum = Math.min(minimum, lvalue.doubleValue());
                    }
                    if (uvalue != null) {
                        maximum = Math.max(maximum, uvalue.doubleValue());
                    }
                }
            }
        }
    } else if (includeInterval && dataset instanceof IntervalXYDataset) {
        // handle special case of IntervalXYDataset
        IntervalXYDataset ixyd = (IntervalXYDataset) dataset;
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = ixyd.getXValue(series, item);
                if (xRange.contains(x)) {
                    double yvalue = ixyd.getYValue(series, item);
                    double lvalue = ixyd.getStartYValue(series, item);
                    double uvalue = ixyd.getEndYValue(series, item);
                    if (!Double.isNaN(yvalue)) {
                        minimum = Math.min(minimum, yvalue);
                        maximum = Math.max(maximum, yvalue);
                    }
                    if (!Double.isNaN(lvalue)) {
                        minimum = Math.min(minimum, lvalue);
                    }
                    if (!Double.isNaN(uvalue)) {
                        maximum = Math.max(maximum, uvalue);
                    }
                }
            }
        }
    } else {
        // standard case - plain XYDataset
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = dataset.getXValue(series, item);
                double y = dataset.getYValue(series, item);
                if (xRange.contains(x)) {
                    if (!Double.isNaN(y)) {
                        minimum = Math.min(minimum, y);
                        maximum = Math.max(maximum, y);
                    }
                }
            }
        }
    }
    if (minimum == Double.POSITIVE_INFINITY) {
        return null;
    } else {
        return new Range(minimum, maximum);
    }
}

From source file:org.jfree.data.general.DatasetUtilities.java

/**
 * Returns the range of y-values in the specified dataset for the
 * data items belonging to the visible series and with x-values in the
 * given range.//from   w  w w .  j a  v  a 2s  .  c  o m
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param visibleSeriesKeys  the visible series keys (<code>null</code> not
 *     permitted).
 * @param xRange  the x-range (<code>null</code> not permitted).
 * @param includeInterval  a flag that determines whether or not the
 *     y-interval for the dataset is included (this only applies if the
 *     dataset is an instance of IntervalXYDataset).
 *
 * @return The y-range (possibly <code>null</code>).
 *
 * @since 1.0.13
 */
public static Range iterateToFindRangeBounds(XYDataset dataset, List visibleSeriesKeys, Range xRange,
        boolean includeInterval) {

    ParamChecks.nullNotPermitted(dataset, "dataset");
    ParamChecks.nullNotPermitted(visibleSeriesKeys, "visibleSeriesKeys");
    ParamChecks.nullNotPermitted(xRange, "xRange");

    double minimum = Double.POSITIVE_INFINITY;
    double maximum = Double.NEGATIVE_INFINITY;

    // handle three cases by dataset type
    if (includeInterval && dataset instanceof OHLCDataset) {
        // handle special case of OHLCDataset
        OHLCDataset ohlc = (OHLCDataset) dataset;
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = ohlc.getXValue(series, item);
                if (xRange.contains(x)) {
                    double lvalue = ohlc.getLowValue(series, item);
                    double uvalue = ohlc.getHighValue(series, item);
                    if (!Double.isNaN(lvalue)) {
                        minimum = Math.min(minimum, lvalue);
                    }
                    if (!Double.isNaN(uvalue)) {
                        maximum = Math.max(maximum, uvalue);
                    }
                }
            }
        }
    } else if (includeInterval && dataset instanceof BoxAndWhiskerXYDataset) {
        // handle special case of BoxAndWhiskerXYDataset
        BoxAndWhiskerXYDataset bx = (BoxAndWhiskerXYDataset) dataset;
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = bx.getXValue(series, item);
                if (xRange.contains(x)) {
                    Number lvalue = bx.getMinRegularValue(series, item);
                    Number uvalue = bx.getMaxRegularValue(series, item);
                    if (lvalue != null) {
                        minimum = Math.min(minimum, lvalue.doubleValue());
                    }
                    if (uvalue != null) {
                        maximum = Math.max(maximum, uvalue.doubleValue());
                    }
                }
            }
        }
    } else if (includeInterval && dataset instanceof IntervalXYDataset) {
        // handle special case of IntervalXYDataset
        IntervalXYDataset ixyd = (IntervalXYDataset) dataset;
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = ixyd.getXValue(series, item);
                if (xRange.contains(x)) {
                    double lvalue = ixyd.getStartYValue(series, item);
                    double uvalue = ixyd.getEndYValue(series, item);
                    if (!Double.isNaN(lvalue)) {
                        minimum = Math.min(minimum, lvalue);
                    }
                    if (!Double.isNaN(uvalue)) {
                        maximum = Math.max(maximum, uvalue);
                    }
                }
            }
        }
    } else {
        // standard case - plain XYDataset
        Iterator iterator = visibleSeriesKeys.iterator();
        while (iterator.hasNext()) {
            Comparable seriesKey = (Comparable) iterator.next();
            int series = dataset.indexOf(seriesKey);
            int itemCount = dataset.getItemCount(series);
            for (int item = 0; item < itemCount; item++) {
                double x = dataset.getXValue(series, item);
                double y = dataset.getYValue(series, item);
                if (xRange.contains(x)) {
                    if (!Double.isNaN(y)) {
                        minimum = Math.min(minimum, y);
                        maximum = Math.max(maximum, y);
                    }
                }
            }
        }
    }
    if (minimum == Double.POSITIVE_INFINITY) {
        return null;
    } else {
        return new Range(minimum, maximum);
    }
}