Search in sources :

Example 21 with PlotOrientation

use of org.jfree.chart.plot.PlotOrientation in project SIMVA-SoS by SESoS.

the class GroupedStackedBarRenderer method calculateBarWidth.

/**
 * Calculates the bar width and stores it in the renderer state.  We
 * override the method in the base class to take account of the
 * series-to-group mapping.
 *
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param rendererIndex  the renderer index.
 * @param state  the renderer state.
 */
@Override
protected void calculateBarWidth(CategoryPlot plot, Rectangle2D dataArea, int rendererIndex, CategoryItemRendererState state) {
    // calculate the bar width
    CategoryAxis xAxis = plot.getDomainAxisForDataset(rendererIndex);
    CategoryDataset data = plot.getDataset(rendererIndex);
    if (data != null) {
        PlotOrientation orientation = plot.getOrientation();
        double space = 0.0;
        if (orientation == PlotOrientation.HORIZONTAL) {
            space = dataArea.getHeight();
        } else if (orientation == PlotOrientation.VERTICAL) {
            space = dataArea.getWidth();
        }
        double maxWidth = space * getMaximumBarWidth();
        int groups = this.seriesToGroupMap.getGroupCount();
        int categories = data.getColumnCount();
        int columns = groups * categories;
        double categoryMargin = 0.0;
        double itemMargin = 0.0;
        if (categories > 1) {
            categoryMargin = xAxis.getCategoryMargin();
        }
        if (groups > 1) {
            itemMargin = getItemMargin();
        }
        double used = space * (1 - xAxis.getLowerMargin() - xAxis.getUpperMargin() - categoryMargin - itemMargin);
        if (columns > 0) {
            state.setBarWidth(Math.min(used / columns, maxWidth));
        } else {
            state.setBarWidth(Math.min(used, maxWidth));
        }
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) CategoryAxis(org.jfree.chart.axis.CategoryAxis) CategoryDataset(org.jfree.data.category.CategoryDataset)

Example 22 with PlotOrientation

use of org.jfree.chart.plot.PlotOrientation in project SIMVA-SoS by SESoS.

the class GroupedStackedBarRenderer method drawItem.

/**
 * Draws a stacked bar for a specific item.
 *
 * @param g2  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 dataset  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 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();
    Comparable group = this.seriesToGroupMap.getGroup(dataset.getRowKey(row));
    PlotOrientation orientation = plot.getOrientation();
    double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, row, column);
    double positiveBase = 0.0;
    double negativeBase = 0.0;
    for (int i = 0; i < row; i++) {
        if (group.equals(this.seriesToGroupMap.getGroup(dataset.getRowKey(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;
    boolean positive = (value > 0.0);
    boolean inverted = rangeAxis.isInverted();
    RectangleEdge barBase;
    if (orientation == PlotOrientation.HORIZONTAL) {
        if (positive && inverted || !positive && !inverted) {
            barBase = RectangleEdge.RIGHT;
        } else {
            barBase = RectangleEdge.LEFT;
        }
    } else {
        if (positive && !inverted || !positive && inverted) {
            barBase = RectangleEdge.BOTTOM;
        } else {
            barBase = RectangleEdge.TOP;
        }
    }
    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;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
    } else {
        bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
    }
    getBarPainter().paintBar(g2, this, row, column, bar, barBase);
    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null && isItemLabelVisible(row, column)) {
        drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
    }
    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            addItemEntity(entities, dataset, row, column, bar);
        }
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) EntityCollection(org.jfree.chart.entity.EntityCollection) Rectangle2D(java.awt.geom.Rectangle2D) CategoryItemLabelGenerator(org.jfree.chart.labels.CategoryItemLabelGenerator) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 23 with PlotOrientation

use of org.jfree.chart.plot.PlotOrientation in project SIMVA-SoS by SESoS.

the class HighLowRenderer method drawItem.

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the area within which the plot is being drawn.
 * @param info  collects information about the drawing.
 * @param plot  the plot (can be used to obtain standard color
 *              information etc).
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param crosshairState  crosshair information for the plot
 *                        (<code>null</code> permitted).
 * @param pass  the pass index.
 */
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) {
    double x = dataset.getXValue(series, item);
    if (!domainAxis.getRange().contains(x)) {
        // the x value is not within the axis range
        return;
    }
    double xx = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
    // setup for collecting optional entity info...
    Shape entityArea = null;
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge location = plot.getRangeAxisEdge();
    Paint itemPaint = getItemPaint(series, item);
    Stroke itemStroke = getItemStroke(series, item);
    g2.setPaint(itemPaint);
    g2.setStroke(itemStroke);
    if (dataset instanceof OHLCDataset) {
        OHLCDataset hld = (OHLCDataset) dataset;
        double yHigh = hld.getHighValue(series, item);
        double yLow = hld.getLowValue(series, item);
        if (!Double.isNaN(yHigh) && !Double.isNaN(yLow)) {
            double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, location);
            double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, location);
            if (orientation == PlotOrientation.HORIZONTAL) {
                g2.draw(new Line2D.Double(yyLow, xx, yyHigh, xx));
                entityArea = new Rectangle2D.Double(Math.min(yyLow, yyHigh), xx - 1.0, Math.abs(yyHigh - yyLow), 2.0);
            } else if (orientation == PlotOrientation.VERTICAL) {
                g2.draw(new Line2D.Double(xx, yyLow, xx, yyHigh));
                entityArea = new Rectangle2D.Double(xx - 1.0, Math.min(yyLow, yyHigh), 2.0, Math.abs(yyHigh - yyLow));
            }
        }
        double delta = getTickLength();
        if (domainAxis.isInverted()) {
            delta = -delta;
        }
        if (getDrawOpenTicks()) {
            double yOpen = hld.getOpenValue(series, item);
            if (!Double.isNaN(yOpen)) {
                double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea, location);
                if (this.openTickPaint != null) {
                    g2.setPaint(this.openTickPaint);
                } else {
                    g2.setPaint(itemPaint);
                }
                if (orientation == PlotOrientation.HORIZONTAL) {
                    g2.draw(new Line2D.Double(yyOpen, xx + delta, yyOpen, xx));
                } else if (orientation == PlotOrientation.VERTICAL) {
                    g2.draw(new Line2D.Double(xx - delta, yyOpen, xx, yyOpen));
                }
            }
        }
        if (getDrawCloseTicks()) {
            double yClose = hld.getCloseValue(series, item);
            if (!Double.isNaN(yClose)) {
                double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, location);
                if (this.closeTickPaint != null) {
                    g2.setPaint(this.closeTickPaint);
                } else {
                    g2.setPaint(itemPaint);
                }
                if (orientation == PlotOrientation.HORIZONTAL) {
                    g2.draw(new Line2D.Double(yyClose, xx, yyClose, xx - delta));
                } else if (orientation == PlotOrientation.VERTICAL) {
                    g2.draw(new Line2D.Double(xx, yyClose, xx + delta, yyClose));
                }
            }
        }
    } else {
        // with the previous point...
        if (item > 0) {
            double x0 = dataset.getXValue(series, item - 1);
            double y0 = dataset.getYValue(series, item - 1);
            double y = dataset.getYValue(series, item);
            if (Double.isNaN(x0) || Double.isNaN(y0) || Double.isNaN(y)) {
                return;
            }
            double xx0 = domainAxis.valueToJava2D(x0, dataArea, plot.getDomainAxisEdge());
            double yy0 = rangeAxis.valueToJava2D(y0, dataArea, location);
            double yy = rangeAxis.valueToJava2D(y, dataArea, location);
            if (orientation == PlotOrientation.HORIZONTAL) {
                g2.draw(new Line2D.Double(yy0, xx0, yy, xx));
            } else if (orientation == PlotOrientation.VERTICAL) {
                g2.draw(new Line2D.Double(xx0, yy0, xx, yy));
            }
        }
    }
    if (entities != null) {
        addEntity(entities, entityArea, dataset, series, item, 0.0, 0.0);
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) Stroke(java.awt.Stroke) Shape(java.awt.Shape) Rectangle2D(java.awt.geom.Rectangle2D) Paint(java.awt.Paint) Line2D(java.awt.geom.Line2D) OHLCDataset(org.jfree.data.xy.OHLCDataset) EntityCollection(org.jfree.chart.entity.EntityCollection) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 24 with PlotOrientation

use of org.jfree.chart.plot.PlotOrientation in project SIMVA-SoS by SESoS.

the class StackedXYBarRenderer method drawItem.

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the area within which the plot is being drawn.
 * @param info  collects information about the drawing.
 * @param plot  the plot (can be used to obtain standard color information
 *              etc).
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param crosshairState  crosshair information for the plot
 *                        (<code>null</code> permitted).
 * @param pass  the pass index.
 */
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) {
    if (!getItemVisible(series, item)) {
        return;
    }
    if (!(dataset instanceof IntervalXYDataset && dataset instanceof TableXYDataset)) {
        String message = "dataset (type " + dataset.getClass().getName() + ") has wrong type:";
        boolean and = false;
        if (!IntervalXYDataset.class.isAssignableFrom(dataset.getClass())) {
            message += " it is no IntervalXYDataset";
            and = true;
        }
        if (!TableXYDataset.class.isAssignableFrom(dataset.getClass())) {
            if (and) {
                message += " and";
            }
            message += " it is no TableXYDataset";
        }
        throw new IllegalArgumentException(message);
    }
    IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
    double value = intervalDataset.getYValue(series, item);
    if (Double.isNaN(value)) {
        return;
    }
    // if we are rendering the values as percentages, we need to calculate
    // the total for the current item.  Unfortunately here we end up
    // repeating the calculation more times than is strictly necessary -
    // hopefully I'll come back to this and find a way to add the
    // total(s) to the renderer state.  The other problem is we implicitly
    // assume the dataset has no negative values...perhaps that can be
    // fixed too.
    double total = 0.0;
    if (this.renderAsPercentages) {
        total = DatasetUtilities.calculateStackTotal((TableXYDataset) dataset, item);
        value = value / total;
    }
    double positiveBase = 0.0;
    double negativeBase = 0.0;
    for (int i = 0; i < series; i++) {
        double v = dataset.getYValue(i, item);
        if (!Double.isNaN(v) && isSeriesVisible(i)) {
            if (this.renderAsPercentages) {
                v = v / total;
            }
            if (v > 0) {
                positiveBase = positiveBase + v;
            } else {
                negativeBase = negativeBase + v;
            }
        }
    }
    double translatedBase;
    double translatedValue;
    RectangleEdge edgeR = plot.getRangeAxisEdge();
    if (value > 0.0) {
        translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, edgeR);
        translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, edgeR);
    } else {
        translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, edgeR);
        translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, edgeR);
    }
    RectangleEdge edgeD = plot.getDomainAxisEdge();
    double startX = intervalDataset.getStartXValue(series, item);
    if (Double.isNaN(startX)) {
        return;
    }
    double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, edgeD);
    double endX = intervalDataset.getEndXValue(series, item);
    if (Double.isNaN(endX)) {
        return;
    }
    double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, edgeD);
    double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
    double translatedHeight = Math.abs(translatedValue - translatedBase);
    if (getMargin() > 0.0) {
        double cut = translatedWidth * getMargin();
        translatedWidth = translatedWidth - cut;
        translatedStartX = translatedStartX + cut / 2;
    }
    Rectangle2D bar = null;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(Math.min(translatedBase, translatedValue), Math.min(translatedEndX, translatedStartX), translatedHeight, translatedWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        bar = new Rectangle2D.Double(Math.min(translatedStartX, translatedEndX), Math.min(translatedBase, translatedValue), translatedWidth, translatedHeight);
    } else {
        throw new IllegalStateException();
    }
    boolean positive = (value > 0.0);
    boolean inverted = rangeAxis.isInverted();
    RectangleEdge barBase;
    if (orientation == PlotOrientation.HORIZONTAL) {
        if (positive && inverted || !positive && !inverted) {
            barBase = RectangleEdge.RIGHT;
        } else {
            barBase = RectangleEdge.LEFT;
        }
    } else {
        if (positive && !inverted || !positive && inverted) {
            barBase = RectangleEdge.BOTTOM;
        } else {
            barBase = RectangleEdge.TOP;
        }
    }
    if (pass == 0) {
        if (getShadowsVisible()) {
            getBarPainter().paintBarShadow(g2, this, series, item, bar, barBase, false);
        }
    } else if (pass == 1) {
        getBarPainter().paintBar(g2, this, series, item, bar, barBase);
        // add an entity for the item...
        if (info != null) {
            EntityCollection entities = info.getOwner().getEntityCollection();
            if (entities != null) {
                addEntity(entities, bar, dataset, series, item, bar.getCenterX(), bar.getCenterY());
            }
        }
    } else if (pass == 2) {
        // been drawn...
        if (isItemLabelVisible(series, item)) {
            XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
            drawItemLabel(g2, dataset, series, item, plot, generator, bar, value < 0.0);
        }
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) TableXYDataset(org.jfree.data.xy.TableXYDataset) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) Rectangle2D(java.awt.geom.Rectangle2D) EntityCollection(org.jfree.chart.entity.EntityCollection) XYItemLabelGenerator(org.jfree.chart.labels.XYItemLabelGenerator) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 25 with PlotOrientation

use of org.jfree.chart.plot.PlotOrientation in project SIMVA-SoS by SESoS.

the class StackedBarRenderer method calculateBarWidth.

/**
 * Calculates the bar width and stores it in the renderer state.
 *
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param rendererIndex  the renderer index.
 * @param state  the renderer state.
 */
@Override
protected void calculateBarWidth(CategoryPlot plot, Rectangle2D dataArea, int rendererIndex, CategoryItemRendererState state) {
    // calculate the bar width
    CategoryAxis xAxis = plot.getDomainAxisForDataset(rendererIndex);
    CategoryDataset data = plot.getDataset(rendererIndex);
    if (data != null) {
        PlotOrientation orientation = plot.getOrientation();
        double space = 0.0;
        if (orientation == PlotOrientation.HORIZONTAL) {
            space = dataArea.getHeight();
        } else if (orientation == PlotOrientation.VERTICAL) {
            space = dataArea.getWidth();
        }
        double maxWidth = space * getMaximumBarWidth();
        int columns = data.getColumnCount();
        double categoryMargin = 0.0;
        if (columns > 1) {
            categoryMargin = xAxis.getCategoryMargin();
        }
        double used = space * (1 - xAxis.getLowerMargin() - xAxis.getUpperMargin() - categoryMargin);
        if (columns > 0) {
            state.setBarWidth(Math.min(used / columns, maxWidth));
        } else {
            state.setBarWidth(Math.min(used, maxWidth));
        }
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) CategoryAxis(org.jfree.chart.axis.CategoryAxis) CategoryDataset(org.jfree.data.category.CategoryDataset)

Aggregations

PlotOrientation (org.jfree.chart.plot.PlotOrientation)100 Paint (java.awt.Paint)52 RectangleEdge (org.jfree.ui.RectangleEdge)49 Rectangle2D (java.awt.geom.Rectangle2D)40 EntityCollection (org.jfree.chart.entity.EntityCollection)39 Line2D (java.awt.geom.Line2D)29 Stroke (java.awt.Stroke)20 Shape (java.awt.Shape)19 GeneralPath (java.awt.geom.GeneralPath)14 Range (org.jfree.data.Range)14 GradientPaint (java.awt.GradientPaint)13 JFreeChart (org.jfree.chart.JFreeChart)11 CategoryAxis (org.jfree.chart.axis.CategoryAxis)11 CategoryDataset (org.jfree.data.category.CategoryDataset)11 Point2D (java.awt.geom.Point2D)9 CategoryItemLabelGenerator (org.jfree.chart.labels.CategoryItemLabelGenerator)9 IntervalXYDataset (org.jfree.data.xy.IntervalXYDataset)7 AlphaComposite (java.awt.AlphaComposite)6 BasicStroke (java.awt.BasicStroke)6 Composite (java.awt.Composite)6