Example usage for org.jfree.ui GradientPaintTransformer transform

List of usage examples for org.jfree.ui GradientPaintTransformer transform

Introduction

In this page you can find the example usage for org.jfree.ui GradientPaintTransformer transform.

Prototype

public GradientPaint transform(GradientPaint paint, Shape target);

Source Link

Document

Transforms a GradientPaint instance to fit some target shape.

Usage

From source file:edu.jhuapl.graphs.jfreechart.CategoryGraphBarPainter.java

@Override
public void paintBar(Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar,
        RectangleEdge base) {// ww  w  .j a v  a  2 s  . com
    Paint itemPaint = itemProperty.get(row, column, Paint.class, renderer.getItemPaint(row, column),
            GraphSource.ITEM_COLOR);
    GradientPaintTransformer t = renderer.getGradientPaintTransformer();

    if (t != null && itemPaint instanceof GradientPaint) {
        itemPaint = t.transform((GradientPaint) itemPaint, bar);
    }

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

    // draw the outline
    if (renderer.isDrawBarOutline()) {
        Stroke stroke = renderer.getItemOutlineStroke(row, column);
        Paint paint = renderer.getItemOutlinePaint(row, column);

        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(bar);
        }
    }
}

From source file:org.schreibubi.JCombinations.jfreechart.XYLineAndShapeRendererExtended.java

/**
 * Draws a horizontal line across the chart to represent a 'range marker'.
 * //  w  ww  . j  ava 2 s.com
 * @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:de.hdm.uls.loadtests.ui.XYLineAndAreaRenderer.java

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device./*from  ww  w.j a va 2 s .  com*/
 * @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.
 */
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 (!getItemVisible(series, item)) {
        return;
    }
    XYAreaRendererState areaState = (XYAreaRendererState) state;

    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(y1)) {
        y1 = 0.0;
    }
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, plot.getDomainAxisEdge());
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());

    // get the previous point and the next point so we can calculate a
    // "hot spot" for the area (used by the chart entity)...
    int itemCount = dataset.getItemCount(series);
    double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
    double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
    if (Double.isNaN(y0)) {
        y0 = 0.0;
    }
    double transX0 = domainAxis.valueToJava2D(x0, dataArea, plot.getDomainAxisEdge());
    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, plot.getRangeAxisEdge());

    double x2 = dataset.getXValue(series, Math.min(item + 1, itemCount - 1));
    double y2 = dataset.getYValue(series, Math.min(item + 1, itemCount - 1));
    if (Double.isNaN(y2)) {
        y2 = 0.0;
    }
    double transX2 = domainAxis.valueToJava2D(x2, dataArea, plot.getDomainAxisEdge());
    double transY2 = rangeAxis.valueToJava2D(y2, dataArea, plot.getRangeAxisEdge());

    double transZero = rangeAxis.valueToJava2D(0.0, dataArea, plot.getRangeAxisEdge());
    Polygon hotspot = null;
    if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
        hotspot = new Polygon();
        hotspot.addPoint((int) transZero, (int) ((transX0 + transX1) / 2.0));
        hotspot.addPoint((int) ((transY0 + transY1) / 2.0), (int) ((transX0 + transX1) / 2.0));
        hotspot.addPoint((int) transY1, (int) transX1);
        hotspot.addPoint((int) ((transY1 + transY2) / 2.0), (int) ((transX1 + transX2) / 2.0));
        hotspot.addPoint((int) transZero, (int) ((transX1 + transX2) / 2.0));
    } else { // vertical orientation
        hotspot = new Polygon();
        hotspot.addPoint((int) ((transX0 + transX1) / 2.0), (int) transZero);
        hotspot.addPoint((int) ((transX0 + transX1) / 2.0), (int) ((transY0 + transY1) / 2.0));
        hotspot.addPoint((int) transX1, (int) transY1);
        hotspot.addPoint((int) ((transX1 + transX2) / 2.0), (int) ((transY1 + transY2) / 2.0));
        hotspot.addPoint((int) ((transX1 + transX2) / 2.0), (int) transZero);
    }

    if (item == 0) { // create a new area polygon for the series
        areaState.area = new Polygon();
        // the first point is (x, 0)
        double zero = rangeAxis.valueToJava2D(0.0, dataArea, plot.getRangeAxisEdge());
        if (plot.getOrientation() == PlotOrientation.VERTICAL) {
            areaState.area.addPoint((int) transX1, (int) zero);
        } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
            areaState.area.addPoint((int) zero, (int) transX1);
        }
    }

    // Add each point to Area (x, y)
    if (plot.getOrientation() == PlotOrientation.VERTICAL) {
        areaState.area.addPoint((int) transX1, (int) transY1);
    } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
        areaState.area.addPoint((int) transY1, (int) transX1);
    }

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

    Shape shape = null;
    if (getPlotShapes()) {
        shape = getItemShape(series, item);
        if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        } else if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        }
        g2.draw(shape);
    }

    if (getPlotLines()) {
        if (item > 0) {
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                areaState.line.setLine(transX0, transY0, transX1, transY1);
            } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
                areaState.line.setLine(transY0, transX0, transY1, transX1);
            }
            g2.draw(areaState.line);
        }
    }

    // Check if the item is the last item for the series.
    // and number of items > 0.  We can't draw an area for a single point.
    if (getPlotArea() && item > 0 && item == (itemCount - 1)) {

        if (orientation == PlotOrientation.VERTICAL) {
            // Add the last point (x,0)
            areaState.area.addPoint((int) transX1, (int) transZero);
        } else if (orientation == PlotOrientation.HORIZONTAL) {
            // Add the last point (x,0)
            areaState.area.addPoint((int) transZero, (int) transX1);
        }

        Paint fillPaint = getItemFillPaint(series, item);
        if (fillPaint instanceof GradientPaint) {
            GradientPaint gp = (GradientPaint) fillPaint;
            GradientPaintTransformer t = new StandardGradientPaintTransformer();
            fillPaint = t.transform(gp, areaState.area.getBounds());
        }
        g2.setPaint(fillPaint);
        g2.fill(areaState.area);

        // draw an outline around the Area.
        if (isOutline()) {
            g2.setStroke(getItemOutlineStroke(series, item));
            g2.setPaint(getItemOutlinePaint(series, item));
            g2.draw(areaState.area);
        }
    }

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

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null && hotspot != null) {
            String tip = null;
            XYToolTipGenerator generator = getToolTipGenerator(series, item);
            if (generator != null) {
                tip = generator.generateToolTip(dataset, series, item);
            }
            String url = null;
            if (getURLGenerator() != null) {
                url = getURLGenerator().generateURL(dataset, series, item);
            }
            XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url);
            entities.add(entity);
        }
    }

}

From source file:extern.AsymmetricStatisticalBarRenderer.java

/**
 * Draws an item for a plot with a horizontal orientation.
 *
 * @param g2  the graphics device./*from   w  w  w  .  j  a va2s .co m*/
 * @param state  the renderer state.
 * @param dataArea  the data area.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the data.
 * @param visibleRow  the visible row index.
 * @param row  the row index (zero-based).
 * @param column  the column index (zero-based).
 */
protected void drawHorizontalItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea,
        CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis,
        AsymmetricStatisticalCategoryDataset dataset, int visibleRow, int row, int column) {

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();

    // BAR Y
    double rectY = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, xAxisLocation);

    int seriesCount = state.getVisibleSeriesCount() >= 0 ? state.getVisibleSeriesCount() : getRowCount();
    int categoryCount = getColumnCount();
    if (seriesCount > 1) {
        double seriesGap = dataArea.getHeight() * getItemMargin() / (categoryCount * (seriesCount - 1));
        rectY = rectY + visibleRow * (state.getBarWidth() + seriesGap);
    } else {
        rectY = rectY + visibleRow * state.getBarWidth();
    }

    // BAR X
    Number meanValue = dataset.getMeanValue(row, column);
    if (meanValue == null) {
        return;
    }
    double value = meanValue.doubleValue();
    double base = 0.0;
    double lclip = getLowerClip();
    double uclip = getUpperClip();

    if (uclip <= 0.0) { // cases 1, 2, 3 and 4
        if (value >= uclip) {
            return; // bar is not visible
        }
        base = uclip;
        if (value <= lclip) {
            value = lclip;
        }
    } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
        if (value >= uclip) {
            value = uclip;
        } else {
            if (value <= lclip) {
                value = lclip;
            }
        }
    } else { // cases 9, 10, 11 and 12
        if (value <= lclip) {
            return; // bar is not visible
        }
        base = getLowerClip();
        if (value >= uclip) {
            value = uclip;
        }
    }

    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transY1 = rangeAxis.valueToJava2D(base, dataArea, yAxisLocation);
    double transY2 = rangeAxis.valueToJava2D(value, dataArea, yAxisLocation);
    double rectX = Math.min(transY2, transY1);

    double rectHeight = state.getBarWidth();
    double rectWidth = Math.abs(transY2 - transY1);

    Rectangle2D bar = new Rectangle2D.Double(rectX, rectY, rectWidth, rectHeight);
    Paint itemPaint = getItemPaint(row, column);
    GradientPaintTransformer t = getGradientPaintTransformer();
    if (t != null && itemPaint instanceof GradientPaint) {
        itemPaint = t.transform((GradientPaint) itemPaint, bar);
    }
    g2.setPaint(itemPaint);
    g2.fill(bar);

    // draw the outline...
    if (isDrawBarOutline() && 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);
        }
    }

    // CI lines
    Number n = dataset.getUpperValue(row, column);
    Number m = dataset.getLowerValue(row, column);

    if (n != null && m != null) {
        double valueUpperDelta = n.doubleValue();
        double valueLowerDelta = m.doubleValue();

        double highVal = rangeAxis.valueToJava2D(valueUpperDelta, dataArea, yAxisLocation);
        double lowVal = rangeAxis.valueToJava2D(valueLowerDelta, dataArea, yAxisLocation);

        if (this.errorIndicatorPaint != null) {
            g2.setPaint(this.errorIndicatorPaint);
        } else {
            g2.setPaint(getItemOutlinePaint(row, column));
        }
        if (this.errorIndicatorStroke != null) {
            g2.setStroke(this.errorIndicatorStroke);
        } else {
            g2.setStroke(getItemOutlineStroke(row, column));
        }
        Line2D line = null;
        line = new Line2D.Double(lowVal, rectY + rectHeight / 2.0d, highVal, rectY + rectHeight / 2.0d);
        g2.draw(line);
        line = new Line2D.Double(highVal, rectY + rectHeight * 0.25, highVal, rectY + rectHeight * 0.75);
        g2.draw(line);
        line = new Line2D.Double(lowVal, rectY + rectHeight * 0.25, lowVal, rectY + rectHeight * 0.75);
        g2.draw(line);
    }

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

    // add an item entity, if this information is being collected
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addItemEntity(entities, dataset, row, column, bar);
    }

}

From source file:extern.AsymmetricStatisticalBarRenderer.java

/**
 * Draws an item for a plot with a vertical orientation.
 *
 * @param g2  the graphics device.//  w  w  w. j  a va 2 s  .c  om
 * @param state  the renderer state.
 * @param dataArea  the data area.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the data.
 * @param visibleRow  the visible row index.
 * @param row  the row index (zero-based).
 * @param column  the column index (zero-based).
 */
protected void drawVerticalItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea,
        CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis,
        AsymmetricStatisticalCategoryDataset dataset, int visibleRow, int row, int column) {

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();

    // BAR X
    double rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, xAxisLocation);

    int seriesCount = state.getVisibleSeriesCount() >= 0 ? state.getVisibleSeriesCount() : getRowCount();
    int categoryCount = getColumnCount();
    if (seriesCount > 1) {
        double seriesGap = dataArea.getWidth() * getItemMargin() / (categoryCount * (seriesCount - 1));
        rectX = rectX + visibleRow * (state.getBarWidth() + seriesGap);
    } else {
        rectX = rectX + visibleRow * state.getBarWidth();
    }

    // BAR Y
    Number meanValue = dataset.getMeanValue(row, column);
    if (meanValue == null) {
        return;
    }

    double value = meanValue.doubleValue();
    double base = 0.0;
    double lclip = getLowerClip();
    double uclip = getUpperClip();

    if (uclip <= 0.0) { // cases 1, 2, 3 and 4
        if (value >= uclip) {
            return; // bar is not visible
        }
        base = uclip;
        if (value <= lclip) {
            value = lclip;
        }
    } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
        if (value >= uclip) {
            value = uclip;
        } else {
            if (value <= lclip) {
                value = lclip;
            }
        }
    } else { // cases 9, 10, 11 and 12
        if (value <= lclip) {
            return; // bar is not visible
        }
        base = getLowerClip();
        if (value >= uclip) {
            value = uclip;
        }
    }

    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transY1 = rangeAxis.valueToJava2D(base, dataArea, yAxisLocation);
    double transY2 = rangeAxis.valueToJava2D(value, dataArea, yAxisLocation);
    double rectY = Math.min(transY2, transY1);

    double rectWidth = state.getBarWidth();
    double rectHeight = Math.abs(transY2 - transY1);

    Rectangle2D bar = new Rectangle2D.Double(rectX, rectY, rectWidth, rectHeight);
    Paint itemPaint = getItemPaint(row, column);
    GradientPaintTransformer t = getGradientPaintTransformer();
    if (t != null && itemPaint instanceof GradientPaint) {
        itemPaint = t.transform((GradientPaint) itemPaint, bar);
    }
    g2.setPaint(itemPaint);
    g2.fill(bar);
    // draw the outline...
    if (isDrawBarOutline() && 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);
        }
    }

    // CI lines      
    Number n = dataset.getUpperValue(row, column);
    Number m = dataset.getLowerValue(row, column);

    if (n != null && m != null) {
        double valueUpperDelta = n.doubleValue();
        double valueLowerDelta = m.doubleValue();

        if (valueLowerDelta > meanValue.doubleValue()) {
            double temp = valueLowerDelta - meanValue.doubleValue();
            valueLowerDelta = meanValue.doubleValue() - temp;
        }

        if (valueUpperDelta < meanValue.doubleValue()) {
            double temp = meanValue.doubleValue() - valueUpperDelta;
            valueUpperDelta = temp;
        }

        double highVal = rangeAxis.valueToJava2D(valueUpperDelta, dataArea, yAxisLocation);
        double lowVal = rangeAxis.valueToJava2D(valueLowerDelta, dataArea, yAxisLocation);

        if (this.errorIndicatorPaint != null) {
            g2.setPaint(this.errorIndicatorPaint);
        } else {
            g2.setPaint(getItemOutlinePaint(row, column));
        }
        if (this.errorIndicatorStroke != null) {
            g2.setStroke(this.errorIndicatorStroke);
        } else {
            g2.setStroke(getItemOutlineStroke(row, column));
        }

        Line2D line = null;
        line = new Line2D.Double(rectX + rectWidth / 2.0d, lowVal, rectX + rectWidth / 2.0d, highVal);
        g2.draw(line);
        line = new Line2D.Double(rectX + rectWidth / 2.0d - 5.0d, highVal, rectX + rectWidth / 2.0d + 5.0d,
                highVal);
        g2.draw(line);
        line = new Line2D.Double(rectX + rectWidth / 2.0d - 5.0d, lowVal, rectX + rectWidth / 2.0d + 5.0d,
                lowVal);
        g2.draw(line);
    }

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

    // add an item entity, if this information is being collected
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addItemEntity(entities, dataset, row, column, bar);
    }
}

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.renderer.HierarchicalBarRenderer.java

/**
 * Draws the bar for one item in the dataset.
 *
 * @param g2/*from ww w.  j a  va 2s .c o  m*/
 *            the graphics device.
 * @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 data
 *            the data.
 * @param row
 *            the row index (zero-based).
 * @param column
 *            the column index (zero-based).
 * @param pass
 *            the pass index.
 */
@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset data, int row, int column, int pass) {
    // nothing is drawn for null values...
    Number dataValue = data.getValue(row, column);
    if (dataValue == null) {
        return;
    }

    // BAR X
    double rectX = domainAxis.getCategoryMiddle(column, this.getColumnCount(), dataArea,
            plot.getDomainAxisEdge()) - (state.getBarWidth() / 2.0);

    int seriesCount = this.getRowCount();

    // BAR Y
    double value = dataValue.doubleValue();
    double base = 0.0;
    double lclip = this.getLowerClip();
    double uclip = this.getUpperClip();

    if (uclip <= 0.0) { // cases 1, 2, 3 and 4
        if (value >= uclip) {
            return; // bar is not visible
        }
        base = uclip;
        if (value <= lclip) {
            value = lclip;
        }
    } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
        if (value >= uclip) {
            value = uclip;
        } else {
            if (value <= lclip) {
                value = lclip;
            }
        }
    } else { // cases 9, 10, 11 and 12
        if (value <= lclip) {
            return; // bar is not visible
        }
        base = this.getLowerClip();
        if (value >= uclip) {
            value = uclip;
        }
    }

    RectangleEdge edge = plot.getRangeAxisEdge();
    double transY1 = rangeAxis.valueToJava2D(base, dataArea, edge);
    double transY2 = rangeAxis.valueToJava2D(value, dataArea, edge);
    double rectY = Math.min(transY2, transY1);

    double rectWidth = state.getBarWidth();
    double rectHeight = Math.abs(transY2 - transY1);

    // draw the bar...
    double shift = 0.0;
    rectWidth = 0.0;
    double widthFactor = 1.0;
    double seriesBarWidth = this.getSeriesBarWidth(row);
    if (!Double.isNaN(seriesBarWidth)) {
        widthFactor = seriesBarWidth;
    }
    rectWidth = widthFactor * state.getBarWidth();
    rectX = rectX + (((1 - widthFactor) * state.getBarWidth()) / 2.0);
    if (seriesCount > 1) {
        // needs to be improved !!!
        shift = (rectWidth * 0.20) / (seriesCount - 1);
    }

    Rectangle2D bar = new Rectangle2D.Double((rectX + ((seriesCount - 1 - row) * shift)), rectY,
            (rectWidth - ((seriesCount - 1 - row) * shift * 2)), rectHeight);

    double rrX;
    double rrY;
    double rrW;
    double rrH;
    if (row == 0) {
        @SuppressWarnings("unchecked")
        Iterator it = this.datasetTree[column].getDescendants();
        int numElement = -1;
        while (it.hasNext()) {
            try {
                Element elt = (Element) it.next();
                numElement++;
                String name = elt.getAttributeValue("name");
                dataValue = Double.valueOf(elt.getAttributeValue("avg"));
                // System.out.println("["+column+"] "+name+" \t-->
                // "+dataValue);
                // BAR X
                rectX = domainAxis.getCategoryMiddle(column, this.getColumnCount(), dataArea,
                        plot.getDomainAxisEdge()) - (state.getBarWidth() / 2.0);

                seriesCount = this.getRowCount();

                // BAR Y
                value = dataValue.doubleValue();
                base = 0.0;
                lclip = this.getLowerClip();
                uclip = this.getUpperClip();

                if (uclip <= 0.0) { // cases 1, 2, 3 and 4
                    if (value >= uclip) {
                        return; // bar is not visible
                    }
                    base = uclip;
                    if (value <= lclip) {
                        value = lclip;
                    }
                } else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
                    if (value >= uclip) {
                        value = uclip;
                    } else {
                        if (value <= lclip) {
                            value = lclip;
                        }
                    }
                } else { // cases 9, 10, 11 and 12
                    if (value <= lclip) {
                        return; // bar is not visible
                    }
                    base = this.getLowerClip();
                    if (value >= uclip) {
                        value = uclip;
                    }
                }

                edge = plot.getRangeAxisEdge();
                transY1 = rangeAxis.valueToJava2D(base, dataArea, edge);
                transY2 = rangeAxis.valueToJava2D(value, dataArea, edge);
                rectY = Math.min(transY2, transY1);

                rectWidth = state.getBarWidth();
                rectHeight = Math.abs(transY2 - transY1);

                // draw the bar...
                shift = 0.0;
                rectWidth = 0.0;
                widthFactor = 1.0;
                seriesBarWidth = this.getSeriesBarWidth(row);
                if (!Double.isNaN(seriesBarWidth)) {
                    widthFactor = seriesBarWidth;
                }
                rectWidth = widthFactor * state.getBarWidth();
                rectX = rectX + (((1 - widthFactor) * state.getBarWidth()) / 2.0);
                if (seriesCount > 1) {
                    // needs to be improved !!!
                    shift = (rectWidth * 0.20) / (seriesCount - 1);
                }
                rrX = (rectX + ((seriesCount - 1 - row) * shift));
                rrY = rectY;
                rrW = (rectWidth - ((seriesCount - 1 - row) * shift * 2));
                rrH = rectHeight;

                // IMPORTANT NOTE :
                // dev attribute is used to save width of the element
                // min attribute is used to save X position of the element
                // max attribute is used to save the number of child already
                // managed
                if (numElement == 0) {
                    elt.setAttribute("dev", "" + rrW);
                    elt.setAttribute("min", "" + rrX);
                    elt.setAttribute("max", "0");
                } else {
                    Element parent = elt.getParentElement();

                    // System.out.println(" Parent
                    // "+parent.getAttributeValue("name")
                    // + " rrX/rrW/child -> "
                    // + parent.getAttributeValue("min")+"/"
                    // + parent.getAttributeValue("dev")+"/"
                    // + parent.getAttributeValue("max") );
                    double pW = Double.valueOf(parent.getAttributeValue("dev"));
                    double pX = Double.valueOf(parent.getAttributeValue("min"));
                    int numChild = Integer.valueOf(parent.getAttributeValue("max"));

                    rrW = pW / parent.getChildren().size();
                    rrX = pX + (rrW * numChild);
                    rrX += HierarchicalBarRenderer.INCLUSION_MARGIN;
                    rrW -= (HierarchicalBarRenderer.INCLUSION_MARGIN * 2);
                    elt.setAttribute("dev", "" + rrW);
                    elt.setAttribute("min", "" + rrX);
                    parent.setAttribute("max", "" + (numChild + 1));
                }

                RoundRectangle2D rbar = new RoundRectangle2D.Double(rrX, rrY, rrW, rrH,
                        HierarchicalBarRenderer.CORNER, HierarchicalBarRenderer.CORNER);

                Rectangle2D childSumLine = null;
                double childSum = Double.valueOf(elt.getAttributeValue("sum"));
                transY1 = rangeAxis.valueToJava2D(base, dataArea, edge);
                transY2 = rangeAxis.valueToJava2D(childSum, dataArea, edge);
                rectY = Math.min(transY2, transY1);

                childSum = (childSum / dataValue.doubleValue()) * rrH;
                if ((childSum < rrH) && (childSum > 0) && ((childSum / rrH) < 0.95)) {
                    childSumLine = new Rectangle2D.Double(rrX, rectY, rrW, 1);
                }
                Paint itemPaint = this.getItemPaintFromName(name, this.series, column);
                GradientPaintTransformer t = this.getGradientPaintTransformer();
                if ((t != null) && itemPaint instanceof GradientPaint) {
                    itemPaint = t.transform((GradientPaint) itemPaint, bar);
                }
                g2.setPaint(itemPaint);

                Color c = g2.getColor();
                g2.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), this.alpha));
                g2.fill(rbar);
                g2.setColor(Color.DARK_GRAY);
                if (childSumLine != null) {
                    g2.fill(childSumLine);
                }

                // draw the outline...
                if (this.isDrawBarOutline()
                        && (state.getBarWidth() > BarRenderer.BAR_OUTLINE_WIDTH_THRESHOLD)) {
                    Stroke stroke = this.getItemOutlineStroke(row, column);
                    Paint paint = this.getItemOutlinePaint(row, column);
                    if ((stroke != null) && (paint != null)) {
                        g2.setStroke(stroke);
                        g2.setPaint(paint);
                        g2.draw(rbar);
                    }
                }
            } catch (ClassCastException e) {
                continue;
            }
        }
    }

    // ////////////////////////////

    // draw the item labels if there are any...
    double transX1 = rangeAxis.valueToJava2D(base, dataArea, edge);
    double transX2 = rangeAxis.valueToJava2D(value, dataArea, edge);

    CategoryItemLabelGenerator generator = this.getItemLabelGenerator(row, column);
    if ((generator != null) && this.isItemLabelVisible(row, column)) {
        this.drawItemLabel(g2, data, row, column, plot, generator, bar, (transX1 > transX2));
    }

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

From source file:edu.dlnu.liuwenpeng.render.BarRenderer.java

/**    
* Draws the bar for a single (series, category) data item.    
*    //from  www  .ja v a 2  s .  c  o  m
* @param g2  the graphics device.    
* @param state  the renderer state.    
* @param dataArea  the data area.    
* @param plot  the plot.    
* @param domainAxis  the domain axis.    
* @param rangeAxis  the range axis.    
* @param dataset  the dataset.    
* @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 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, row, column);
    double[] barL0L1 = calculateBarL0L1(value);
    if (barL0L1 == null) {
        return; // the bar is not visible    
    }

    RectangleEdge edge = plot.getRangeAxisEdge();
    double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
    double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);

    // in the following code, barL0 is (in Java2D coordinates) the LEFT    
    // end of the bar for a horizontal bar chart, and the TOP end of the    
    // bar for a vertical bar chart.  Whether this is the BASE of the bar    
    // or not depends also on (a) whether the data value is 'negative'    
    // relative to the base value and (b) whether or not the range axis is    
    // inverted.  This only matters if/when we apply the minimumBarLength    
    // attribute, because we should extend the non-base end of the bar    
    boolean positive = (value >= this.base);
    boolean inverted = rangeAxis.isInverted();
    double barL0 = Math.min(transL0, transL1);
    double barLength = Math.abs(transL1 - transL0);
    double barLengthAdj = 0.0;
    if (barLength > 0.0 && barLength < getMinimumBarLength()) {
        barLengthAdj = getMinimumBarLength() - barLength;
    }
    double barL0Adj = 0.0;
    if (orientation == PlotOrientation.HORIZONTAL) {
        if (positive && inverted || !positive && !inverted) {
            barL0Adj = barLengthAdj;
        }
    } else {
        if (positive && !inverted || !positive && inverted) {
            barL0Adj = barLengthAdj;
        }
    }

    // draw the bar...    
    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0 - barL0Adj, barW0, barLength + barLengthAdj, state.getBarWidth());
    } else {

        bar = new Rectangle2D.Double(barW0, barL0 - barL0Adj, state.getBarWidth(), barLength + barLengthAdj);
    }
    Paint itemPaint = getItemPaint(row, column);
    if (dataset.getValue(row, column).doubleValue() >= 0) {
        itemPaint = Color.red;

    } else {
        itemPaint = Color.GREEN;
    }
    GradientPaintTransformer t = getGradientPaintTransformer();
    if (t != null && itemPaint instanceof GradientPaint) {
        itemPaint = t.transform((GradientPaint) itemPaint, bar);
    }
    g2.setPaint(itemPaint);
    g2.fill(bar);

    // draw the outline...    
    if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
        Stroke stroke = getItemOutlineStroke(row, column);
        Paint paint = getItemOutlinePaint(row, column);

        if (dataset.getValue(row, column).doubleValue() >= 0) {
            paint = Color.red;

        } else {
            paint = Color.green;
        }
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);

            g2.setPaint(paint);
            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));
    }

    // add an item entity, if this information is being collected    
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addItemEntity(entities, dataset, row, column, bar);
    }

}