Search in sources :

Example 1 with TableXYDataset

use of org.jfree.data.xy.TableXYDataset in project SIMVA-SoS by SESoS.

the class StackedXYAreaRenderer2Test method testFindRangeBounds.

/**
 * Check that the renderer is calculating the range bounds correctly.
 */
@Test
public void testFindRangeBounds() {
    TableXYDataset dataset = RendererXYPackageUtils.createTestTableXYDataset();
    JFreeChart chart = ChartFactory.createStackedXYAreaChart("Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    StackedXYAreaRenderer2 renderer = new StackedXYAreaRenderer2();
    plot.setRenderer(renderer);
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    Range bounds = rangeAxis.getRange();
    assertTrue(bounds.contains(6.0));
    assertTrue(bounds.contains(8.0));
    // try null argument
    assertNull(renderer.findRangeBounds(null));
    // try empty dataset
    assertNull(renderer.findRangeBounds(new DefaultTableXYDataset()));
}
Also used : DefaultTableXYDataset(org.jfree.data.xy.DefaultTableXYDataset) NumberAxis(org.jfree.chart.axis.NumberAxis) XYPlot(org.jfree.chart.plot.XYPlot) TableXYDataset(org.jfree.data.xy.TableXYDataset) DefaultTableXYDataset(org.jfree.data.xy.DefaultTableXYDataset) Range(org.jfree.data.Range) JFreeChart(org.jfree.chart.JFreeChart) Test(org.junit.Test)

Example 2 with TableXYDataset

use of org.jfree.data.xy.TableXYDataset 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 3 with TableXYDataset

use of org.jfree.data.xy.TableXYDataset in project SIMVA-SoS by SESoS.

the class StackedXYAreaRenderer2 method findRangeBounds.

/**
 * Returns the range of values the renderer requires to display all the
 * items from the specified dataset.
 *
 * @param dataset  the dataset (<code>null</code> permitted).
 *
 * @return The range (or <code>null</code> if the dataset is
 *         <code>null</code> or empty).
 */
@Override
public Range findRangeBounds(XYDataset dataset) {
    if (dataset == null) {
        return null;
    }
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    TableXYDataset d = (TableXYDataset) dataset;
    int itemCount = d.getItemCount();
    for (int i = 0; i < itemCount; i++) {
        double[] stackValues = getStackValues((TableXYDataset) dataset, d.getSeriesCount(), i);
        min = Math.min(min, stackValues[0]);
        max = Math.max(max, stackValues[1]);
    }
    if (min == Double.POSITIVE_INFINITY) {
        return null;
    }
    return new Range(min, max);
}
Also used : TableXYDataset(org.jfree.data.xy.TableXYDataset) Range(org.jfree.data.Range) Paint(java.awt.Paint)

Example 4 with TableXYDataset

use of org.jfree.data.xy.TableXYDataset in project SIMVA-SoS by SESoS.

the class DatasetUtilitiesTest method testFindStackedRangeBoundsForTableXYDataset1.

/**
 * Some checks for the findStackedRangeBounds() method.
 */
@Test
public void testFindStackedRangeBoundsForTableXYDataset1() {
    TableXYDataset d2 = createTableXYDataset1();
    Range r = DatasetUtilities.findStackedRangeBounds(d2);
    assertEquals(-2.0, r.getLowerBound(), EPSILON);
    assertEquals(2.0, r.getUpperBound(), EPSILON);
}
Also used : DefaultTableXYDataset(org.jfree.data.xy.DefaultTableXYDataset) TableXYDataset(org.jfree.data.xy.TableXYDataset) Range(org.jfree.data.Range) Test(org.junit.Test)

Example 5 with TableXYDataset

use of org.jfree.data.xy.TableXYDataset in project SIMVA-SoS by SESoS.

the class StackedXYAreaRenderer2 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 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  information about crosshairs on a plot.
 * @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) {
    // setup for collecting optional entity info...
    Shape entityArea;
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }
    TableXYDataset tdataset = (TableXYDataset) dataset;
    PlotOrientation orientation = plot.getOrientation();
    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(y1)) {
        y1 = 0.0;
    }
    double[] stack1 = getStackValues(tdataset, series, item);
    // get the previous point and the next point so we can calculate a
    // "hot spot" for the area (used by the chart entity)...
    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[] stack0 = getStackValues(tdataset, series, Math.max(item - 1, 0));
    int itemCount = dataset.getItemCount(series);
    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[] stack2 = getStackValues(tdataset, series, Math.min(item + 1, itemCount - 1));
    double xleft = (x0 + x1) / 2.0;
    double xright = (x1 + x2) / 2.0;
    double[] stackLeft = averageStackValues(stack0, stack1);
    double[] stackRight = averageStackValues(stack1, stack2);
    double[] adjStackLeft = adjustedStackValues(stack0, stack1);
    double[] adjStackRight = adjustedStackValues(stack1, stack2);
    RectangleEdge edge0 = plot.getDomainAxisEdge();
    float transX1 = (float) domainAxis.valueToJava2D(x1, dataArea, edge0);
    float transXLeft = (float) domainAxis.valueToJava2D(xleft, dataArea, edge0);
    float transXRight = (float) domainAxis.valueToJava2D(xright, dataArea, edge0);
    if (this.roundXCoordinates) {
        transX1 = Math.round(transX1);
        transXLeft = Math.round(transXLeft);
        transXRight = Math.round(transXRight);
    }
    float transY1;
    RectangleEdge edge1 = plot.getRangeAxisEdge();
    GeneralPath left = new GeneralPath();
    GeneralPath right = new GeneralPath();
    if (y1 >= 0.0) {
        // handle positive value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[1], dataArea, edge1);
        float transStack1 = (float) rangeAxis.valueToJava2D(stack1[1], dataArea, edge1);
        float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[1], dataArea, edge1);
        // LEFT POLYGON
        if (y0 >= 0.0) {
            double yleft = (y0 + y1) / 2.0 + stackLeft[1];
            float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            if (orientation == PlotOrientation.VERTICAL) {
                left.moveTo(transX1, transY1);
                left.lineTo(transX1, transStack1);
                left.lineTo(transXLeft, transStackLeft);
                left.lineTo(transXLeft, transYLeft);
            } else {
                left.moveTo(transY1, transX1);
                left.lineTo(transStack1, transX1);
                left.lineTo(transStackLeft, transXLeft);
                left.lineTo(transYLeft, transXLeft);
            }
            left.closePath();
        } else {
            if (orientation == PlotOrientation.VERTICAL) {
                left.moveTo(transX1, transStack1);
                left.lineTo(transX1, transY1);
                left.lineTo(transXLeft, transStackLeft);
            } else {
                left.moveTo(transStack1, transX1);
                left.lineTo(transY1, transX1);
                left.lineTo(transStackLeft, transXLeft);
            }
            left.closePath();
        }
        float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[1], dataArea, edge1);
        // RIGHT POLYGON
        if (y2 >= 0.0) {
            double yright = (y1 + y2) / 2.0 + stackRight[1];
            float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            if (orientation == PlotOrientation.VERTICAL) {
                right.moveTo(transX1, transStack1);
                right.lineTo(transX1, transY1);
                right.lineTo(transXRight, transYRight);
                right.lineTo(transXRight, transStackRight);
            } else {
                right.moveTo(transStack1, transX1);
                right.lineTo(transY1, transX1);
                right.lineTo(transYRight, transXRight);
                right.lineTo(transStackRight, transXRight);
            }
            right.closePath();
        } else {
            if (orientation == PlotOrientation.VERTICAL) {
                right.moveTo(transX1, transStack1);
                right.lineTo(transX1, transY1);
                right.lineTo(transXRight, transStackRight);
            } else {
                right.moveTo(transStack1, transX1);
                right.lineTo(transY1, transX1);
                right.lineTo(transStackRight, transXRight);
            }
            right.closePath();
        }
    } else {
        // handle negative value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[0], dataArea, edge1);
        float transStack1 = (float) rangeAxis.valueToJava2D(stack1[0], dataArea, edge1);
        float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[0], dataArea, edge1);
        // LEFT POLYGON
        if (y0 >= 0.0) {
            if (orientation == PlotOrientation.VERTICAL) {
                left.moveTo(transX1, transStack1);
                left.lineTo(transX1, transY1);
                left.lineTo(transXLeft, transStackLeft);
            } else {
                left.moveTo(transStack1, transX1);
                left.lineTo(transY1, transX1);
                left.lineTo(transStackLeft, transXLeft);
            }
            left.clone();
        } else {
            double yleft = (y0 + y1) / 2.0 + stackLeft[0];
            float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            if (orientation == PlotOrientation.VERTICAL) {
                left.moveTo(transX1, transY1);
                left.lineTo(transX1, transStack1);
                left.lineTo(transXLeft, transStackLeft);
                left.lineTo(transXLeft, transYLeft);
            } else {
                left.moveTo(transY1, transX1);
                left.lineTo(transStack1, transX1);
                left.lineTo(transStackLeft, transXLeft);
                left.lineTo(transYLeft, transXLeft);
            }
            left.closePath();
        }
        float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[0], dataArea, edge1);
        // RIGHT POLYGON
        if (y2 >= 0.0) {
            if (orientation == PlotOrientation.VERTICAL) {
                right.moveTo(transX1, transStack1);
                right.lineTo(transX1, transY1);
                right.lineTo(transXRight, transStackRight);
            } else {
                right.moveTo(transStack1, transX1);
                right.lineTo(transY1, transX1);
                right.lineTo(transStackRight, transXRight);
            }
            right.closePath();
        } else {
            double yright = (y1 + y2) / 2.0 + stackRight[0];
            float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            if (orientation == PlotOrientation.VERTICAL) {
                right.moveTo(transX1, transStack1);
                right.lineTo(transX1, transY1);
                right.lineTo(transXRight, transYRight);
                right.lineTo(transXRight, transStackRight);
            } else {
                right.moveTo(transStack1, transX1);
                right.lineTo(transY1, transX1);
                right.lineTo(transYRight, transXRight);
                right.lineTo(transStackRight, transXRight);
            }
            right.closePath();
        }
    }
    // Get series Paint and Stroke
    Paint itemPaint = getItemPaint(series, item);
    if (pass == 0) {
        g2.setPaint(itemPaint);
        g2.fill(left);
        g2.fill(right);
    }
    // add an entity for the item...
    if (entities != null) {
        GeneralPath gp = new GeneralPath(left);
        gp.append(right, false);
        entityArea = gp;
        addEntity(entities, entityArea, dataset, series, item, transX1, transY1);
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) Shape(java.awt.Shape) GeneralPath(java.awt.geom.GeneralPath) EntityCollection(org.jfree.chart.entity.EntityCollection) TableXYDataset(org.jfree.data.xy.TableXYDataset) Paint(java.awt.Paint) Paint(java.awt.Paint) RectangleEdge(org.jfree.ui.RectangleEdge)

Aggregations

TableXYDataset (org.jfree.data.xy.TableXYDataset)10 Range (org.jfree.data.Range)7 Test (org.junit.Test)6 JFreeChart (org.jfree.chart.JFreeChart)5 NumberAxis (org.jfree.chart.axis.NumberAxis)5 XYPlot (org.jfree.chart.plot.XYPlot)5 Paint (java.awt.Paint)3 EntityCollection (org.jfree.chart.entity.EntityCollection)3 PlotOrientation (org.jfree.chart.plot.PlotOrientation)3 DefaultTableXYDataset (org.jfree.data.xy.DefaultTableXYDataset)3 Shape (java.awt.Shape)2 Rectangle2D (java.awt.geom.Rectangle2D)2 RectangleEdge (org.jfree.ui.RectangleEdge)2 Point (java.awt.Point)1 Polygon (java.awt.Polygon)1 Stroke (java.awt.Stroke)1 GeneralPath (java.awt.geom.GeneralPath)1 Stack (java.util.Stack)1 XYItemEntity (org.jfree.chart.entity.XYItemEntity)1 XYItemLabelGenerator (org.jfree.chart.labels.XYItemLabelGenerator)1