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:org.schreibubi.JCombinations.jfreechart.XYLineAndShapeRendererExtended.java

/**
 * Draws a horizontal line across the chart to represent a 'range marker'.
 * /*from w w w . jav a  2 s.  co  m*/
 * @param g2
 *            the graphics device.
 * @param plot
 *            the plot.
 * @param rangeAxis
 *            the range axis.
 * @param marker
 *            the marker line.
 * @param dataArea
 *            the axis data area.
 */
@Override
public void drawRangeMarker(Graphics2D g2, XYPlot plot, ValueAxis rangeAxis, Marker marker,
        Rectangle2D dataArea) {

    if (marker instanceof ValueMarker)
        super.drawRangeMarker(g2, plot, rangeAxis, marker, dataArea);
    else if (marker instanceof IntervalMarker)
        super.drawRangeMarker(g2, plot, rangeAxis, marker, dataArea);
    else if (marker instanceof ArbitraryMarker) {

        ArbitraryMarker im = (ArbitraryMarker) marker;
        ArrayList<Double> xvals = im.getDomainVal();
        ArrayList<Double> lows = im.getRangeLowValues();
        ArrayList<Double> highs = im.getRangeHighValues();
        sort(xvals, lows, highs);

        Range range = rangeAxis.getRange();
        ValueAxis domainAxis = plot.getDomainAxis();
        Range domain = domainAxis.getRange();

        int length = xvals.size();
        int[] xpoly = new int[length * 2];
        int[] ypoly = new int[length * 2];

        for (int i = 0; i < xvals.size(); i++) {
            double x = domain.constrain(xvals.get(i));
            double low = range.constrain(lows.get(i));
            double high = range.constrain(highs.get(i));
            int low2d = (int) Math.round(rangeAxis.valueToJava2D(low, dataArea, plot.getRangeAxisEdge()));
            int high2d = (int) Math.round(rangeAxis.valueToJava2D(high, dataArea, plot.getRangeAxisEdge()));
            int x2d = (int) Math.round(domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge()));
            xpoly[i] = x2d;
            xpoly[2 * length - 1 - i] = x2d;
            ypoly[i] = low2d;
            ypoly[2 * length - 1 - i] = high2d;
        }

        PlotOrientation orientation = plot.getOrientation();
        Polygon poly = null;
        if (orientation == PlotOrientation.HORIZONTAL)
            poly = new Polygon(ypoly, xpoly, length * 2);
        else if (orientation == PlotOrientation.VERTICAL)
            poly = new Polygon(xpoly, ypoly, length * 2);

        Paint p = im.getPaint();
        if (p instanceof GradientPaint) {
            GradientPaint gp = (GradientPaint) p;
            GradientPaintTransformer t = im.getGradientPaintTransformer();
            if (t != null)
                gp = t.transform(gp, poly);
            g2.setPaint(gp);
        } else
            g2.setPaint(p);
        g2.fill(poly);
        /*
         * String label = marker.getLabel(); RectangleAnchor anchor = marker.getLabelAnchor(); if ( label != null ) {
         * Font labelFont = marker.getLabelFont(); g2.setFont( labelFont ); g2.setPaint( marker.getLabelPaint() );
         * Point2D coordinates = calculateRangeMarkerTextAnchorPoint( g2, orientation, dataArea, poly,
         * marker.getLabelOffset(), marker.getLabelOffsetType(), anchor ); TextUtilities.drawAlignedString( label,
         * g2, ( float ) coordinates.getX(), ( float ) coordinates.getY(), marker.getLabelTextAnchor() ); }
         */
    }
}

From source file:gda.plots.SimpleXYItemRenderer.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.//from   w ww . java 2  s.  c  o  m
 * 
 * @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 item
 *            the item 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) {
    SimpleXYSeries sxys = (SimpleXYSeries) ((SimpleXYSeriesCollection) dataset).getSeries(series);

    if (!sxys.isVisible()) {
        return;
    }
    // setup for collecting optional entity info...
    Shape entityArea = null;
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

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

    // get the data point
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);

    // Test
    x1 = xValueTransformer.transformValue(x1);

    if (Double.isNaN(x1) || Double.isNaN(y1)) {
        return;
    }

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    if (sxys.isDrawLines()) {
        if (item > 0) {
            // get the previous data point...
            double x0 = dataset.getXValue(series, item - 1);
            double y0 = dataset.getYValue(series, item - 1);

            // Test
            // System.out.print("tranformed " + x0);
            x0 = xValueTransformer.transformValue(x0);
            // Message.debug(" to " + x0);
            if (!Double.isNaN(x0) && !Double.isNaN(y0)) {
                boolean drawLine = true;
                if (getPlotDiscontinuous()) {
                    // only draw a line if the gap between the current and
                    // previous data
                    // point is within the threshold
                    int numX = dataset.getItemCount(series);
                    double minX = dataset.getXValue(series, 0);
                    double maxX = dataset.getXValue(series, numX - 1);
                    drawLine = (x1 - x0) <= ((maxX - minX) / numX * getGapThreshold());
                }
                if (drawLine) {
                    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
                    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

                    // only draw if we have good values
                    if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                            || Double.isNaN(transY1)) {
                        return;
                    }

                    if (orientation == PlotOrientation.HORIZONTAL) {
                        state.workingLine.setLine(transY0, transX0, transY1, transX1);
                    } else if (orientation == PlotOrientation.VERTICAL) {
                        state.workingLine.setLine(transX0, transY0, transX1, transY1);
                    }

                    if (state.workingLine.intersects(dataArea)) {
                        g2.draw(state.workingLine);
                    }
                }
            }
        }
    }

    if (sxys.isDrawMarkers()) {

        Shape shape = sxys.getSymbol();
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        }
        if (shape.intersects(dataArea)) {
            g2.setPaint(sxys.getSymbolPaint());
            // Always use full stroke for drawing marker
            g2.setStroke(new BasicStroke());
            if (sxys.getFilled()) {
                g2.fill(shape);
            } else {
                g2.draw(shape);
            }
            g2.setPaint(sxys.getPaint());
            g2.setStroke(sxys.getStroke());
        }
        entityArea = shape;

    }

    if (getPlotImages()) {
        // use shape scale with transform??
        // double scale = getShapeScale(plot, series, item, transX1,
        // transY1);
        Image image = getImage(plot, series, item, transX1, transY1);
        if (image != null) {
            Point hotspot = getImageHotspot(plot, series, item, transX1, transY1, image);
            g2.drawImage(image, (int) (transX1 - hotspot.getX()), (int) (transY1 - hotspot.getY()), null);
            entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), transY1 - hotspot.getY(),
                    image.getWidth(null), image.getHeight(null));
        }

    }

    // draw the item label if there is one...
    if (isItemLabelVisible(series, item)) {
        drawItemLabel(g2, orientation, dataset, series, item, transX1, transY1, (y1 < 0.0));
    }

    updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, orientation);

    // add an entity for the item...
    if (entities != null) {
        addEntity(entities, entityArea, dataset, series, item, transX1, transY1);
    }

}

From source file:com.aurel.track.report.gantt.data.TrackGanttRenderer.java

/**
 * Draws a single task.//  w  w w  . j  ava 2 s  . c om
 *
 * @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).
 */
protected void drawTask(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, TrackTaskSeriesCollection dataset, int row, int column,
        int pass) {

    log.debug("Working on item at position - column: " + column + " row: " + row);

    int seriesCount = getRowCount();
    int categoryCount = getColumnCount();

    RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();

    // Y0
    Number value0 = dataset.getEndValue(row, column);
    if (value0 == null) {
        return;
    }
    double java2dValue0 = rangeAxis.valueToJava2D(value0.doubleValue(), dataArea, rangeAxisLocation);

    // Y1
    Number value1 = dataset.getStartValue(row, column);
    if (value1 == null) {
        return;
    }
    double java2dValue1 = rangeAxis.valueToJava2D(value1.doubleValue(), dataArea, rangeAxisLocation);

    if (java2dValue1 < java2dValue0) {
        double temp = java2dValue1;
        java2dValue1 = java2dValue0;
        java2dValue0 = temp;
        Number tempNum = value1;
        value1 = value0;
        value0 = tempNum;
    }

    double rectStart = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, domainAxisLocation);

    // BREADTH
    double rectBreadth = state.getBarWidth();

    // BAR HEIGHT
    double rectLength = Math.abs(java2dValue1 - java2dValue0);

    Rectangle2D bar = null;

    if (seriesCount > 1) {
        double seriesGap = dataArea.getHeight() * getItemMargin() / (categoryCount * (seriesCount - 1));
        rectStart = rectStart + row * (state.getBarWidth() + seriesGap);
    } else {
        rectStart = rectStart + row * state.getBarWidth();
    }

    // correct the start/stop Bars point
    if (startBars > (int) (java2dValue1 - rectLength)) {
        this.startBars = (int) (java2dValue1 - rectLength);
    }

    if (stopBars < (int) java2dValue1) {
        stopBars = (int) java2dValue1;
    }

    // The coordinates for the bar labels
    int startLabelX = 0;
    int startLabelY = 0;

    int endLabelX = 0;
    int endLabelY = 0;

    int barLabelX = 0;
    int barLabelY = 0;

    Polygon outer = null;
    Polygon inner = null;

    boolean isMilestone = dataset.isMilestone(column, row);
    boolean isSupertask = dataset.isSupertask(column, row);

    if (isMilestone) {

        // The start point for the end date label
        endLabelX = (int) (java2dValue1 + rectBreadth / 2);
        endLabelY = (int) (rectStart + (3 * rectBreadth / 4));

        barLabelX = (int) (java2dValue1 - rectBreadth / 2);
        barLabelY = (int) (rectStart + (3 * rectBreadth / 4));

        int breadth = (int) rectBreadth;

        int centerX = (int) java2dValue1;

        int centerY = (int) (rectStart + rectBreadth / 2);

        int[] xPoints = { centerX, centerX - breadth / 2, centerX, centerX + breadth / 2 };
        int[] yPoints = { centerY - breadth / 2, centerY, centerY + breadth / 2, centerY };

        outer = new Polygon(xPoints, yPoints, 4);

        //calculate the difference from the ratio
        int diff = (int) (rectBreadth / (2 * msInfo.getRatio()));

        int[] xPointsI = { centerX, centerX - breadth / 2 + diff, centerX, centerX + breadth / 2 - diff };
        int[] yPointsI = { centerY - breadth / 2 + diff, centerY, centerY + breadth / 2 - diff, centerY };

        inner = new Polygon(xPointsI, yPointsI, 4);

    }

    else if (isSupertask) {

        // The start point for the end date label
        startLabelX = (int) (java2dValue1 - rectLength - rectBreadth / 2);
        startLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        endLabelX = (int) (java2dValue1 + rectBreadth / 2);
        endLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        barLabelX = (int) (java2dValue1 - rectLength + rectBreadth / 3);
        barLabelY = (int) (rectStart + rectBreadth);

        int breadth = (int) rectBreadth;
        int length = (int) rectLength;

        int startX = (int) (java2dValue1 - rectLength);
        int startY = (int) rectStart;

        int[] xPoints = { startX - breadth / 2, startX + length + breadth / 2, startX + length + breadth / 2,
                startX + length, startX + length - breadth / 2, startX + breadth / 2, startX,
                startX - breadth / 2 };
        int[] yPoints = { startY, startY, startY + breadth / 2, startY + breadth, startY + breadth / 2,
                startY + breadth / 2, startY + breadth, startY + breadth / 2 };

        outer = new Polygon(xPoints, yPoints, 8);

        //calculate the difference from the ratio
        int diff = (int) (rectBreadth / (2 * stInfo.getRatio()));

        int[] xPointsI = { startX - breadth / 2 + diff, startX + length + breadth / 2 - diff,
                startX + length + breadth / 2 - diff, startX + length, startX + length - breadth / 2 + diff,
                startX + length - breadth / 2 + diff, startX + breadth / 2 - diff, startX + breadth / 2 - diff,
                startX, startX - breadth / 2 + diff };
        int[] yPointsI = { startY + diff, startY + diff, startY + breadth / 2, startY + breadth / 2 + diff,
                startY + breadth / 2, startY + breadth / 2 - diff, startY + breadth / 2 - diff,
                startY + breadth / 2, startY + breadth / 2 + diff, startY + breadth / 2 };

        inner = new Polygon(xPointsI, yPointsI, 10);
    } else {

        rectBreadth = rectBreadth / 2.0;
        // The start point for the end date label
        startLabelX = (int) (java2dValue1 - rectLength);
        startLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        endLabelX = (int) java2dValue1;
        endLabelY = (int) (rectStart + rectBreadth * 3 / 4);

        barLabelX = (int) (java2dValue1 - rectLength);
        barLabelY = (int) (rectStart); //+ rectBreadth * 4 / 3 );

        bar = new Rectangle2D.Double(java2dValue0, rectStart, rectLength, rectBreadth);
        // percent complete could be obtained in the 
        // ReportBeans, set in the ReportBean and set in the Item by the GanttGenerator
        // currently this is not relevant.
        Rectangle2D completeBar = null;
        Rectangle2D incompleteBar = null;
        Number percent = dataset.getPercentComplete(row, column);
        double start = getStartPercent();
        double end = getEndPercent();
        if (percent != null) {
            double p = percent.doubleValue();
            completeBar = new Rectangle2D.Double(java2dValue0, rectStart + start * rectBreadth, rectLength * p,
                    rectBreadth * (end - start));
            incompleteBar = new Rectangle2D.Double(java2dValue0 + rectLength * p,
                    rectStart + start * rectBreadth, rectLength * (1 - p), rectBreadth * (end - start));
        }

        /** This is for displaying e.g. (german Soll und Ist Termine). This implementation is designed to display only one Series.
         * If you need Soll und Ist, you can generate two gantt charts.
         * --->> Paint seriesPaint = getItemPaint (row, column);
         */

        Paint seriesPaint = tsInfo.getInnerColor();

        g2.setPaint(seriesPaint);
        g2.fill(bar);

        if (tsInfo.isDisplayIDLabel() && (!tsInfo.isDisplaySynopsis())) {
            // draw the tasks ID
            drawLabel(new Integer(dataset.getItem(column, row).getId()).toString(), g2, barLabelX, barLabelY,
                    tsInfo.getLabelFontID(), false, (int) rectLength);
        }

        else if (tsInfo.isDisplaySynopsis()) {
            if (tsInfo.isDisplayDateLabel()) {

                // format start date to the requested format using the current locale
                String label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStart());

                label = dataset.getItem(column, row).getDescription() + " - " + label;
                drawLabel(label, g2, barLabelX, barLabelY, tsInfo.getLabelFont(), true);

                // format end date to the requested format using the current locale
                label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStop());

                // draw the supertasks end date
                drawLabel(label, g2, endLabelX, endLabelY, tsInfo.getLabelFont(), false);

            } else {
                drawLabel(dataset.getItem(column, row).getDescription(), g2, startLabelX, startLabelY,
                        tsInfo.getLabelFont(), true);
            }
        }

        if (tsInfo.isDisplayDateLabel() && (!tsInfo.isDisplaySynopsis())) {

            // format start date to the requested format using the current locale
            String label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStart());

            // draw the tasks start date
            drawLabel(label, g2, startLabelX, startLabelY, tsInfo.getLabelFont(), true);

            // format end date to the requested format using the current locale
            label = (new SimpleDateFormat(tsInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStop());
            // draw the tasks end date
            drawLabel(label, g2, endLabelX, endLabelY, tsInfo.getLabelFont(), false);
        }

        if (completeBar != null) {
            g2.setPaint(getCompletePaint());
            g2.fill(completeBar);
        }
        if (incompleteBar != null) {
            g2.setPaint(getIncompletePaint());
            g2.fill(incompleteBar);
        }

        // draw the outline...
        if (state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {

            Stroke stroke = getItemOutlineStroke(row, column);
            Paint paint = getItemOutlinePaint(row, column);
            if (stroke != null && paint != null) {
                g2.setStroke(stroke);
                g2.setPaint(paint);

                g2.draw(bar);
            }
        }
    }

    // Draw milestone or Supertask
    if (isMilestone) {

        g2.setPaint(msInfo.getOuterColor());
        g2.fill(outer);
        g2.draw(outer);

        g2.setPaint(msInfo.getInnerColor());
        g2.fill(inner);
        g2.draw(inner);

        Stroke stroke = getItemOutlineStroke(row, column);
        Paint paint = getItemOutlinePaint(row, column);
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(outer);
        }

        // draw the milestones due date
        if (msInfo.isDisplayDateLabel()) {

            // format milestone date to the requested format using the current locale
            String label = (new SimpleDateFormat(msInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStart());

            drawLabel(label, g2, endLabelX, endLabelY, msInfo.getLabelFont(), false);
        }

        if (msInfo.isDisplayIDLabel() && (!msInfo.isDisplaySynopsis())) {
            // for milestones the ID is aligned at the left side.
            drawLabel(new Integer(dataset.getItem(column, row).getId()).toString(), g2, barLabelX, barLabelY,
                    msInfo.getLabelFontID(), true, (int) rectLength);
        } else if (msInfo.isDisplaySynopsis()) {
            drawLabel(dataset.getItem(column, row).getDescription(), g2, barLabelX, barLabelY,
                    msInfo.getLabelFont(), true, (int) rectLength);
        }

    } else if (isSupertask) {

        g2.setPaint(stInfo.getOuterColor());
        g2.fill(outer);
        g2.draw(outer);

        g2.setPaint(stInfo.getInnerColor());
        g2.fill(inner);
        g2.draw(inner);

        Stroke stroke = getItemOutlineStroke(row, column);
        Paint paint = getItemOutlinePaint(row, column);
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(outer);
        }

        // draw the supertasks ID
        if (stInfo.isDisplayIDLabel() && (!stInfo.isDisplaySynopsis())) {
            drawLabel(new Integer(dataset.getItem(column, row).getId()).toString(), g2, barLabelX, barLabelY,
                    stInfo.getLabelFontID(), false, (int) rectLength);
        } else if (stInfo.isDisplaySynopsis()) {
            if (stInfo.isDisplayDateLabel()) {

                // format start date to the requested format using the current locale
                String label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStart());

                label = dataset.getItem(column, row).getDescription() + " - " + label;
                drawLabel(label, g2, startLabelX, startLabelY, stInfo.getLabelFont(), true);

                // format end date to the requested format using the current locale
                label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                        .format(dataset.getItem(column, row).getStop());

                // draw the supertasks end date
                drawLabel(label, g2, endLabelX, endLabelY, stInfo.getLabelFont(), false);

            } else {
                drawLabel(dataset.getItem(column, row).getDescription(), g2, startLabelX, startLabelY,
                        stInfo.getLabelFont(), true);
            }
        }
        if (stInfo.isDisplayDateLabel() && (!tsInfo.isDisplaySynopsis())) {

            // format start date to the requested format using the current locale
            String label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStart());

            // draw the supertasks start date
            drawLabel(label, g2, startLabelX, startLabelY, stInfo.getLabelFont(), true);

            // format end date to the requested format using the current locale
            label = (new SimpleDateFormat(stInfo.getDateLabelFormat(), locale))
                    .format(dataset.getItem(column, row).getStop());

            // draw the supertasks end date
            drawLabel(label, g2, endLabelX, endLabelY, stInfo.getLabelFont(), false);
        }
    }
}

From source file:diet.gridr.g5k.util.ExtendedStackedBarRenderer.java

/**
 * Draws a stacked bar for a specific item.
 *
 * @param g2  the graphics device./*  ww w  .ja v a 2 s  . com*/
 * @param state  the renderer state.
 * @param dataArea  the plot area.
 * @param plot  the plot.
 * @param domainAxis  the domain (category) axis.
 * @param rangeAxis  the range (value) 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(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {

    // nothing is drawn for null values...
    Number dataValue = dataset.getValue(row, column);
    if (dataValue == null) {
        return;
    }

    double value = dataValue.doubleValue();

    PlotOrientation orientation = plot.getOrientation();
    double barW0 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge())
            - state.getBarWidth() / 2.0;

    double positiveBase = 0.0;
    double negativeBase = 0.0;

    for (int i = 0; i < row; i++) {
        Number v = dataset.getValue(i, column);
        if (v != null) {
            double d = v.doubleValue();
            if (d > 0) {
                positiveBase = positiveBase + d;
            } else {
                negativeBase = negativeBase + d;
            }
        }
    }

    double translatedBase;
    double translatedValue;
    RectangleEdge location = plot.getRangeAxisEdge();
    if (value > 0.0) {
        translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, location);
        translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, location);
    } else {
        translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, location);
        translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, location);
    }
    double barL0 = Math.min(translatedBase, translatedValue);
    double barLength = Math.max(Math.abs(translatedValue - translatedBase), getMinimumBarLength());

    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
    } else {
        bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
    }
    Paint seriesPaint = getItemPaint(row, column);
    g2.setPaint(seriesPaint);
    g2.fill(bar);
    if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
        g2.setStroke(getItemStroke(row, column));
        g2.setPaint(getItemOutlinePaint(row, column));
        g2.draw(bar);
    }

    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null && isItemLabelVisible(row, column)) {
        drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
    }

    if (value > 0.0) {
        if (this.showPositiveTotal) {
            if (isLastPositiveItem(dataset, row, column)) {
                g2.setPaint(Color.black);
                g2.setFont(this.totalLabelFont);
                double total = calculateSumOfPositiveValuesForCategory(dataset, column);
                TextUtilities.drawRotatedString(this.totalFormatter.format(total), g2, (float) bar.getCenterX(),
                        (float) (bar.getMinY() - 4.0), TextAnchor.BOTTOM_CENTER, 0.0, TextAnchor.BOTTOM_CENTER);
            }
        }
    } else {
        if (this.showNegativeTotal) {
            if (isLastNegativeItem(dataset, row, column)) {
                g2.setPaint(Color.black);
                g2.setFont(this.totalLabelFont);
                double total = calculateSumOfNegativeValuesForCategory(dataset, column);
                TextUtilities.drawRotatedString(String.valueOf(total), g2, (float) bar.getCenterX(),
                        (float) (bar.getMaxY() + 4.0), TextAnchor.TOP_CENTER, 0.0, TextAnchor.TOP_CENTER);
            }
        }
    }

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            String tip = null;
            CategoryToolTipGenerator tipster = getToolTipGenerator(row, column);
            if (tipster != null) {
                tip = tipster.generateToolTip(dataset, row, column);
            }
            String url = null;
            if (getItemURLGenerator(row, column) != null) {
                url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
            }
            CategoryItemEntity entity = new CategoryItemEntity(bar, tip, url, dataset, row,
                    dataset.getColumnKey(column), column);
            entities.add(entity);
        }
    }

}

From source file:org.rdv.viz.chart.FastXYItemRenderer.java

@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) {

    boolean itemVisible = getItemVisible(series, item);

    // setup for collecting optional entity info...
    Shape entityArea = null;//from w  ww .  j av a2 s.  c om
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

    PlotOrientation orientation = plot.getOrientation();
    Paint paint = getItemPaint(series, item);
    Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(paint);
    g2.setStroke(seriesStroke);

    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(x1) || Double.isNaN(y1)) {
        itemVisible = false;
    }

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    if (getPlotLines()) {
        if (item == 0) {
            previousDrawnItem = 1;
        }

        if (getDrawSeriesLineAsPath()) {
            State s = (State) state;
            if (s.getSeriesIndex() != series) {
                // we are starting a new series path
                s.seriesPath.reset();
                s.setLastPointGood(false);
                s.setSeriesIndex(series);
            }

            // update path to reflect latest point
            if (itemVisible && !Double.isNaN(transX1) && !Double.isNaN(transY1)) {
                float x = (float) transX1;
                float y = (float) transY1;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    x = (float) transY1;
                    y = (float) transX1;
                }
                if (s.isLastPointGood()) {
                    // TODO: check threshold
                    s.seriesPath.lineTo(x, y);
                } else {
                    s.seriesPath.moveTo(x, y);
                }
                s.setLastPointGood(true);
            } else {
                s.setLastPointGood(false);
            }
            if (item == dataset.getItemCount(series) - 1) {
                if (s.getSeriesIndex() == series) {
                    // draw path
                    g2.setStroke(lookupSeriesStroke(series));
                    g2.setPaint(lookupSeriesPaint(series));
                    g2.draw(s.seriesPath);
                }
            }
        }

        else if (item != 0 && itemVisible) {
            // get the previous data point...
            double x0 = dataset.getXValue(series, item - previousDrawnItem);
            double y0 = dataset.getYValue(series, item - previousDrawnItem);
            if (!Double.isNaN(x0) && !Double.isNaN(y0)) {
                boolean drawLine = true;
                if (getPlotDiscontinuous()) {
                    // only draw a line if the gap between the current and
                    // previous data point is within the threshold
                    int numX = dataset.getItemCount(series);
                    double minX = dataset.getXValue(series, 0);
                    double maxX = dataset.getXValue(series, numX - 1);
                    if (getGapThresholdType() == UnitType.ABSOLUTE) {
                        drawLine = Math.abs(x1 - x0) <= getGapThreshold();
                    } else {
                        drawLine = Math.abs(x1 - x0) <= ((maxX - minX) / numX * getGapThreshold());
                    }
                }
                if (drawLine) {
                    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
                    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

                    // only draw if we have good values
                    if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                            || Double.isNaN(transY1)) {
                        return;
                    }

                    // Only draw line if it is more than a pixel away from the
                    // previous one
                    if ((transX1 - transX0 > 2 || transX1 - transX0 < -2 || transY1 - transY0 > 2
                            || transY1 - transY0 < -2)) {
                        previousDrawnItem = 1;

                        if (orientation == PlotOrientation.HORIZONTAL) {
                            state.workingLine.setLine(transY0, transX0, transY1, transX1);
                        } else if (orientation == PlotOrientation.VERTICAL) {
                            state.workingLine.setLine(transX0, transY0, transX1, transY1);
                        }

                        if (state.workingLine.intersects(dataArea)) {
                            g2.draw(state.workingLine);
                        }
                    } else {
                        // Increase counter for the previous drawn item.
                        previousDrawnItem++;
                    }
                }
            }
        }
    }

    // we needed to get this far even for invisible items, to ensure that
    // seriesPath updates happened, but now there is nothing more we need
    // to do for non-visible items...
    if (!itemVisible) {
        return;
    }

    // add a cursor to indicate the position of the last data item
    if (getCursorVisible() && item == dataset.getItemCount(series) - 1) {
        Line2D cursorX = new Line2D.Double(transX1 - DEFAULT_CURSOR_SIZE, transY1,
                transX1 + DEFAULT_CURSOR_SIZE, transY1);
        g2.draw(cursorX);
        Line2D cursorY = new Line2D.Double(transX1, transY1 - DEFAULT_CURSOR_SIZE, transX1,
                transY1 + DEFAULT_CURSOR_SIZE);
        g2.draw(cursorY);
    }

    if (getBaseShapesVisible()) {

        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        }
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                g2.fill(shape);
            } else {
                g2.draw(shape);
            }
        }
        entityArea = shape;

    }

    if (getPlotImages()) {
        Image image = getImage(plot, series, item, transX1, transY1);
        if (image != null) {
            Point hotspot = getImageHotspot(plot, series, item, transX1, transY1, image);
            g2.drawImage(image, (int) (transX1 - hotspot.getX()), (int) (transY1 - hotspot.getY()), null);
            entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), transY1 - hotspot.getY(),
                    image.getWidth(null), image.getHeight(null));
        }

    }

    double xx = transX1;
    double yy = transY1;
    if (orientation == PlotOrientation.HORIZONTAL) {
        xx = transY1;
        yy = transX1;
    }

    // draw the item label if there is one...
    if (isItemLabelVisible(series, item)) {
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, (y1 < 0.0));
    }

    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
            orientation);

    // add an entity for the item...
    if (entities != null && dataArea.contains(xx, yy)) {
        addEntity(entities, entityArea, dataset, series, item, xx, yy);
    }
}

From source file:edu.ucla.stat.SOCR.chart.gui.ExtendedStackedBarRenderer.java

/**
 * Draws a stacked bar for a specific item.
 *
 * @param g2  the graphics device.//  ww  w .  j a  v a  2 s  . co m
 * @param state  the renderer state.
 * @param dataArea  the plot area.
 * @param plot  the plot.
 * @param domainAxis  the domain (category) axis.
 * @param rangeAxis  the range (value) 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(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {

    // nothing is drawn for null values...
    Number dataValue = dataset.getValue(row, column);
    if (dataValue == null) {
        return;
    }

    double value = dataValue.doubleValue();

    PlotOrientation orientation = plot.getOrientation();
    double barW0 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge())
            - state.getBarWidth() / 2.0;

    double positiveBase = 0.0;
    double negativeBase = 0.0;

    for (int i = 0; i < row; i++) {
        Number v = dataset.getValue(i, column);
        if (v != null) {
            double d = v.doubleValue();
            if (d > 0) {
                positiveBase = positiveBase + d;
            } else {
                negativeBase = negativeBase + d;
            }
        }
    }

    double translatedBase;
    double translatedValue;
    RectangleEdge location = plot.getRangeAxisEdge();
    if (value > 0.0) {
        translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, location);
        translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, location);
    } else {
        translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, location);
        translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, location);
    }
    double barL0 = Math.min(translatedBase, translatedValue);
    double barLength = Math.max(Math.abs(translatedValue - translatedBase), getMinimumBarLength());

    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
    } else {
        bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
    }
    Paint seriesPaint = getItemPaint(row, column);
    g2.setPaint(seriesPaint);
    g2.fill(bar);
    if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
        g2.setStroke(getItemStroke(row, column));
        g2.setPaint(getItemOutlinePaint(row, column));
        g2.draw(bar);
    }

    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null && isItemLabelVisible(row, column)) {
        drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
    }

    if (value > 0.0) {
        if (this.showPositiveTotal) {
            if (isLastPositiveItem(dataset, row, column)) {
                g2.setPaint(Color.black);
                g2.setFont(this.totalLabelFont);
                double total = calculateSumOfPositiveValuesForCategory(dataset, column);
                if (orientation == PlotOrientation.HORIZONTAL)
                    TextUtilities.drawRotatedString(this.totalFormatter.format(total), g2,
                            (float) (bar.getMaxX() + 5.0), (float) bar.getCenterY(), TextAnchor.CENTER_LEFT,
                            0.0, TextAnchor.CENTER_LEFT);

                else
                    TextUtilities.drawRotatedString(this.totalFormatter.format(total), g2,
                            (float) bar.getCenterX(), (float) (bar.getMinY() - 4.0), TextAnchor.BOTTOM_CENTER,
                            0.0, TextAnchor.BOTTOM_CENTER);
            }
        }
    } else {
        if (this.showNegativeTotal) {
            if (isLastNegativeItem(dataset, row, column)) {
                g2.setPaint(Color.black);
                g2.setFont(this.totalLabelFont);
                double total = calculateSumOfNegativeValuesForCategory(dataset, column);
                if (orientation == PlotOrientation.HORIZONTAL)
                    TextUtilities.drawRotatedString(String.valueOf(total), g2, (float) (bar.getMinX() - 5.0),
                            (float) bar.getCenterY(), TextAnchor.CENTER_RIGHT, 0.0, TextAnchor.CENTER_RIGHT);
                else
                    TextUtilities.drawRotatedString(String.valueOf(total), g2, (float) bar.getCenterX(),
                            (float) (bar.getMaxY() + 4.0), TextAnchor.TOP_CENTER, 0.0, TextAnchor.TOP_CENTER);
            }
        }
    }

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            String tip = null;
            CategoryToolTipGenerator tipster = getToolTipGenerator(row, column);
            if (tipster != null) {
                tip = tipster.generateToolTip(dataset, row, column);
            }
            String url = null;
            if (getItemURLGenerator(row, column) != null) {
                url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
            }
            CategoryItemEntity entity = new CategoryItemEntity(bar, tip, url, dataset, dataset.getRowKey(row),
                    dataset.getColumnKey(column));
            entities.add(entity);
        }
    }

}

From source file:net.sf.maltcms.chromaui.charts.renderer.XYNoBlockRenderer.java

/**
 * Draws the block representing the specified item.
 *
 * @param g2 the graphics device./*w  w w .  j av a 2 s .  c om*/
 * @param state the state.
 * @param dataArea the data area.
 * @param info the plot rendering info.
 * @param plot the plot.
 * @param domainAxis the x-axis.
 * @param rangeAxis the y-axis.
 * @param dataset the dataset.
 * @param series the series index.
 * @param item the item index.
 * @param crosshairState the crosshair state.
 * @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) {
    //        return;
    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double z = 0.0;
    if (dataset instanceof XYZDataset) {
        z = ((XYZDataset) dataset).getZValue(series, item);
        if (entityThreshold != Double.NaN && z < entityThreshold) {
            return;
        }
    }

    //}
    Paint p = getPaintScale().getPaint(z);
    //        if(p.equals(getPaintScale().getPaint(getPaintScale().getLowerBound()))) {
    //            return;
    //        }
    //        double xx0 = domainAxis.valueToJava2D(x + xOffset, dataArea,
    //                plot.getDomainAxisEdge());
    //        double yy0 = rangeAxis.valueToJava2D(y + yOffset, dataArea,
    //                plot.getRangeAxisEdge());
    //        double xx1 = domainAxis.valueToJava2D(x + blockWidth
    //                + xOffset, dataArea, plot.getDomainAxisEdge());
    //        double yy1 = rangeAxis.valueToJava2D(y + blockHeight
    //                + yOffset, dataArea, plot.getRangeAxisEdge());
    double xx0 = domainAxis.valueToJava2D(x - getBlockWidth() / 2, dataArea, plot.getDomainAxisEdge());
    double yy0 = rangeAxis.valueToJava2D(y - getBlockHeight() / 2, dataArea, plot.getRangeAxisEdge());
    double xx1 = domainAxis.valueToJava2D(x + getBlockWidth() / 2, dataArea, plot.getDomainAxisEdge());
    double yy1 = rangeAxis.valueToJava2D(y + getBlockHeight() / 2, dataArea, plot.getRangeAxisEdge());

    Rectangle2D block;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation.equals(PlotOrientation.HORIZONTAL)) {
        if (dataArea.contains(Math.min(yy0, yy1), Math.min(xx0, xx1), Math.abs(yy1 - yy0),
                Math.abs(xx0 - xx1))) {
            block = new Rectangle2D.Double(Math.min(yy0, yy1), Math.min(xx0, xx1), Math.abs(yy1 - yy0),
                    Math.abs(xx0 - xx1));
        } else {
            return;
        }
    } else {
        if (dataArea.contains(Math.min(xx0, xx1), Math.min(yy0, yy1), Math.abs(xx1 - xx0),
                Math.abs(yy0 - yy1))) {
            block = new Rectangle2D.Double(Math.min(xx0, xx1), Math.min(yy0, yy1), Math.abs(xx1 - xx0),
                    Math.abs(yy0 - yy1));
        } else {
            return;
        }
    }

    g2.setPaint(p);
    g2.fill(block);
    g2.setStroke(new BasicStroke(1.0f));
    g2.draw(block);
    EntityCollection entities = state.getEntityCollection();
    //        System.out.println("Entity collection is of type: "+entities.getClass());
    if (entities != null) {
        //System.out.println("Adding entity");
        addEntity(entities, block, dataset, series, item, block.getCenterX(), block.getCenterY());
    }

}

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

/**
 * draw//  w ww . j  a  v  a  2 s.com
 *
 * @param g2 the graphics
 * @param dataArea where the data area is
 * @param index which data set
 * @param info info
 * @param crosshairState crosshairState
 *
 * @return any drawn
 */
public boolean render(Graphics2D g2, Rectangle2D dataArea, int index, PlotRenderingInfo info,
        CrosshairState crosshairState) {

    XYDataset dataset = getDataset(index);
    if (DatasetUtilities.isEmptyOrNull(dataset)) {
        return false;
    }

    ValueAxis rangeAxis = getRangeAxisForDataset(index);
    ValueAxis domainAxis = getDomainAxisForDataset(index);
    AxisLocation rangeAxisLocation = getRangeAxisLocation(index);
    AxisLocation domainAxisLocation = getDomainAxisLocation(index);
    RectangleEdge rangeEdge = getRangeAxisEdge();
    RectangleEdge domainEdge = getDomainAxisEdge();
    int seriesCount = dataset.getSeriesCount();
    //        System.out.println ("********************************");
    for (int series = seriesCount - 1; series >= 0; series--) {
        int itemCount = dataset.getItemCount(series);
        int[] xs = new int[itemCount];
        int[] ys = new int[itemCount];
        g2.setStroke(getRendererForDataset(dataset).getSeriesStroke(series));
        g2.setPaint(getRendererForDataset(dataset).getSeriesPaint(series));
        int pointCnt = 0;
        for (int item = 0; item < itemCount; item++) {
            double x1 = dataset.getXValue(series, item);
            double y1 = dataset.getYValue(series, item);
            if (!timeseries.valuesOk(index, x1, y1)) {
                if (pointCnt > 0) {
                    g2.drawPolyline(xs, ys, pointCnt);
                    pointCnt = 0;
                }
                continue;
            }
            double transX = domainAxis.valueToJava2D(x1, dataArea, domainEdge);
            double transY = rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
            if (!dataArea.contains(transX, transY)) {
                continue;
            }

            xs[pointCnt] = (int) (transX + 0.5);
            ys[pointCnt] = (int) (transY + 0.5);
            pointCnt++;
            if (pointCnt > 10) {
                g2.drawPolyline(xs, ys, pointCnt);
                xs[0] = xs[pointCnt - 1];
                ys[0] = ys[pointCnt - 1];
                pointCnt = 1;
            }

        }
        if (pointCnt > 1) {
            g2.drawPolyline(xs, ys, pointCnt);
        }
        long t2 = System.currentTimeMillis();
        //            System.err.println("time:" + (t2 - t1) + "ms #" + itemCount);
    }
    return true;
}

From source file:msi.gama.outputs.layers.charts.FastXYItemRenderer.java

/** {@inheritDoc} */
@Override//from  ww  w.j a  va 2  s . c o  m
public void drawItem(final Graphics2D g2, final XYItemRendererState state, final Rectangle2D dataArea,
        final PlotRenderingInfo info, final XYPlot plot, final ValueAxis domainAxis, final ValueAxis rangeAxis,
        final XYDataset dataset, final int series, final int item, final CrosshairState crosshairState,
        final int pass) {

    if (!getItemVisible(series, item)) {
        return;
    }
    // setup for collecting optional entity info...
    boolean bAddEntity = false;
    Shape entityArea = null;
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

    final PlotOrientation orientation = plot.getOrientation();
    final Paint paint = getItemPaint(series, item);
    final Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(paint);
    g2.setStroke(seriesStroke);

    // get the data point...
    final double x1 = dataset.getXValue(series, item);
    final double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(x1) || Double.isNaN(y1)) {
        return;
    }

    final RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    final RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    final double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    final double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    if (getPlotLines()) {
        if (item == 0) {
            if (this.drawSeriesLineAsPath) {
                final State s = (State) state;
                s.seriesPath.reset();
                s.lastPointGood = false;
            }
            previousDrawnItem = 0;
        }

        if (this.drawSeriesLineAsPath) {
            final State s = (State) state;
            // update path to reflect latest point
            if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) {
                float x = (float) transX1;
                float y = (float) transY1;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    x = (float) transY1;
                    y = (float) transX1;
                }
                if (s.isLastPointGood()) {
                    // TODO: check threshold
                    s.seriesPath.lineTo(x, y);
                } else {
                    s.seriesPath.moveTo(x, y);
                }
                s.setLastPointGood(true);
            } else {
                s.setLastPointGood(false);
            }
            if (item == dataset.getItemCount(series) - 1) {
                // draw path
                g2.setStroke(getSeriesStroke(series));
                g2.setPaint(getSeriesPaint(series));
                g2.draw(s.seriesPath);
            }
        }

        else if (item != 0) {
            // get the previous data point...
            final double x0 = dataset.getXValue(series, item - previousDrawnItem);
            final double y0 = dataset.getYValue(series, item - previousDrawnItem);
            if (!Double.isNaN(x0) && !Double.isNaN(y0)) {
                boolean drawLine = true;
                if (getPlotDiscontinuous()) {
                    // only draw a line if the gap between the current and
                    // previous data point is within the threshold
                    final int numX = dataset.getItemCount(series);
                    final double minX = dataset.getXValue(series, 0);
                    final double maxX = dataset.getXValue(series, numX - 1);
                    if (this.gapThresholdType == UnitType.ABSOLUTE) {
                        drawLine = Math.abs(x1 - x0) <= this.gapThreshold;
                    } else {
                        drawLine = Math.abs(x1 - x0) <= (maxX - minX) / numX * getGapThreshold();
                    }
                }
                if (drawLine) {
                    final double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
                    final double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

                    // only draw if we have good values
                    if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                            || Double.isNaN(transY1)) {
                        return;
                    }

                    // Only draw line if it is more than a pixel away from the previous one
                    if (transX1 - transX0 > 2 || transX1 - transX0 < -2 || transY1 - transY0 > 2
                            || transY1 - transY0 < -2 || 0 == previousDrawnItem) {
                        previousDrawnItem = 1;

                        if (orientation == PlotOrientation.HORIZONTAL) {
                            state.workingLine.setLine(transY0, transX0, transY1, transX1);
                        } else if (orientation == PlotOrientation.VERTICAL) {
                            state.workingLine.setLine(transX0, transY0, transX1, transY1);
                        }

                        if (state.workingLine.intersects(dataArea)) {
                            g2.draw(state.workingLine);
                        }
                    } else {
                        // Increase counter for the previous drawn item.
                        previousDrawnItem++;
                        bAddEntity = false;
                    }
                }
            }
        }
    }

    if (getBaseShapesVisible()) {

        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        }
        if (shape.intersects(dataArea)) {
            bAddEntity = true;
            if (getItemShapeFilled(series, item)) {
                g2.fill(shape);
            } else {
                g2.draw(shape);
            }
        }
        entityArea = shape;

    }

    if (getPlotImages()) {
        final Image image = getImage(plot, series, item, transX1, transY1);
        if (image != null) {
            final Point hotspot = getImageHotspot(plot, series, item, transX1, transY1, image);
            g2.drawImage(image, (int) (transX1 - hotspot.getX()), (int) (transY1 - hotspot.getY()), null);
            entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), transY1 - hotspot.getY(),
                    image.getWidth(null), image.getHeight(null));
        }

    }

    // draw the item label if there is one...
    if (isItemLabelVisible(series, item)) {
        double xx = transX1;
        double yy = transY1;
        if (orientation == PlotOrientation.HORIZONTAL) {
            xx = transY1;
            yy = transX1;
        }
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, y1 < 0.0);
    }

    updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, orientation);

    // add an entity for the item...
    if (entities != null && bAddEntity) {
        addEntity(entities, entityArea, dataset, series, item, transX1, transY1);
    }
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.renderer.FormattedScatterRenderer.java

/**
 * This function is taken directly from JFreeChart with adjustments to draw differently colored
 * items./*  w w w . j a  va  2s . c o  m*/
 * 
 * When updating JFreeChart this function must probably be adapted.
 * 
 */
@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {

    // do nothing if item is not visible
    if (!getItemVisible(row, column)) {
        return;
    }
    int visibleRow = state.getVisibleSeriesIndex(row);
    if (visibleRow < 0) {
        return;
    }
    int visibleRowCount = state.getVisibleSeriesCount();

    PlotOrientation orientation = plot.getOrientation();

    ValueSourceToMultiValueCategoryDatasetAdapter dataSet = (ValueSourceToMultiValueCategoryDatasetAdapter) dataset;
    List values = dataSet.getValues(row, column);
    if (values == null) {
        return;
    }
    int valueCount = values.size();
    for (int i = 0; i < valueCount; i++) {
        // current data point...
        double x1;
        if (getUseSeriesOffset()) {
            x1 = domainAxis.getCategorySeriesMiddle(column, dataset.getColumnCount(), visibleRow,
                    visibleRowCount, getItemMargin(), dataArea, plot.getDomainAxisEdge());
        } else {
            x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());
        }
        Number n = (Number) values.get(i);
        int idx = dataSet.getValueIndex(row, column, i);
        double value = n.doubleValue();
        double y1 = rangeAxis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge());

        Shape shape = getItemShape(row, idx);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, y1, x1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, x1, y1);
        }
        if (getItemShapeFilled(row, column)) {
            if (getUseFillPaint()) {
                g2.setPaint(getItemFillPaint(row, column));
            } else {
                g2.setPaint(getItemPaint(row, idx));
            }
            g2.fill(shape);
        }
        if (getDrawOutlines()) {
            if (getUseOutlinePaint()) {
                g2.setPaint(getItemOutlinePaint(row, column));
            } else {
                g2.setPaint(getItemPaint(row, idx));
            }
            g2.setStroke(getItemOutlineStroke(row, column));
            g2.draw(shape);
        }
    }
}