Example usage for org.jfree.chart.axis CategoryAxis getCategoryMiddle

List of usage examples for org.jfree.chart.axis CategoryAxis getCategoryMiddle

Introduction

In this page you can find the example usage for org.jfree.chart.axis CategoryAxis getCategoryMiddle.

Prototype

public double getCategoryMiddle(Comparable category, List categories, Rectangle2D area, RectangleEdge edge) 

Source Link

Document

A convenience method that returns the axis coordinate for the centre of a category.

Usage

From source file:edu.cuny.jfree.chart.renderer.category.ValueListShapeRenderer.java

@Override
public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea,
        final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis,
        final CategoryDataset dataset, final int row, final int column, final int pass) {

    final ListCategoryDataset setData = (ListCategoryDataset) dataset;

    final List list = setData.getList(row, column);
    if (list == null) {
        return;//from   w ww .  j a v a2 s  .c  o m
    }

    final PlotOrientation orientation = plot.getOrientation();
    final double x = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    final Iterator iterator = list.iterator();
    while (iterator.hasNext()) {
        final Number value = (Number) iterator.next();
        final double y = rangeAxis.valueToJava2D(value.doubleValue(), dataArea, plot.getRangeAxisEdge());

        Shape shape = getItemShape(row, column);

        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, y, x);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, x, y);
        }
        if (getItemShapeVisible(row, column)) {
            if (getItemShapeFilled(row, column)) {
                g2.setPaint(getItemPaint(row, column));
                g2.fill(shape);
            } else {
                if (getUseOutlinePaint()) {
                    g2.setPaint(getItemOutlinePaint(row, column));
                } else {
                    g2.setPaint(getItemPaint(row, column));
                }
                g2.setStroke(getItemOutlineStroke(row, column));
                g2.draw(shape);
            }
        }
        g2.setPaint(getItemPaint(row, column));

        if (isItemLabelVisible(row, column)) {
            if (orientation == PlotOrientation.HORIZONTAL) {
                drawItemLabel(g2, orientation, dataset, row, column, y, x, value.doubleValue() < 0.0D);
            } else if (orientation == PlotOrientation.VERTICAL) {
                drawItemLabel(g2, orientation, dataset, row, column, x, y, value.doubleValue() < 0.0D);
            }
        }

        if (state.getInfo() != null) {
            final EntityCollection entities = state.getEntityCollection();
            if ((entities != null) && (shape != null)) {
                String tip = null;
                final 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);
                }
                final CategoryItemEntity entity = new CategoryItemEntity(shape, tip, url, dataset,
                        dataset.getRowKey(row), dataset.getColumnKey(column));
                entities.add(entity);
            }
        }
    }
}

From source file:hudson.util.StackedAreaRenderer2.java

@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {

    // plot non-null values...
    Number dataValue = dataset.getValue(row, column);
    if (dataValue == null) {
        return;//from   ww  w  . j a  v  a2 s  .c om
    }

    double value = dataValue.doubleValue();

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

    double previousHeightx1 = getPreviousHeight(dataset, row, column);
    double y1 = value + previousHeightx1;
    RectangleEdge location = plot.getRangeAxisEdge();
    double yy1 = rangeAxis.valueToJava2D(y1, dataArea, location);

    g2.setPaint(getItemPaint(row, column));
    g2.setStroke(getItemStroke(row, column));

    // add an item entity, if this information is being collected
    EntityCollection entities = state.getEntityCollection();

    // in column zero, the only job to do is draw any visible item labels
    // and this is done in the second pass...
    if (column == 0) {
        if (pass == 1) {
            // draw item labels, if visible
            if (isItemLabelVisible(row, column)) {
                drawItemLabel(g2, plot.getOrientation(), dataset, row, column, xx1, yy1, (y1 < 0.0));
            }
        }
    } else {
        Number previousValue = dataset.getValue(row, column - 1);
        if (previousValue != null) {

            double xx0 = domainAxis.getCategoryMiddle(column - 1, getColumnCount(), dataArea,
                    plot.getDomainAxisEdge());
            double y0 = previousValue.doubleValue();

            // Get the previous height, but this will be different for both
            // y0 and y1 as the previous series values could differ.
            double previousHeightx0 = getPreviousHeight(dataset, row, column - 1);

            // Now stack the current y values on top of the previous values.
            y0 += previousHeightx0;

            // Now translate the previous heights
            double previousHeightxx0 = rangeAxis.valueToJava2D(previousHeightx0, dataArea, location);
            double previousHeightxx1 = rangeAxis.valueToJava2D(previousHeightx1, dataArea, location);

            // Now translate the current y values.
            double yy0 = rangeAxis.valueToJava2D(y0, dataArea, location);

            if (pass == 0) {
                // left half
                Polygon p = new Polygon();
                p.addPoint((int) xx0, (int) yy0);
                p.addPoint((int) (xx0 + xx1) / 2, (int) (yy0 + yy1) / 2);
                p.addPoint((int) (xx0 + xx1) / 2, (int) (previousHeightxx0 + previousHeightxx1) / 2);
                p.addPoint((int) xx0, (int) previousHeightxx0);

                g2.setPaint(getItemPaint(row, column - 1));
                g2.setStroke(getItemStroke(row, column - 1));
                g2.fill(p);

                if (entities != null)
                    addItemEntity(entities, dataset, row, column - 1, p);

                // right half
                p = new Polygon();
                p.addPoint((int) xx1, (int) yy1);
                p.addPoint((int) (xx0 + xx1) / 2, (int) (yy0 + yy1) / 2);
                p.addPoint((int) (xx0 + xx1) / 2, (int) (previousHeightxx0 + previousHeightxx1) / 2);
                p.addPoint((int) xx1, (int) previousHeightxx1);

                g2.setPaint(getItemPaint(row, column));
                g2.setStroke(getItemStroke(row, column));
                g2.fill(p);

                if (entities != null)
                    addItemEntity(entities, dataset, row, column, p);
            } else {
                if (isItemLabelVisible(row, column)) {
                    drawItemLabel(g2, plot.getOrientation(), dataset, row, column, xx1, yy1, (y1 < 0.0));
                }
            }
        }
    }
}

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

/**
 * Draws a stacked bar for a specific item.
 *
 * @param g2  the graphics device./*w  w  w.  j  a  v a2s  . c  o  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);
                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:edu.ucla.stat.SOCR.chart.gui.ExtendedStackedBarRenderer.java

/**
 * Draws a stacked bar for a specific item.
 *
 * @param g2  the graphics device.//from  w ww  .  jav  a2 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);
                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:org.openfaces.component.chart.impl.renderers.LineFillRenderer.java

private double calculateItemXPoint(CategoryPlot plot, CategoryDataset dataSet, CategoryAxis domainAxis,
        Rectangle2D dataArea, int column, int visibleRow, int visibleRowCount) {
    double itemXPoint;
    if (getUseSeriesOffset()) {
        itemXPoint = domainAxis.getCategorySeriesMiddle(column, dataSet.getColumnCount(), visibleRow,
                visibleRowCount, getItemMargin(), dataArea, plot.getDomainAxisEdge());
    } else {//  w w w. j ava 2s .  co m
        itemXPoint = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());
    }

    return itemXPoint;
}

From source file:org.jfree.chart.demo.ExtendedStackedBarRenderer.java

/**
 * Draws a stacked bar for a specific item.
 *
 * @param g2  the graphics device.//from   w w w . j ava2s . c  o 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).
 */
public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea,
        final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis,
        final CategoryDataset dataset, final int row, final int column) {

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

    final double value = dataValue.doubleValue();

    final PlotOrientation orientation = plot.getOrientation();
    final 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++) {
        final Number v = dataset.getValue(i, column);
        if (v != null) {
            final double d = v.doubleValue();
            if (d > 0) {
                positiveBase = positiveBase + d;
            } else {
                negativeBase = negativeBase + d;
            }
        }
    }

    final double translatedBase;
    final double translatedValue;
    final 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);
    }
    final double barL0 = Math.min(translatedBase, translatedValue);
    final 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);
    }
    final 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);
    }

    //        final CategoryLabelGenerator generator = getLabelGenerator(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);
                final double total = calculateSumOfPositiveValuesForCategory(dataset, column);
                //            RefineryUtilities.drawRotatedString(
                //              this.totalFormatter.format(total), g2,
                //            (float) bar.getCenterX(),
                //          (float) (bar.getMinY() - 4.0), 
                ///        TextAnchor.BOTTOM_CENTER, 
                //         TextAnchor.BOTTOM_CENTER, 
                //       0.0
                // );              
            }
        }
    } else {
        if (this.showNegativeTotal) {
            if (isLastNegativeItem(dataset, row, column)) {
                g2.setPaint(Color.black);
                g2.setFont(this.totalLabelFont);
                final double total = calculateSumOfNegativeValuesForCategory(dataset, column);
                /*                    RefineryUtilities.drawRotatedString(
                String.valueOf(total), g2,
                (float) bar.getCenterX(),
                (float) (bar.getMaxY() + 4.0), 
                TextAnchor.TOP_CENTER, 
                TextAnchor.TOP_CENTER, 
                0.0
                                    );              
                  */ }
        }
    }

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        final EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
        if (entities != null) {
            String tip = null;
            final 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);
            }
            final CategoryItemEntity entity = new CategoryItemEntity(bar, tip, url, dataset, row,
                    dataset.getColumnKey(column), column);
            //            entities.addEntity(entity);
        }
    }

}

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  w  w  w .j a va2s.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:org.operamasks.faces.render.graph.CurveAndShapeRenderer.java

private void drawSeriesCurve(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
        ValueAxis rangeAxis, CategoryDataset dataset, int series) {
    // do nothing if item is not visible
    if (!(getItemVisible(series, 0) && (getItemLineVisible(series, 0) || drawArea))) {
        return;//from   w w  w.  j a  va 2  s.  co m
    }

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    PlotOrientation orientation = plot.getOrientation();

    int itemCount = dataset.getColumnCount();
    double[][] points = new double[itemCount][2];
    int count = 0;

    // get data points
    for (int i = 0; i < itemCount; i++) {
        Number value = dataset.getValue(series, i);
        if (value != null) {
            points[count][0] = domainAxis.getCategoryMiddle(i, itemCount, dataArea, xAxisLocation);
            points[count][1] = rangeAxis.valueToJava2D(value.doubleValue(), dataArea, yAxisLocation);
            count++;
        }
    }

    if (count < 2) {
        return;
    }

    // draw curve
    CubicSplineFunction2D f = new CubicSplineFunction2D(points, count);
    GeneralPath path = new GeneralPath();

    double startX = points[0][0];
    double startY = points[0][1];
    double endX = points[count - 1][0];
    double endY = points[count - 1][1];
    double yz = rangeAxis.valueToJava2D(0.0, dataArea, yAxisLocation);

    if (orientation == PlotOrientation.HORIZONTAL) {
        if (drawArea) {
            path.moveTo((float) yz, (float) startX);
            path.lineTo((float) startY, (float) startX);
            for (double x = Math.floor(startX) + 1.0; x < endX; x += 1.0) {
                path.lineTo((float) f.getValue(x), (float) x);
            }
            path.lineTo((float) endY, (float) endX);
            path.lineTo((float) yz, (float) endX);
            path.closePath();
        } else {
            path.moveTo((float) startY, (float) startX);
            for (double x = Math.floor(startX) + 1.0; x < endX; x += 1.0) {
                path.lineTo((float) f.getValue(x), (float) x);
            }
            path.lineTo((float) endY, (float) endX);
        }
    } else {
        if (drawArea) {
            path.moveTo((float) startX, (float) yz);
            path.lineTo((float) startX, (float) startY);
            for (double x = Math.floor(startX) + 1.0; x < endX; x += 1.0) {
                path.lineTo((float) x, (float) f.getValue(x));
            }
            path.lineTo((float) endX, (float) endY);
            path.lineTo((float) endX, (float) yz);
            path.closePath();
        } else {
            path.moveTo((float) startX, (float) startY);
            for (double x = Math.floor(startX) + 1.0; x < endX; x += 1.0) {
                path.lineTo((float) x, (float) f.getValue(x));
            }
            path.lineTo((float) endX, (float) endY);
        }
    }

    Paint paint = getSeriesPaint(series);
    Stroke stroke = getSeriesStroke(series);

    if (drawArea) {
        g2.setPaint(paint);
        g2.fill(path);

        // create paint for outline
        if (paint instanceof Color) {
            paint = ((Color) paint).darker();
        } else if (paint instanceof GradientPaint) {
            paint = ((GradientPaint) paint).getColor1().darker();
        }
    }

    if (getItemLineVisible(series, 0)) {
        g2.setPaint(paint);
        g2.setStroke(stroke);
        g2.draw(path);
    }
}

From source file:org.jfree.chart.demo.GanttRenderer2.java

protected double calculateBarW0(CategoryPlot categoryplot, PlotOrientation plotorientation,
        Rectangle2D rectangle2d, CategoryAxis categoryaxis, CategoryItemRendererState categoryitemrendererstate,
        int i, int j) {
    double d = 0.0D;
    if (plotorientation == PlotOrientation.HORIZONTAL)
        d = rectangle2d.getHeight();/*from   w w  w.jav  a  2s .  c  o  m*/
    else
        d = rectangle2d.getWidth();
    double d1 = categoryaxis.getCategoryStart(j, getColumnCount(), rectangle2d,
            categoryplot.getDomainAxisEdge());
    int k = getRowCount();
    int l = getColumnCount();
    if (k > 1) {
        double d2 = (d * getItemMargin()) / (double) (l * (k - 1));
        double d3 = calculateSeriesWidth(d, categoryaxis, l, k);
        d1 = (d1 + (double) i * (d3 + d2) + d3 / 2D) - categoryitemrendererstate.getBarWidth() / 2D;
    } else {
        d1 = categoryaxis.getCategoryMiddle(j, getColumnCount(), rectangle2d, categoryplot.getDomainAxisEdge())
                - categoryitemrendererstate.getBarWidth() / 2D;
    }
    return d1;
}

From source file:org.openfaces.component.chart.impl.renderers.LineFillRenderer.java

private void processCategoryArea(Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
        ValueAxis rangeAxis, CategoryDataset dataSet, LineFillItemRendererState rendererState, int row,
        int column, double currentValue, double currentItemYPoint, int totalColumns, boolean isFirstItem,
        boolean isLastItem) {
    RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    double zeroRangePoint = calculateItemYPoint(plot, rangeAxis, dataArea, 0.0);
    double categoryStartXPoint = domainAxis.getCategoryStart(column, totalColumns, dataArea, domainAxisEdge);
    double categoryMiddleXPoint = domainAxis.getCategoryMiddle(column, totalColumns, dataArea, domainAxisEdge);
    double categoryEndXPoint = domainAxis.getCategoryEnd(column, totalColumns, dataArea, domainAxisEdge);

    if (isFirstItem) {
        categoryStartXPoint = categoryMiddleXPoint;
    } else if (isLastItem) {
        categoryEndXPoint = categoryMiddleXPoint;
    }//from  w w  w.  j  av a 2 s  .c  o  m

    double previousItemYValue = 0.0;
    if (!isFirstItem) {
        Number previousItemValue = dataSet.getValue(row, column - 1);

        if (previousItemValue != null) {
            previousItemYValue = (previousItemValue.doubleValue() + currentValue) / 2.0;

            if (domainAxis instanceof CategoryAxisAdapter) {
                categoryStartXPoint -= ((CategoryAxisAdapter) domainAxis).calculateCategoryGapSize(totalColumns,
                        dataArea, domainAxisEdge) / 2.0;
            }
        } else {
            categoryStartXPoint = categoryMiddleXPoint;
        }
    }

    double nextItemYValue = 0.0;
    if (!isLastItem) {
        Number nextValue = dataSet.getValue(row, column + 1);
        if (nextValue != null) {
            nextItemYValue = (nextValue.doubleValue() + currentValue) / 2.0;

            if (domainAxis instanceof CategoryAxisAdapter) {
                categoryEndXPoint += ((CategoryAxisAdapter) domainAxis).calculateCategoryGapSize(totalColumns,
                        dataArea, domainAxisEdge) / 2.0;
            }
        } else {
            categoryEndXPoint = categoryMiddleXPoint;
        }
    }

    double previousItemYPoint = rangeAxis.valueToJava2D(previousItemYValue, dataArea, rangeAxisEdge);
    double nextItemYPoint = rangeAxis.valueToJava2D(nextItemYValue, dataArea, rangeAxisEdge);

    Polygon polygon = rendererState.getAreaPolygon();
    if (plot.getOrientation() == PlotOrientation.VERTICAL) {
        polygon.addPoint((int) categoryStartXPoint, (int) zeroRangePoint);
        polygon.addPoint((int) categoryStartXPoint, (int) previousItemYPoint);
        polygon.addPoint((int) categoryMiddleXPoint, (int) currentItemYPoint);
        polygon.addPoint((int) categoryEndXPoint, (int) nextItemYPoint);
        polygon.addPoint((int) categoryEndXPoint, (int) zeroRangePoint);
    } else {
        polygon.addPoint((int) zeroRangePoint, (int) categoryStartXPoint);
        polygon.addPoint((int) previousItemYPoint, (int) categoryStartXPoint);
        polygon.addPoint((int) currentItemYPoint, (int) categoryMiddleXPoint);
        polygon.addPoint((int) nextItemYPoint, (int) categoryEndXPoint);
        polygon.addPoint((int) zeroRangePoint, (int) categoryEndXPoint);
    }
}