Search in sources :

Example 1 with OHLCDataset

use of org.jfree.data.xy.OHLCDataset in project tdq-studio-se by Talend.

the class ToolTipChartComposite method getTooltipAtPoint.

/**
 * This method attempts to get a tooltip by converting the screen X,Y into Chart Area X,Y and then looking for a
 * data point in a data set that lies inside a hotspot around that value.
 *
 * @param point The Java 2D point
 * @return A string for the data at the point or null if no data is found.
 */
protected String getTooltipAtPoint(Point point) {
    String result = null;
    Point2D translatedPoint = this.translateScreenToJava2D(point);
    Plot plot = this.getChart().getPlot();
    PlotRenderingInfo info = this.getChartRenderingInfo().getPlotInfo();
    if (plot instanceof CombinedDomainXYPlot) {
        int index = info.getSubplotIndex(translatedPoint);
        if (index < 0) {
            index = 0;
        }
        plot = (Plot) ((CombinedDomainXYPlot) plot).getSubplots().get(index);
        info = this.getChartRenderingInfo().getPlotInfo().getSubplotInfo(index);
    }
    if (plot != null && plot instanceof XYPlot) {
        XYPlot xyPlot = (XYPlot) plot;
        ValueAxis domainAxis = xyPlot.getDomainAxis();
        ValueAxis rangeAxis = xyPlot.getRangeAxis();
        // had to switch to SWT's rectangle here.
        Rectangle screenArea = this.scale(info.getDataArea());
        double hotspotSizeX = hotspontsize * this.getScaleX();
        double hotspotSizeY = hotspontsize * this.getScaleY();
        double x0 = point.getX();
        double y0 = point.getY();
        double x1 = x0 - hotspotSizeX;
        double y1 = y0 + hotspotSizeY;
        double x2 = x0 + hotspotSizeX;
        double y2 = y0 - hotspotSizeY;
        RectangleEdge xEdge = RectangleEdge.BOTTOM;
        RectangleEdge yEdge = RectangleEdge.LEFT;
        // Switch everything for horizontal charts
        if (xyPlot.getOrientation() == PlotOrientation.HORIZONTAL) {
            hotspotSizeX = hotspontsize * this.getScaleY();
            hotspotSizeY = hotspontsize * this.getScaleX();
            x0 = point.getY();
            y0 = point.getX();
            x1 = x0 + hotspotSizeX;
            y1 = y0 - hotspotSizeY;
            x2 = x0 - hotspotSizeX;
            y2 = y0 + hotspotSizeY;
            xEdge = RectangleEdge.LEFT;
            yEdge = RectangleEdge.BOTTOM;
        }
        // OK, here we have to get ourselves back into AWT land...
        Rectangle2D r2d = new Rectangle2D.Double();
        r2d.setRect(screenArea.x, screenArea.y, screenArea.width, screenArea.height);
        double ty0 = rangeAxis.java2DToValue(y0, r2d, yEdge);
        double tx1 = domainAxis.java2DToValue(x1, r2d, xEdge);
        double ty1 = rangeAxis.java2DToValue(y1, r2d, yEdge);
        double tx2 = domainAxis.java2DToValue(x2, r2d, xEdge);
        double ty2 = rangeAxis.java2DToValue(y2, r2d, yEdge);
        int datasetCount = xyPlot.getDatasetCount();
        for (int datasetIndex = 0; datasetIndex < datasetCount; datasetIndex++) {
            XYDataset dataset = xyPlot.getDataset(datasetIndex);
            int seriesCount = dataset.getSeriesCount();
            for (int series = 0; series < seriesCount; series++) {
                int itemCount = dataset.getItemCount(series);
                if (dataset instanceof OHLCDataset) {
                    // This could be optimized to use a binary search for x first
                    for (int item = 0; item < itemCount; item++) {
                        double xValue = dataset.getXValue(series, item);
                        double yValueHi = ((OHLCDataset) dataset).getHighValue(series, item);
                        double yValueLo = ((OHLCDataset) dataset).getLowValue(series, item);
                        // Check hi lo and swap if needed
                        if (yValueHi < yValueLo) {
                            double temp = yValueHi;
                            yValueHi = yValueLo;
                            yValueLo = temp;
                        }
                        // Check if the dataset 'X' value lies between the hotspot (tx1 < xValue < tx2)
                        if (tx1 < xValue && xValue < tx2) {
                            // Check if the cursor 'y' value lies between the high and low (low < ty0 < high)
                            if (yValueLo < ty0 && ty0 < yValueHi) {
                                return hiLoTips.generateToolTip(dataset, series, item);
                            }
                        }
                    }
                } else {
                    // This could be optimized to use a binary search for x first
                    for (int item = 0; item < itemCount; item++) {
                        double xValue = dataset.getXValue(series, item);
                        double yValue = dataset.getYValue(series, item);
                        // Check if the dataset 'X' value lies between the hotspot (tx1< xValue < tx2)
                        if (tx1 < xValue && xValue < tx2) {
                            // Check if the dataset 'Y' value lies between the hotspot (ty1 < yValue < ty2)
                            if (ty1 < yValue && yValue < ty2) {
                                return xyTips.generateToolTip(dataset, series, item);
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : PlotRenderingInfo(org.jfree.chart.plot.PlotRenderingInfo) XYPlot(org.jfree.chart.plot.XYPlot) CombinedDomainXYPlot(org.jfree.chart.plot.CombinedDomainXYPlot) Plot(org.jfree.chart.plot.Plot) Rectangle(org.eclipse.swt.graphics.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) Point(java.awt.Point) OHLCDataset(org.jfree.data.xy.OHLCDataset) XYPlot(org.jfree.chart.plot.XYPlot) CombinedDomainXYPlot(org.jfree.chart.plot.CombinedDomainXYPlot) Point2D(java.awt.geom.Point2D) ValueAxis(org.jfree.chart.axis.ValueAxis) XYDataset(org.jfree.data.xy.XYDataset) CombinedDomainXYPlot(org.jfree.chart.plot.CombinedDomainXYPlot) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 2 with OHLCDataset

use of org.jfree.data.xy.OHLCDataset in project ta4j by ta4j.

the class CandlestickChart method main.

public static void main(String[] args) {
    /*
          Getting time series
         */
    TimeSeries series = CsvTradesLoader.loadBitstampSeries();
    /*
          Creating the OHLC dataset
         */
    OHLCDataset ohlcDataset = createOHLCDataset(series);
    /*
          Creating the additional dataset
         */
    TimeSeriesCollection xyDataset = createAdditionalDataset(series);
    /*
          Creating the chart
         */
    JFreeChart chart = ChartFactory.createCandlestickChart("Bitstamp BTC price", "Time", "USD", ohlcDataset, true);
    // Candlestick rendering
    CandlestickRenderer renderer = new CandlestickRenderer();
    renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
    XYPlot plot = chart.getXYPlot();
    plot.setRenderer(renderer);
    // Additional dataset
    int index = 1;
    plot.setDataset(index, xyDataset);
    plot.mapDatasetToRangeAxis(index, 0);
    XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, false);
    renderer2.setSeriesPaint(index, Color.blue);
    plot.setRenderer(index, renderer2);
    // Misc
    plot.setRangeGridlinePaint(Color.lightGray);
    plot.setBackgroundPaint(Color.white);
    NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
    numberAxis.setAutoRangeIncludesZero(false);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    /*
          Displaying the chart
         */
    displayChart(chart);
}
Also used : OHLCDataset(org.jfree.data.xy.OHLCDataset) TimeSeries(org.ta4j.core.TimeSeries) NumberAxis(org.jfree.chart.axis.NumberAxis) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) CandlestickRenderer(org.jfree.chart.renderer.xy.CandlestickRenderer) JFreeChart(org.jfree.chart.JFreeChart)

Example 3 with OHLCDataset

use of org.jfree.data.xy.OHLCDataset in project jgnash by ccavanaugh.

the class SecurityItemLabelGenerator method generateToolTip.

/**
     * Generates a tooltip text item for a particular item within a series.
     *
     * @param dataset  the dataset.
     * @param series  the series (zero-based index).
     * @param item  the item (zero-based index).
     *
     * @return The tooltip text.
     */
@Override
public String generateToolTip(final XYDataset dataset, final int series, final int item) {
    String result = null;
    if (dataset instanceof OHLCDataset) {
        OHLCDataset d = (OHLCDataset) dataset;
        Number close = d.getClose(series, item);
        Number x = d.getX(series, item);
        if (x != null) {
            Date date = new Date(x.longValue());
            result = dateLabel + " " + dateFormatter.format(date);
            if (close != null) {
                result = result + " " + closeLabel + "  " + numberFormatter.format(close.doubleValue());
            }
        }
    }
    return result;
}
Also used : OHLCDataset(org.jfree.data.xy.OHLCDataset) Date(java.util.Date)

Aggregations

OHLCDataset (org.jfree.data.xy.OHLCDataset)3 XYPlot (org.jfree.chart.plot.XYPlot)2 Point (java.awt.Point)1 Point2D (java.awt.geom.Point2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Date (java.util.Date)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 JFreeChart (org.jfree.chart.JFreeChart)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 CombinedDomainXYPlot (org.jfree.chart.plot.CombinedDomainXYPlot)1 Plot (org.jfree.chart.plot.Plot)1 PlotRenderingInfo (org.jfree.chart.plot.PlotRenderingInfo)1 CandlestickRenderer (org.jfree.chart.renderer.xy.CandlestickRenderer)1 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)1 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)1 XYDataset (org.jfree.data.xy.XYDataset)1 RectangleEdge (org.jfree.ui.RectangleEdge)1 TimeSeries (org.ta4j.core.TimeSeries)1