Search in sources :

Example 1 with RectangleEdge

use of org.jfree.chart.ui.RectangleEdge 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)

Example 2 with RectangleEdge

use of org.jfree.chart.ui.RectangleEdge in project jfreechart-fx by jfree.

the class CrosshairOverlayFX method paintOverlay.

@Override
public void paintOverlay(Graphics2D g2, ChartCanvas chartCanvas) {
    if (chartCanvas.getRenderingInfo() == null) {
        return;
    }
    Shape savedClip = g2.getClip();
    Rectangle2D dataArea = chartCanvas.getRenderingInfo().getPlotInfo().getDataArea();
    g2.clip(dataArea);
    JFreeChart chart = chartCanvas.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ValueAxis xAxis = plot.getDomainAxis();
    RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
    Iterator iterator = getDomainCrosshairs().iterator();
    while (iterator.hasNext()) {
        Crosshair ch = (Crosshair) iterator.next();
        if (ch.isVisible()) {
            double x = ch.getValue();
            double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                drawVerticalCrosshair(g2, dataArea, xx, ch);
            } else {
                drawHorizontalCrosshair(g2, dataArea, xx, ch);
            }
        }
    }
    ValueAxis yAxis = plot.getRangeAxis();
    RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
    iterator = getRangeCrosshairs().iterator();
    while (iterator.hasNext()) {
        Crosshair ch = (Crosshair) iterator.next();
        if (ch.isVisible()) {
            double y = ch.getValue();
            double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                drawHorizontalCrosshair(g2, dataArea, yy, ch);
            } else {
                drawVerticalCrosshair(g2, dataArea, yy, ch);
            }
        }
    }
    g2.setClip(savedClip);
}
Also used : Shape(java.awt.Shape) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) Crosshair(org.jfree.chart.plot.Crosshair) Rectangle2D(java.awt.geom.Rectangle2D) Iterator(java.util.Iterator) JFreeChart(org.jfree.chart.JFreeChart) RectangleEdge(org.jfree.chart.ui.RectangleEdge)

Aggregations

JFreeChart (org.jfree.chart.JFreeChart)2 ValueAxis (org.jfree.chart.axis.ValueAxis)2 XYPlot (org.jfree.chart.plot.XYPlot)2 RectangleEdge (org.jfree.chart.ui.RectangleEdge)2 Shape (java.awt.Shape)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Iterator (java.util.Iterator)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 Crosshair (org.jfree.chart.plot.Crosshair)1 CrosshairState (org.jfree.chart.plot.CrosshairState)1 PlotOrientation (org.jfree.chart.plot.PlotOrientation)1 PlotRenderingInfo (org.jfree.chart.plot.PlotRenderingInfo)1 XYDotRenderer (org.jfree.chart.renderer.xy.XYDotRenderer)1 XYItemRendererState (org.jfree.chart.renderer.xy.XYItemRendererState)1 XYDataset (org.jfree.data.xy.XYDataset)1