Example usage for org.jfree.chart.plot PlotRenderingInfo getDataArea

List of usage examples for org.jfree.chart.plot PlotRenderingInfo getDataArea

Introduction

In this page you can find the example usage for org.jfree.chart.plot PlotRenderingInfo getDataArea.

Prototype

public Rectangle2D getDataArea() 

Source Link

Document

Returns the plot's data area (in Java2D space).

Usage

From source file:ec.util.chart.swing.Charts.java

@Nullable
public static LegendItemEntity getSeriesForPoint(@Nonnull Point pt, @Nonnull ChartPanel cp) {

    final double chartX;
    final double chartY;
    final Rectangle2D plotArea;
    final XYPlot plot;
    {/*from   w w w  . j a  v  a  2  s . c  o  m*/
        // Let's find the X and Y values of the clicked point
        Point2D p = cp.translateScreenToJava2D(pt);
        chartX = p.getX();
        chartY = p.getY();
        // Let's find plotArea and plot
        XYPlot tmpPlot = cp.getChart().getXYPlot();
        PlotRenderingInfo plotInfo = cp.getChartRenderingInfo().getPlotInfo();
        if (tmpPlot instanceof CombinedDomainXYPlot) {
            int subplotIndex = plotInfo.getSubplotIndex(p);
            if (subplotIndex == -1) {
                return null;
            }
            plotArea = plotInfo.getSubplotInfo(subplotIndex).getDataArea();
            plot = ((CombinedDomainXYPlot) tmpPlot).findSubplot(plotInfo, p);
        } else {
            plotArea = plotInfo.getDataArea();
            plot = tmpPlot;
        }
    }

    // Let's avoid unnecessary computation
    final ValueAxis domainAxis = plot.getDomainAxis();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    final double x = domainAxis.java2DToValue(chartX, plotArea, domainAxisEdge);

    final double sensitivity = TOL;
    double distanceClickSeries = TOL + 1;

    Entry<XYDataset, Comparable> result = null;

    // For each series in each datasets
    for (XYDataset dataset : asDatasetList(plot)) {
        for (int series = 0; series < dataset.getSeriesCount(); series++) {
            // Index of the closest data item of the current series just left to the click
            int lp = getNearestLeftPoint(x, 0, dataset.getItemCount(series) - 1, series, dataset);

            try {
                // X and Y values of data items to the left and to the right
                double leftX = dataset.getXValue(series, lp);
                double leftY = dataset.getYValue(series, lp);
                double rightX = dataset.getXValue(series, lp + 1);
                double rightY = dataset.getYValue(series, lp + 1);

                double lx = domainAxis.valueToJava2D(leftX, plotArea, domainAxisEdge);
                double ly = rangeAxis.valueToJava2D(leftY, plotArea, rangeAxisEdge);
                double rx = domainAxis.valueToJava2D(rightX, plotArea, domainAxisEdge);
                double ry = rangeAxis.valueToJava2D(rightY, plotArea, rangeAxisEdge);

                // Distance to left point
                double distL = Point2D.distance(lx, ly, chartX, chartY);
                // Distance to right point
                double distR = Point2D.distance(rx, ry, chartX, chartY);
                // Average of both distances
                double distLRavg = (distL + distR) / 2d;
                // Distance to the segment between L and R
                //double distSeg = Line2D.ptSegDist(leftX, leftY, rightX, rightY, chartX, chartY);
                double distSeg = ptSegDist(lx, ly, rx, ry, chartX, chartY);

                // With a line renderer, this is probably a bit of overkill as
                // distSeg would be enough, but it becomes more reliable to check all these
                // if using splines
                double tmp = Math.min(Math.min(distSeg, Math.min(distL, distR)), distLRavg);

                // Are we closer than the previous series?
                if (tmp < sensitivity && tmp < distanceClickSeries) {
                    distanceClickSeries = tmp;
                    result = new SimpleEntry<>(dataset, dataset.getSeriesKey(series));
                }
            } catch (Exception ex) {
                /*
                 * An exception might happen when some series have less data
                 * than others, catching the the exception here will simply rule
                 * them out from the detection on this click
                 */
            }
        }
    }

    return result != null ? createFakeLegendItemEntity(result.getKey(), result.getValue()) : null;
}

From source file:de.hs.mannheim.modUro.controller.diagram.fx.interaction.ScrollHandlerFX.java

/**
 * Handle the case where a plot implements the {@link Zoomable} interface.
 *
 * @param zoomable  the zoomable plot./*  w w  w  .j  a va2  s  .  c  o m*/
 * @param e  the mouse wheel event.
 */
private void handleZoomable(ChartCanvas canvas, Zoomable zoomable, ScrollEvent e) {
    // don't zoom unless the mouse pointer is in the plot's data area
    ChartRenderingInfo info = canvas.getRenderingInfo();
    PlotRenderingInfo pinfo = info.getPlotInfo();
    Point2D p = new Point2D.Double(e.getX(), e.getY());
    if (pinfo.getDataArea().contains(p)) {
        Plot plot = (Plot) zoomable;
        // do not notify while zooming each axis
        boolean notifyState = plot.isNotify();
        plot.setNotify(false);
        int clicks = (int) e.getDeltaY();
        double zf = 1.0 + this.zoomFactor;
        if (clicks < 0) {
            zf = 1.0 / zf;
        }
        if (true) { //this.chartPanel.isDomainZoomable()) {
            zoomable.zoomDomainAxes(zf, pinfo, p, true);
        }
        if (true) { //this.chartPanel.isRangeZoomable()) {
            zoomable.zoomRangeAxes(zf, pinfo, p, true);
        }
        plot.setNotify(notifyState); // this generates the change event too
    }
}

From source file:slash.navigation.converter.gui.profileview.LazyToolTipChartPanel.java

protected String getTooltipAtPoint(Point point) {
    XYPlot plot = (XYPlot) getChart().getPlot();
    PlotRenderingInfo info = getChartRenderingInfo().getPlotInfo();
    double x0 = point.getX();
    double x1 = x0 - 2 * getScaleX();
    double x2 = x0 + 4 * getScaleX();

    ValueAxis domainAxis = plot.getDomainAxis();
    Rectangle2D screenArea = scale(info.getDataArea());
    double tx1 = domainAxis.java2DToValue(x1, screenArea, BOTTOM);
    double tx2 = domainAxis.java2DToValue(x2, screenArea, BOTTOM);

    for (int datasetIndex = 0; datasetIndex < plot.getDatasetCount(); datasetIndex++) {
        XYDataset dataset = plot.getDataset(datasetIndex);
        for (int seriesIndex = 0; seriesIndex < dataset.getSeriesCount(); seriesIndex++) {
            int itemCount = dataset.getItemCount(seriesIndex);
            for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
                double xValue = dataset.getXValue(seriesIndex, itemIndex);
                if (tx1 < xValue && xValue < tx2)
                    return toolTipGenerator.generateToolTip(dataset, seriesIndex, itemIndex);
            }/*w ww.j a v a  2  s.  c  o  m*/
        }
    }
    return null;
}

From source file:peakml.util.jfreechart.FastSpectrumPlot.java

public void zoomDomainAxes(double factor, PlotRenderingInfo state, Point2D source, boolean useAnchor) {
    if (useAnchor) {
        // get the source coordinate - this plot has always a VERTICAL orientation
        double sourceX = source.getX();
        double anchorX = xaxis.java2DToValue(sourceX, state.getDataArea(), RectangleEdge.BOTTOM);

        xaxis.resizeRange2(factor, anchorX);
    } else {/*  w ww .j a va 2  s .  com*/
        xaxis.resizeRange(factor);
    }
}

From source file:peakml.util.jfreechart.FastSpectrumPlot.java

public void zoomRangeAxes(double factor, PlotRenderingInfo state, Point2D source, boolean useAnchor) {
    if (useAnchor) {
        // get the source coordinate - this plot has always a VERTICAL orientation
        double sourceY = source.getY();
        double anchorY = yaxis.java2DToValue(sourceY, state.getDataArea(), RectangleEdge.LEFT);

        yaxis.resizeRange2(factor, anchorY);
    } else {/*from w  w w.ja  va2  s  .co  m*/
        yaxis.resizeRange(factor);
    }
}

From source file:MWC.GUI.JFreeChart.StepperXYPlot.java

/**
 * Draws the XY plot on a Java 2D graphics device (such as the screen or a
 * printer), together with a current time marker
 * <P>/*w w w. ja  va  2  s  .  c o m*/
 * XYPlot relies on an XYItemRenderer to draw each item in the plot. This
 * allows the visual representation of the data to be changed easily.
 * <P>
 * The optional info argument collects information about the rendering of the
 * plot (dimensions, tooltip information etc). Just pass in null if you do not
 * need this information.
 * 
 * @param g2
 *          The graphics device.
 * @param plotArea
 *          The area within which the plot (including axis labels) should be
 *          drawn.
 * @param info
 *          Collects chart drawing information (null permitted).
 */
public final void draw(final Graphics2D g2, final Rectangle2D plotArea, final Point2D anchor,
        final PlotState state, final PlotRenderingInfo info) {
    super.draw(g2, plotArea, anchor, state, info);

    // do we want to view the line?
    if (!_showLine)
        return;

    // do we have a time?
    if (_currentTime != null) {
        // find the screen area for the dataset
        final Rectangle2D dataArea = info.getDataArea();

        // determine the time we are plotting the line at
        long theTime = _currentTime.getMicros();

        // hmmm, how do we format the date
        final CanBeRelativeToTimeStepper axis = (CanBeRelativeToTimeStepper) this.getDomainAxis();

        // are we working in relative time mode?
        if (axis.isRelativeTimes()) {
            if (_myStepper != null) {
                // yes, we now need to offset the time
                theTime = theTime - _myStepper.getTimeZero().getMicros();
            }
        }

        // hmm, see if we are wroking with a date or number axis
        double linePosition = 0;
        if (axis instanceof DateAxis) {
            // ok, now scale the time to graph units
            final DateAxis dateAxis = (DateAxis) axis;

            // find the new x value
            linePosition = dateAxis.dateToJava2D(new Date(theTime / 1000), dataArea, this.getDomainAxisEdge());

            if (_resetAxes) {
                dateAxis.setAutoRange(true);
                _resetAxes = false;
            }

            if (isGrowWithTime()) {
                final long endMillis = theTime / 1000;
                long startMillis;

                if (_fixedDuration != null) {
                    startMillis = endMillis - _fixedDuration.getMillis();
                } else {
                    startMillis = (long) dateAxis.getLowerBound();
                }

                final Date startDate = new Date(startMillis);
                final Date endDate = new Date(endMillis);

                dateAxis.setRange(startDate, endDate);
            } else {
            }

        } else {
            if (axis instanceof NumberAxis) {
                final NumberAxis numberAxis = (NumberAxis) axis;
                linePosition = numberAxis.valueToJava2D(theTime, dataArea, this.getDomainAxisEdge());

                if (isGrowWithTime())
                    numberAxis.setRange(numberAxis.getRange().getLowerBound(), theTime);
                else {
                    if (_resetAxes) {
                        numberAxis.setAutoRange(true);
                        _resetAxes = false;
                    }
                }

            }
        }

        // ok, finally draw the line - if we're not showing the growing plot
        if (!isGrowWithTime())
            plotStepperLine(g2, linePosition, dataArea);

    }
}

From source file:com.epiq.bitshark.ui.FrequencyDomainPanel.java

/**
 * /*from ww w  . j a v a  2 s  . co m*/
 * @param point
 */
protected void updateMouseMarker(Point point) {

    double xValue = 0;

    if (!markerLocked) {
        double pointerX = point.getX();

        PlotRenderingInfo renderingInfo = chartPanel.getChartRenderingInfo().getPlotInfo();
        Rectangle2D dataArea = renderingInfo.getDataArea();
        Rectangle2D plotArea = renderingInfo.getPlotArea();
        RectangleEdge domainEdge = plot.getDomainAxisEdge();

        xValue = frequencyAxis.java2DToValue(pointerX, dataArea, domainEdge);

        previousMarkerX = xValue;

    } else {
        xValue = previousMarkerX;
    }

    int bin = (int) Math.round(xValue);

    try {
        XYDataItem item = series.getDataItem(bin);
        if (item != null) {
            double yValue = item.getYValue();
            markerAnnotation.setXY(xValue, yValue);
            markerAnnotation.setVisible(true);

            plot.setDomainCrosshairValue(xValue);
            plot.setDomainCrosshairVisible(true);

            markerTitle.setText("Frequency: " + binToFrequencyStr(bin) + " \nPower: "
                    + String.format("%5.1f", PowerAxis.valueToDb(yValue)) + " dB");

            markerTitle.setVisible(true);
        } else {
            markerAnnotation.setVisible(false);
            plot.setDomainCrosshairVisible(false);
        }
    } catch (Exception e) {
        markerAnnotation.setVisible(false);
        plot.setDomainCrosshairVisible(false);
    }
}

From source file:de.hs.mannheim.modUro.controller.diagram.fx.ChartCanvas.java

/**
 * 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.// w ww  .  j av a 2 s .  com
 */
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;
}

From source file:org.talend.dataprofiler.chart.util.ToolTipChartComposite.java

/**
 * 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.
 * //from   ww  w .  j ava2s.com
 * @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;
}

From source file:de.laures.cewolf.jfree.ThermometerPlot.java

/**
 * Multiplies the range on the range axis/axes by the specified factor.
 *
 * @param factor  the zoom factor./*from   w  w w .j  av  a  2  s.  c om*/
 * @param state  the plot state.
 * @param source  the source point.
 * @param useAnchor  a flag that controls whether or not the source point is used for the zoom anchor.
 *
 * @since 1.0.7
 */
public void zoomRangeAxes(double factor, PlotRenderingInfo state, Point2D source, boolean useAnchor) {
    double anchorY = this.getRangeAxis().java2DToValue(source.getY(), state.getDataArea(), RectangleEdge.LEFT);
    this.rangeAxis.resizeRange(factor, anchorY);
}