Search in sources :

Example 1 with PlotRenderingInfo

use of org.jfree.chart.plot.PlotRenderingInfo in project jfreechart-fx by jfree.

the class ChartCanvas method findDataArea.

/**
 * Returns the data area (the area inside the axes) for the plot or subplot.
 *
 * @param point  the selection point (for subplot selection).
 *
 * @return The data area.
 */
public Rectangle2D findDataArea(Point2D point) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle2D result;
    if (plotInfo.getSubplotCount() == 0) {
        result = plotInfo.getDataArea();
    } else {
        int subplotIndex = plotInfo.getSubplotIndex(point);
        if (subplotIndex == -1) {
            return null;
        }
        result = plotInfo.getSubplotInfo(subplotIndex).getDataArea();
    }
    return result;
}
Also used : PlotRenderingInfo(org.jfree.chart.plot.PlotRenderingInfo) Rectangle2D(java.awt.geom.Rectangle2D)

Example 2 with PlotRenderingInfo

use of org.jfree.chart.plot.PlotRenderingInfo in project jfreechart-fx by jfree.

the class PanHandlerFX method handleMouseDragged.

/**
 * Handles a mouse dragged event by calculating the distance panned and
 * updating the axes accordingly.
 *
 * @param canvas  the JavaFX canvas ({@code null} not permitted).
 * @param e  the mouse event ({@code null} not permitted).
 */
@Override
public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) {
    if (this.panLast == null) {
        // handle panning if we have a start point else unregister
        canvas.clearLiveHandler();
        return;
    }
    JFreeChart chart = canvas.getChart();
    double dx = e.getX() - this.panLast.getX();
    double dy = e.getY() - this.panLast.getY();
    if (dx == 0.0 && dy == 0.0) {
        return;
    }
    double wPercent = -dx / this.panW;
    double hPercent = dy / this.panH;
    boolean old = chart.getPlot().isNotify();
    chart.getPlot().setNotify(false);
    Pannable p = (Pannable) chart.getPlot();
    PlotRenderingInfo info = canvas.getRenderingInfo().getPlotInfo();
    if (p.getOrientation().isVertical()) {
        p.panDomainAxes(wPercent, info, this.panLast);
        p.panRangeAxes(hPercent, info, this.panLast);
    } else {
        p.panDomainAxes(hPercent, info, this.panLast);
        p.panRangeAxes(wPercent, info, this.panLast);
    }
    this.panLast = new Point2D.Double(e.getX(), e.getY());
    chart.getPlot().setNotify(old);
}
Also used : Pannable(org.jfree.chart.plot.Pannable) PlotRenderingInfo(org.jfree.chart.plot.PlotRenderingInfo) Point2D(java.awt.geom.Point2D) JFreeChart(org.jfree.chart.JFreeChart)

Example 3 with PlotRenderingInfo

use of org.jfree.chart.plot.PlotRenderingInfo in project jfreechart-fx by jfree.

the class ZoomHandlerFX method handleMouseReleased.

@Override
public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) {
    Plot p = canvas.getChart().getPlot();
    if (!(p instanceof Zoomable)) {
        return;
    }
    boolean hZoom, vZoom;
    Zoomable z = (Zoomable) p;
    if (z.getOrientation().isHorizontal()) {
        hZoom = z.isRangeZoomable();
        vZoom = z.isDomainZoomable();
    } else {
        hZoom = z.isDomainZoomable();
        vZoom = z.isRangeZoomable();
    }
    boolean zoomTrigger1 = hZoom && Math.abs(e.getX() - this.startPoint.getX()) >= 10;
    boolean zoomTrigger2 = vZoom && Math.abs(e.getY() - this.startPoint.getY()) >= 10;
    if (zoomTrigger1 || zoomTrigger2) {
        Point2D endPoint = new Point2D.Double(e.getX(), e.getY());
        PlotRenderingInfo pri = canvas.getRenderingInfo().getPlotInfo();
        if ((hZoom && (e.getX() < this.startPoint.getX())) || (vZoom && (e.getY() < this.startPoint.getY()))) {
            boolean saved = p.isNotify();
            p.setNotify(false);
            z.zoomDomainAxes(0, pri, endPoint);
            z.zoomRangeAxes(0, pri, endPoint);
            p.setNotify(saved);
        } else {
            double x = this.startPoint.getX();
            double y = this.startPoint.getY();
            double w = e.getX() - x;
            double h = e.getY() - y;
            Rectangle2D dataArea = canvas.findDataArea(this.startPoint);
            double maxX = dataArea.getMaxX();
            double maxY = dataArea.getMaxY();
            // otherwise both are true
            if (!vZoom) {
                y = dataArea.getMinY();
                w = Math.min(w, maxX - this.startPoint.getX());
                h = dataArea.getHeight();
            } else if (!hZoom) {
                x = dataArea.getMinX();
                w = dataArea.getWidth();
                h = Math.min(h, maxY - this.startPoint.getY());
            } else {
                w = Math.min(w, maxX - this.startPoint.getX());
                h = Math.min(h, maxY - this.startPoint.getY());
            }
            Rectangle2D zoomArea = new Rectangle2D.Double(x, y, w, h);
            boolean saved = p.isNotify();
            p.setNotify(false);
            double pw0 = percentW(x, dataArea);
            double pw1 = percentW(x + w, dataArea);
            double ph0 = percentH(y, dataArea);
            double ph1 = percentH(y + h, dataArea);
            PlotRenderingInfo info = this.viewer.getRenderingInfo().getPlotInfo();
            if (z.getOrientation().isVertical()) {
                z.zoomDomainAxes(pw0, pw1, info, endPoint);
                z.zoomRangeAxes(1 - ph1, 1 - ph0, info, endPoint);
            } else {
                z.zoomRangeAxes(pw0, pw1, info, endPoint);
                z.zoomDomainAxes(1 - ph1, 1 - ph0, info, endPoint);
            }
            p.setNotify(saved);
        }
    }
    this.viewer.hideZoomRectangle();
    this.startPoint = null;
    canvas.clearLiveHandler();
}
Also used : Point2D(java.awt.geom.Point2D) PlotRenderingInfo(org.jfree.chart.plot.PlotRenderingInfo) Plot(org.jfree.chart.plot.Plot) Rectangle2D(java.awt.geom.Rectangle2D) Zoomable(org.jfree.chart.plot.Zoomable)

Example 4 with PlotRenderingInfo

use of org.jfree.chart.plot.PlotRenderingInfo 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 5 with PlotRenderingInfo

use of org.jfree.chart.plot.PlotRenderingInfo in project n2a by frothga.

the class Raster method createChart.

public JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart chart = ChartFactory.createScatterPlot(// chart title
    null, // x axis label
    null, // y axis label
    null, // data
    dataset, PlotOrientation.VERTICAL, // include legend
    false, // tooltips
    true, // urls
    false);
    XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);
    plot.setDomainPannable(true);
    plot.setRangePannable(true);
    plot.setRenderer(new XYDotRenderer() {

        public void drawItem(java.awt.Graphics2D g2, XYItemRendererState state, java.awt.geom.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;
            int dotWidth = 1;
            double rasterLines = rangeAxis.getRange().getLength();
            int pixels = g2.getClipBounds().height;
            double height = pixels / rasterLines;
            if (height > 10)
                height -= 2;
            else if (height > 2)
                height -= 1;
            int dotHeight = (int) Math.min(20, Math.max(1, Math.floor(height)));
            double x = dataset.getXValue(series, item);
            double y = dataset.getYValue(series, item);
            if (Double.isNaN(y))
                return;
            double adjx = (dotWidth - 1) / 2.0;
            double adjy = (dotHeight - 1) / 2.0;
            RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
            RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
            double transX = domainAxis.valueToJava2D(x, dataArea, xAxisLocation) - adjx;
            double transY = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation) - adjy;
            g2.setPaint(Color.black);
            PlotOrientation orientation = plot.getOrientation();
            if (orientation == PlotOrientation.HORIZONTAL)
                g2.fillRect((int) transY, (int) transX, dotHeight, dotWidth);
            else
                g2.fillRect((int) transX, (int) transY, dotWidth, dotHeight);
            int datasetIndex = plot.indexOf(dataset);
            updateCrosshairValues(crosshairState, x, y, datasetIndex, transX, transY, orientation);
        }
    });
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    // Integer units only
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    return chart;
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) NumberAxis(org.jfree.chart.axis.NumberAxis) PlotRenderingInfo(org.jfree.chart.plot.PlotRenderingInfo) XYDotRenderer(org.jfree.chart.renderer.xy.XYDotRenderer) CrosshairState(org.jfree.chart.plot.CrosshairState) JFreeChart(org.jfree.chart.JFreeChart) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) XYItemRendererState(org.jfree.chart.renderer.xy.XYItemRendererState) XYDataset(org.jfree.data.xy.XYDataset) RectangleEdge(org.jfree.chart.ui.RectangleEdge)

Aggregations

PlotRenderingInfo (org.jfree.chart.plot.PlotRenderingInfo)7 Point2D (java.awt.geom.Point2D)4 Rectangle2D (java.awt.geom.Rectangle2D)4 JFreeChart (org.jfree.chart.JFreeChart)3 ValueAxis (org.jfree.chart.axis.ValueAxis)3 Plot (org.jfree.chart.plot.Plot)3 XYPlot (org.jfree.chart.plot.XYPlot)3 XYDataset (org.jfree.data.xy.XYDataset)3 NumberAxis (org.jfree.chart.axis.NumberAxis)2 CrosshairState (org.jfree.chart.plot.CrosshairState)2 PlotOrientation (org.jfree.chart.plot.PlotOrientation)2 XYItemRendererState (org.jfree.chart.renderer.xy.XYItemRendererState)2 RectangleEdge (org.jfree.ui.RectangleEdge)2 Graphics2D (java.awt.Graphics2D)1 Paint (java.awt.Paint)1 Point (java.awt.Point)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 ChartRenderingInfo (org.jfree.chart.ChartRenderingInfo)1 EntityCollection (org.jfree.chart.entity.EntityCollection)1