Example usage for org.jfree.chart.axis ValueAxis valueToJava2D

List of usage examples for org.jfree.chart.axis ValueAxis valueToJava2D

Introduction

In this page you can find the example usage for org.jfree.chart.axis ValueAxis valueToJava2D.

Prototype

public abstract double valueToJava2D(double value, Rectangle2D area, RectangleEdge edge);

Source Link

Document

Converts a data value to a coordinate in Java2D space, assuming that the axis runs along one edge of the specified dataArea.

Usage

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Draws the quadrants.// www.j  a  v  a2s  .c om
 *
 * @param g2  the graphics device.
 * @param area  the area.
 */
protected void drawQuadrants(Graphics2D g2, Rectangle2D area) {

    //  0 | 1
    //  --+--
    //  2 | 3
    boolean somethingToDraw = false;

    ValueAxis xAxis = getDomainAxis();
    double x = this.quadrantOrigin.getX();
    double xx = xAxis.valueToJava2D(x, area, getDomainAxisEdge());

    ValueAxis yAxis = getRangeAxis();
    double y = this.quadrantOrigin.getY();
    double yy = yAxis.valueToJava2D(y, area, getRangeAxisEdge());

    double xmin = xAxis.getLowerBound();
    double xxmin = xAxis.valueToJava2D(xmin, area, getDomainAxisEdge());

    double xmax = xAxis.getUpperBound();
    double xxmax = xAxis.valueToJava2D(xmax, area, getDomainAxisEdge());

    double ymin = yAxis.getLowerBound();
    double yymin = yAxis.valueToJava2D(ymin, area, getRangeAxisEdge());

    double ymax = yAxis.getUpperBound();
    double yymax = yAxis.valueToJava2D(ymax, area, getRangeAxisEdge());

    Rectangle2D[] r = new Rectangle2D[] { null, null, null, null };
    if (this.quadrantPaint[0] != null) {
        if ((x > xmin) && (y < ymax)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[0] = new Rectangle2D.Double(Math.min(yymax, yy), Math.min(xxmin, xx), Math.abs(yy - yymax),
                        Math.abs(xx - xxmin));
            } else { // PlotOrientation.VERTICAL
                r[0] = new Rectangle2D.Double(Math.min(xxmin, xx), Math.min(yymax, yy), Math.abs(xx - xxmin),
                        Math.abs(yy - yymax));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[1] != null) {
        if ((x < xmax) && (y < ymax)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[1] = new Rectangle2D.Double(Math.min(yymax, yy), Math.min(xxmax, xx), Math.abs(yy - yymax),
                        Math.abs(xx - xxmax));
            } else { // PlotOrientation.VERTICAL
                r[1] = new Rectangle2D.Double(Math.min(xx, xxmax), Math.min(yymax, yy), Math.abs(xx - xxmax),
                        Math.abs(yy - yymax));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[2] != null) {
        if ((x > xmin) && (y > ymin)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[2] = new Rectangle2D.Double(Math.min(yymin, yy), Math.min(xxmin, xx), Math.abs(yy - yymin),
                        Math.abs(xx - xxmin));
            } else { // PlotOrientation.VERTICAL
                r[2] = new Rectangle2D.Double(Math.min(xxmin, xx), Math.min(yymin, yy), Math.abs(xx - xxmin),
                        Math.abs(yy - yymin));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[3] != null) {
        if ((x < xmax) && (y > ymin)) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[3] = new Rectangle2D.Double(Math.min(yymin, yy), Math.min(xxmax, xx), Math.abs(yy - yymin),
                        Math.abs(xx - xxmax));
            } else { // PlotOrientation.VERTICAL
                r[3] = new Rectangle2D.Double(Math.min(xx, xxmax), Math.min(yymin, yy), Math.abs(xx - xxmax),
                        Math.abs(yy - yymin));
            }
            somethingToDraw = true;
        }
    }
    if (somethingToDraw) {
        Composite originalComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getBackgroundAlpha()));
        for (int i = 0; i < 4; i++) {
            if ((this.quadrantPaint[i] != null) && (r[i] != null)) {
                g2.setPaint(this.quadrantPaint[i]);
                g2.fill(r[i]);
            }
        }
        g2.setComposite(originalComposite);
    }
}

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Utility method for drawing a vertical line on the data area of the plot.
 *
 * @param g2  the graphics device.//from  w  w w .  j  a v a2  s. c  o m
 * @param dataArea  the data area.
 * @param value  the coordinate, where to draw the line.
 * @param stroke  the stroke to use.
 * @param paint  the paint to use.
 */
protected void drawVerticalLine(Graphics2D g2, Rectangle2D dataArea, double value, Stroke stroke, Paint paint) {

    ValueAxis axis = getDomainAxis();
    if (getOrientation() == PlotOrientation.HORIZONTAL) {
        axis = getRangeAxis();
    }
    if (axis.getRange().contains(value)) {
        double xx = axis.valueToJava2D(value, dataArea, RectangleEdge.BOTTOM);
        Line2D line = new Line2D.Double(xx, dataArea.getMinY(), xx, dataArea.getMaxY());
        g2.setStroke(stroke);
        g2.setPaint(paint);
        g2.draw(line);
    }

}

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Utility method for drawing a horizontal line across the data area of the
 * plot.//from  www. jav a  2  s.c  o m
 *
 * @param g2  the graphics device.
 * @param dataArea  the data area.
 * @param value  the coordinate, where to draw the line.
 * @param stroke  the stroke to use.
 * @param paint  the paint to use.
 */
protected void drawHorizontalLine(Graphics2D g2, Rectangle2D dataArea, double value, Stroke stroke,
        Paint paint) {

    ValueAxis axis = getRangeAxis();
    if (getOrientation() == PlotOrientation.HORIZONTAL) {
        axis = getDomainAxis();
    }
    if (axis.getRange().contains(value)) {
        double yy = axis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
        Line2D line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy);
        g2.setStroke(stroke);
        g2.setPaint(paint);
        g2.draw(line);
    }

}

From source file:org.pentaho.plugin.jfreereport.reportcharts.backport.StackedAreaRenderer.java

/**
 * Draw a single data item./*from w  ww  .  j  a  v a2  s .c o m*/
 *
 * @param g2         the graphics device.
 * @param state      the renderer state.
 * @param dataArea   the data plot area.
 * @param plot       the plot.
 * @param domainAxis the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset    the data.
 * @param row        the row index (zero-based).
 * @param column     the column index (zero-based).
 * @param pass       the pass index.
 */
public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea,
        final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis,
        final CategoryDataset dataset, final int row, final int column, final int pass) {

    if (!isSeriesVisible(row)) {
        return;
    }

    if ((pass == 1) && !isItemLabelVisible(row, column)) {
        return;
    }

    // setup for collecting optional entity info...
    Shape entityArea = null;
    final EntityCollection entities = state.getEntityCollection();

    double y1 = 0.0;
    Number n = dataset.getValue(row, column);
    if (n != null) {
        y1 = n.doubleValue();
        if (this.renderAsPercentages) {
            final double total = DataUtilities.calculateColumnTotal(dataset, column);
            y1 = y1 / total;
        }
    }
    final double[] stack1 = getStackValues(dataset, row, column);

    // leave the y values (y1, y0) untranslated as it is going to be be
    // stacked up later by previous series values, after this it will be
    // translated.
    double xx1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    // get the previous point and the next point so we can calculate a
    // "hot spot" for the area (used by the chart entity)...
    double y0 = 0.0;
    n = dataset.getValue(row, Math.max(column - 1, 0));
    if (n != null) {
        y0 = n.doubleValue();
        if (this.renderAsPercentages) {
            final double total = DataUtilities.calculateColumnTotal(dataset, Math.max(column - 1, 0));
            y0 = y0 / total;
        }
    }
    final double[] stack0 = getStackValues(dataset, row, Math.max(column - 1, 0));

    // FIXME: calculate xx0
    double xx0 = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    final int itemCount = dataset.getColumnCount();
    double y2 = 0.0;
    n = dataset.getValue(row, Math.min(column + 1, itemCount - 1));
    if (n != null) {
        y2 = n.doubleValue();
        if (this.renderAsPercentages) {
            final double total = DataUtilities.calculateColumnTotal(dataset,
                    Math.min(column + 1, itemCount - 1));
            y2 = y2 / total;
        }
    }
    final double[] stack2 = getStackValues(dataset, row, Math.min(column + 1, itemCount - 1));

    double xx2 = domainAxis.getCategoryEnd(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    // This gets rid of the white lines between most category values
    // Doug Moran - Hitachi Vantara
    xx0 = Math.round(xx0);
    xx1 = Math.round(xx1);
    xx2 = Math.round(xx2);

    // FIXME: calculate xxLeft and xxRight
    final double xxLeft = xx0;
    final double xxRight = xx2;

    final double[] stackLeft = averageStackValues(stack0, stack1);
    final double[] stackRight = averageStackValues(stack1, stack2);
    final double[] adjStackLeft = adjustedStackValues(stack0, stack1);
    final double[] adjStackRight = adjustedStackValues(stack1, stack2);

    final float transY1;

    final RectangleEdge edge1 = plot.getRangeAxisEdge();

    final GeneralPath left = new GeneralPath();
    final GeneralPath right = new GeneralPath();
    if (y1 >= 0.0) { // handle positive value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[1], dataArea, edge1);
        final float transStack1 = (float) rangeAxis.valueToJava2D(stack1[1], dataArea, edge1);
        final float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[1], dataArea, edge1);

        // LEFT POLYGON
        if (y0 >= 0.0) {
            final double yleft = (y0 + y1) / 2.0 + stackLeft[1];
            final float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            left.moveTo((float) xx1, transY1);
            left.lineTo((float) xx1, transStack1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.lineTo((float) xxLeft, transYLeft);
            left.closePath();
        } else {
            left.moveTo((float) xx1, transStack1);
            left.lineTo((float) xx1, transY1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.closePath();
        }

        final float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[1], dataArea, edge1);
        // RIGHT POLYGON
        if (y2 >= 0.0) {
            final double yright = (y1 + y2) / 2.0 + stackRight[1];
            final float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transYRight);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        } else {
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        }
    } else { // handle negative value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[0], dataArea, edge1);
        final float transStack1 = (float) rangeAxis.valueToJava2D(stack1[0], dataArea, edge1);
        final float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[0], dataArea, edge1);

        // LEFT POLYGON
        if (y0 >= 0.0) {
            left.moveTo((float) xx1, transStack1);
            left.lineTo((float) xx1, transY1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.clone();
        } else {
            final double yleft = (y0 + y1) / 2.0 + stackLeft[0];
            final float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            left.moveTo((float) xx1, transY1);
            left.lineTo((float) xx1, transStack1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.lineTo((float) xxLeft, transYLeft);
            left.closePath();
        }
        final float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[0], dataArea, edge1);

        // RIGHT POLYGON
        if (y2 >= 0.0) {
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        } else {
            final double yright = (y1 + y2) / 2.0 + stackRight[0];
            final float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transYRight);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        }
    }

    if (pass == 0) {
        final Paint itemPaint = getItemPaint(row, column);
        g2.setPaint(itemPaint);
        g2.fill(left);
        g2.fill(right);

        // add an entity for the item...
        if (entities != null) {
            final GeneralPath gp = new GeneralPath(left);
            gp.append(right, false);
            entityArea = gp;
            addItemEntity(entities, dataset, row, column, entityArea);
        }
    } else if (pass == 1) {
        drawItemLabel(g2, plot.getOrientation(), dataset, row, column, xx1, transY1, y1 < 0.0);
    }

}

From source file:gda.plots.TurboXYItemRenderer.java

/**
 * Draws the visual representation of a single data item. This mostly reproduces the code of StandardXYItemRenderer
 * but using the line by line information stored in the SimpleXYSeries instead of the series indexed information
 * stored in the Renderer itself./* w w  w  . j ava2 s. c  om*/
 * 
 * @param g2
 *            the graphics device.
 * @param state
 *            the renderer state.
 * @param dataArea
 *            the area within which the data is being drawn.
 * @param info
 *            collects information 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 crosshairState
 *            crosshair information for the plot ( <code>null</code> permitted).
 * @param pass
 *            the pass index.
 */
@Override
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) {

    if (_item > 0)
        return;
    SimpleXYSeries sxys = (SimpleXYSeries) ((SimpleXYSeriesCollection) dataset).getSeries(series);
    if (!sxys.isVisible()) {
        return;
    }

    PlotOrientation orientation = plot.getOrientation();
    g2.setPaint(sxys.getPaint());
    g2.setStroke(sxys.getStroke());

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();

    try {
        int x0 = -1; // the x position in pixels of the previous point
        int y0 = -1; // the y position in pixels of the previous point
        int x1 = -1; // the x position in pixels of the current point
        int y1 = -1; // the y position in pixels of the current point
        int xmin = (int) dataArea.getMinX();
        int xmax = (int) dataArea.getMaxX();
        int ymin = (int) dataArea.getMinY();
        int ymax = (int) dataArea.getMaxY();
        GeneralPath path = null;

        /*
         * To remove the time spent repeatedly calling domainAxis.valueToJava2D for linear axes use simple linear
         * maths
         */
        double xl = 0., mx = Double.NaN, cx = 0.;
        if (domainAxis instanceof SimpleNumberAxis) {
            xl = domainAxis.getRange().getLowerBound();
            mx = dataArea.getWidth() / (domainAxis.getRange().getUpperBound() - xl);
            cx = xmin;
        }
        double yl = 0., my = Double.NaN, cy = 0.;
        if (rangeAxis instanceof SimpleNumberAxis) {
            yl = rangeAxis.getRange().getLowerBound();
            my = -dataArea.getHeight() / (rangeAxis.getRange().getUpperBound() - yl);
            cy = ymax;
        }
        List<XYDataItem> list = sxys.getData();

        boolean MX_MY_NaN = Double.isNaN(mx) || Double.isNaN(my);
        Paint paint = sxys.getPaint();
        Stroke stroke = sxys.getStroke();
        Paint paint_symbol = sxys.getSymbolPaint();
        Stroke stroke_symbol = new BasicStroke();
        drawLines = sxys.isDrawLines();
        boolean filled = sxys.getFilled();
        Shape shape = sxys.getSymbol();
        boolean drawMarkers = sxys.isDrawMarkers() & shape != null;
        int tooltipThresholdCounts = -1; /* number of points to be shown below which markers are also to be drawn */
        if (drawLines && drawMarkers && shape != null && tooltipThreshold != 0) {
            Rectangle shapeBoundingBox = shape.getBounds();
            tooltipThresholdCounts = (int) dataArea.getWidth()
                    / (Math.max(1, shapeBoundingBox.width) * tooltipThreshold);
        }

        java.util.Vector<ddouble> markerPositions = null;
        Shape entityArea = null;
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }
        boolean prevLineAdded = false;
        // In case the iterator does not work then use the TODO comment iterator use and why not always
        // comment variables
        synchronized (list) {
            Iterator<XYDataItem> iter = list.iterator();
            /*
             * loop over all points calculating X1 and Y1. Store previous points positions into X0 and Y0 The
             * variable addThis determines if the current point is to be added to the path If previous line was
             * added that the current must be even if off the screen - but in this case the flag prevLineAdded is
             * set false so that the next does not have to be added.
             */
            for (int item = 0; iter.hasNext(); item++, x0 = x1, y0 = y1, x1 = -1, y1 = -1) {
                XYDataItem dataitem = iter.next();
                double x = dataitem.getX().doubleValue();
                double y = dataitem.getY().doubleValue();
                x = xValueTransformer.transformValue(x);

                x1 = MX_MY_NaN ? (int) domainAxis.valueToJava2D(x, dataArea, xAxisLocation)
                        : (int) ((x - xl) * mx + cx);
                y1 = MX_MY_NaN ? (int) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation)
                        : (int) ((y - yl) * my + cy);

                boolean addThis = true;
                if (item == 0) {
                    x0 = x1;
                    y0 = y1;
                    if ((x1 < xmin) || (x1 > xmax) || (y1 < ymin) || (y1 > ymax)) {
                        addThis = false;
                    }
                } else {
                    if (x1 == x0 && y1 == y0) {
                        addThis = false;
                    }
                    if ((x1 < xmin && x0 < xmin) || (x1 > xmax && x0 > xmax) || (y1 < ymin && y0 < ymin)
                            || (y1 > ymax && y0 > ymax)) {
                        if (prevLineAdded) {
                            path = addPointToLine(path, orientation, x1, y1);
                        }
                        addThis = false;
                    }
                }
                if (addThis) {
                    /*
                     * If the current point is to be added then ensure previous one is as well to prevent lines
                     * not crossing the edge of the screen
                     */
                    if (!prevLineAdded) {
                        path = addPointToLine(path, orientation, x0, y0);
                    }
                    path = addPointToLine(path, orientation, x1, y1);
                    prevLineAdded = true;
                }
                prevLineAdded = addThis;
                if (addThis && drawMarkers) {
                    if (markerPositions == null) {
                        markerPositions = new java.util.Vector<ddouble>();
                    }
                    markerPositions.add(new ddouble(item, x1, y1));
                    if (tooltipThresholdCounts != -1 && markerPositions.size() > tooltipThresholdCounts) {
                        drawMarkers = false;
                        markerPositions = null;
                    }
                }
            }
            if (path != null) {
                g2.setStroke(stroke);
                g2.setPaint(paint);
                g2.draw(path);
            }
            if (markerPositions != null) {
                if (drawMarkers) {
                    g2.setPaint(paint_symbol);
                    g2.setStroke(stroke_symbol);
                    for (ddouble dd : markerPositions) {
                        Shape shape_item = ShapeUtilities.createTranslatedShape(shape, dd.x, dd.y);
                        if (filled) {
                            g2.fill(shape_item);
                        } else {
                            g2.draw(shape_item);
                        }
                        entityArea = shape_item;
                        // add an entity for the item...
                        if (entities != null) {
                            addEntity(entities, entityArea, dataset, series, dd.item, dd.x, dd.y);
                        }
                    }
                    g2.setPaint(paint);
                    g2.setStroke(stroke);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}