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

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

Introduction

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

Prototype

public double getCategoryEnd(int category, int categoryCount, Rectangle2D area, RectangleEdge edge) 

Source Link

Document

Returns the end coordinate for the specified category.

Usage

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;
    }//  ww  w .  j  a v a  2s .c om

    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);
    }
}

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

protected void drawTask(Graphics2D graphics2d, CategoryItemRendererState categoryitemrendererstate,
        Rectangle2D rectangle2d, CategoryPlot categoryplot, CategoryAxis categoryaxis, ValueAxis valueaxis,
        GanttCategoryDataset ganttcategorydataset, int i, int j) {
    PlotOrientation plotorientation = categoryplot.getOrientation();
    org.jfree.ui.RectangleEdge rectangleedge = categoryplot.getRangeAxisEdge();
    Number number = ganttcategorydataset.getEndValue(i, j);
    if (number == null)
        return;// www  .  jav a  2 s .  c o  m
    double d = valueaxis.valueToJava2D(number.doubleValue(), rectangle2d, rectangleedge);
    Number number1 = ganttcategorydataset.getStartValue(i, j);
    if (number1 == null)
        return;
    double d1 = valueaxis.valueToJava2D(number1.doubleValue(), rectangle2d, rectangleedge);
    if (d1 < d) {
        double d2 = d1;
        d1 = d;
        d = d2;
        Number number2 = number1;
        number1 = number;
        number = number2;
    }
    int k = countNonNullValues(ganttcategorydataset, j);
    if (k == 0)
        return;
    int l = countPriorNonNullValues(ganttcategorydataset, j, i);
    double d3 = (categoryaxis.getCategoryEnd(j, getColumnCount(), rectangle2d, categoryplot.getDomainAxisEdge())
            - categoryaxis.getCategoryStart(j, getColumnCount(), rectangle2d, categoryplot.getDomainAxisEdge()))
            / (double) k;
    double d4 = categoryaxis.getCategoryStart(j, getColumnCount(), rectangle2d,
            categoryplot.getDomainAxisEdge()) + d3 * (double) l;
    double d5 = Math.abs(d1 - d);
    java.awt.geom.Rectangle2D.Double double1 = null;
    if (plotorientation == PlotOrientation.HORIZONTAL)
        double1 = new java.awt.geom.Rectangle2D.Double(d, d4, d5, d3);
    else if (plotorientation == PlotOrientation.VERTICAL)
        double1 = new java.awt.geom.Rectangle2D.Double(d4, d1, d3, d5);
    java.awt.geom.Rectangle2D.Double double2 = null;
    java.awt.geom.Rectangle2D.Double double3 = null;
    Number number3 = ganttcategorydataset.getPercentComplete(i, j);
    double d6 = getStartPercent();
    double d7 = getEndPercent();
    if (number3 != null) {
        double d8 = number3.doubleValue();
        if (categoryplot.getOrientation() == PlotOrientation.HORIZONTAL) {
            double2 = new java.awt.geom.Rectangle2D.Double(d, d4 + d6 * d3, d5 * d8, d3 * (d7 - d6));
            double3 = new java.awt.geom.Rectangle2D.Double(d + d5 * d8, d4 + d6 * d3, d5 * (1.0D - d8),
                    d3 * (d7 - d6));
        } else if (categoryplot.getOrientation() == PlotOrientation.VERTICAL) {
            double2 = new java.awt.geom.Rectangle2D.Double(d4 + d6 * d3, d1 + d5 * (1.0D - d8), d3 * (d7 - d6),
                    d5 * d8);
            double3 = new java.awt.geom.Rectangle2D.Double(d4 + d6 * d3, d1, d3 * (d7 - d6), d5 * (1.0D - d8));
        }
    }
    Paint paint = getItemPaint(i, j);
    graphics2d.setPaint(paint);
    graphics2d.fill(double1);
    if (double2 != null) {
        graphics2d.setPaint(getCompletePaint());
        graphics2d.fill(double2);
    }
    if (double3 != null) {
        graphics2d.setPaint(getIncompletePaint());
        graphics2d.fill(double3);
    }
    if (isDrawBarOutline() && categoryitemrendererstate.getBarWidth() > 3D) {
        java.awt.Stroke stroke = getItemOutlineStroke(i, j);
        Paint paint1 = getItemOutlinePaint(i, j);
        if (stroke != null && paint1 != null) {
            graphics2d.setStroke(stroke);
            graphics2d.setPaint(paint1);
            graphics2d.draw(double1);
        }
    }
    org.jfree.chart.labels.CategoryItemLabelGenerator categoryitemlabelgenerator = getItemLabelGenerator(i, j);
    if (categoryitemlabelgenerator != null && isItemLabelVisible(i, j))
        drawItemLabel(graphics2d, ganttcategorydataset, i, j, categoryplot, categoryitemlabelgenerator, double1,
                false);
    if (categoryitemrendererstate.getInfo() != null) {
        EntityCollection entitycollection = categoryitemrendererstate.getEntityCollection();
        if (entitycollection != null) {
            String s = null;
            CategoryToolTipGenerator categorytooltipgenerator = getToolTipGenerator(i, j);
            if (categorytooltipgenerator != null)
                s = categorytooltipgenerator.generateToolTip(ganttcategorydataset, i, j);
            String s1 = null;
            if (getItemURLGenerator(i, j) != null)
                s1 = getItemURLGenerator(i, j).generateURL(ganttcategorydataset, i, j);
            CategoryItemEntity categoryitementity = new CategoryItemEntity(double1, s, s1, ganttcategorydataset,
                    ganttcategorydataset.getRowKey(i), ganttcategorydataset.getColumnKey(j));
            entitycollection.add(categoryitementity);
        }
    }
}

From source file:org.talend.dataprofiler.chart.preview.HideSeriesGanttRenderer.java

/**
 * Draws a single task.//from   w  w w. ja v a 2 s. c o  m
 * 
 * @param g2 the graphics device.
 * @param state the renderer state.
 * @param dataArea the data plot area.
 * @param plot the plot.
 * @param domainAxis the domain axis.
 * @param rangeAxis the range axis.
 * @param dataset the data.
 * @param row the row index (zero-based).
 * @param column the column index (zero-based).
 */
protected void drawTask(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, GanttCategoryDataset dataset, int row, int column) {

    PlotOrientation orientation = plot.getOrientation();

    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;
    }

    // count the number of non-null values
    int totalBars = countNonNullValues(dataset, column);
    if (totalBars == 0) {
        return;
    }
    // count non-null values up to but not including the current value
    int priorBars = countPriorNonNullValues(dataset, column, row);

    // double rectStart = calculateBarW0(plot, orientation, dataArea,
    // domainAxis, state, row, column);
    // double rectBreadth = state.getBarWidth();

    double rectBreadth = (domainAxis.getCategoryEnd(column, getColumnCount(), dataArea,
            plot.getDomainAxisEdge())
            - domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()))
            / totalBars;
    double rectStart = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge())
            + rectBreadth * priorBars;
    double rectLength = Math.abs(java2dValue1 - java2dValue0);

    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(java2dValue0, rectStart, rectLength, rectBreadth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        bar = new Rectangle2D.Double(rectStart, java2dValue1, rectBreadth, rectLength);
    }

    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();
        if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
            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));
        } else if (plot.getOrientation() == PlotOrientation.VERTICAL) {
            completeBar = new Rectangle2D.Double(rectStart + start * rectBreadth,
                    java2dValue1 + rectLength * (1 - p), rectBreadth * (end - start), rectLength * p);
            incompleteBar = new Rectangle2D.Double(rectStart + start * rectBreadth, java2dValue1,
                    rectBreadth * (end - start), rectLength * (1 - p));
        }

    }

    Paint seriesPaint = getItemPaint(row, column);
    g2.setPaint(seriesPaint);
    g2.fill(bar);

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

    // 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);
        }
    }

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

    // 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.pentaho.plugin.jfreereport.reportcharts.backport.StackedAreaRenderer.java

/**
 * Draw a single data item./*from w  w  w.j  av a2s  .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).
 * @param pass       the pass index.
 */
public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea,
        final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis,
        final CategoryDataset dataset, final int row, final int column, final int pass) {

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

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

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

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

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

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

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

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

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

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

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

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

    final float transY1;

    final RectangleEdge edge1 = plot.getRangeAxisEdge();

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

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

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

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

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

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

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

}