Search in sources :

Example 1 with XYItemLabelGenerator

use of org.jfree.chart.labels.XYItemLabelGenerator in project SIMVA-SoS by SESoS.

the class AbstractXYItemRenderer method drawItemLabel.

/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param x  the x coordinate (in Java2D space).
 * @param y  the y coordinate (in Java2D space).
 * @param negative  indicates a negative value (which affects the item
 *                  label position).
 */
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation, XYDataset dataset, int series, int item, double x, double y, boolean negative) {
    XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
    if (generator != null) {
        Font labelFont = getItemLabelFont(series, item);
        Paint paint = getItemLabelPaint(series, item);
        g2.setFont(labelFont);
        g2.setPaint(paint);
        String label = generator.generateLabel(dataset, series, item);
        // get the label position..
        ItemLabelPosition position;
        if (!negative) {
            position = getPositiveItemLabelPosition(series, item);
        } else {
            position = getNegativeItemLabelPosition(series, item);
        }
        // work out the label anchor point...
        Point2D anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), x, y, orientation);
        TextUtilities.drawRotatedString(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(), position.getRotationAnchor());
    }
}
Also used : Point2D(java.awt.geom.Point2D) XYItemLabelGenerator(org.jfree.chart.labels.XYItemLabelGenerator) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font)

Example 2 with XYItemLabelGenerator

use of org.jfree.chart.labels.XYItemLabelGenerator 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 XYItemLabelGenerator

use of org.jfree.chart.labels.XYItemLabelGenerator in project SIMVA-SoS by SESoS.

the class XYBarRenderer 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;
    }
    IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
    double value0;
    double value1;
    if (this.useYInterval) {
        value0 = intervalDataset.getStartYValue(series, item);
        value1 = intervalDataset.getEndYValue(series, item);
    } else {
        value0 = this.base;
        value1 = intervalDataset.getYValue(series, item);
    }
    if (Double.isNaN(value0) || Double.isNaN(value1)) {
        return;
    }
    if (value0 <= value1) {
        if (!rangeAxis.getRange().intersects(value0, value1)) {
            return;
        }
    } else {
        if (!rangeAxis.getRange().intersects(value1, value0)) {
            return;
        }
    }
    double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, plot.getRangeAxisEdge());
    double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, plot.getRangeAxisEdge());
    double bottom = Math.min(translatedValue0, translatedValue1);
    double top = Math.max(translatedValue0, translatedValue1);
    double startX = intervalDataset.getStartXValue(series, item);
    if (Double.isNaN(startX)) {
        return;
    }
    double endX = intervalDataset.getEndXValue(series, item);
    if (Double.isNaN(endX)) {
        return;
    }
    if (startX <= endX) {
        if (!domainAxis.getRange().intersects(startX, endX)) {
            return;
        }
    } else {
        if (!domainAxis.getRange().intersects(endX, startX)) {
            return;
        }
    }
    // is there an alignment adjustment to be made?
    if (this.barAlignmentFactor >= 0.0 && this.barAlignmentFactor <= 1.0) {
        double x = intervalDataset.getXValue(series, item);
        double interval = endX - startX;
        startX = x - interval * this.barAlignmentFactor;
        endX = startX + interval;
    }
    RectangleEdge location = plot.getDomainAxisEdge();
    double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, location);
    double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, location);
    double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
    double left = Math.min(translatedStartX, translatedEndX);
    if (getMargin() > 0.0) {
        double cut = translatedWidth * getMargin();
        translatedWidth = translatedWidth - cut;
        left = left + cut / 2;
    }
    Rectangle2D bar = null;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        // clip left and right bounds to data area
        bottom = Math.max(bottom, dataArea.getMinX());
        top = Math.min(top, dataArea.getMaxX());
        bar = new Rectangle2D.Double(bottom, left, top - bottom, translatedWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        // clip top and bottom bounds to data area
        bottom = Math.max(bottom, dataArea.getMinY());
        top = Math.min(top, dataArea.getMaxY());
        bar = new Rectangle2D.Double(left, bottom, translatedWidth, top - bottom);
    }
    boolean positive = (value1 > 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 (getShadowsVisible()) {
        this.barPainter.paintBarShadow(g2, this, series, item, bar, barBase, !this.useYInterval);
    }
    this.barPainter.paintBar(g2, this, series, item, bar, barBase);
    if (isItemLabelVisible(series, item)) {
        XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
        drawItemLabel(g2, dataset, series, item, plot, generator, bar, value1 < 0.0);
    }
    // update the crosshair point
    double x1 = (startX + endX) / 2.0;
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1, plot.getOrientation());
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addEntity(entities, bar, dataset, series, item, 0.0, 0.0);
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) EntityCollection(org.jfree.chart.entity.EntityCollection) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) Rectangle2D(java.awt.geom.Rectangle2D) XYItemLabelGenerator(org.jfree.chart.labels.XYItemLabelGenerator) Paint(java.awt.Paint) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 4 with XYItemLabelGenerator

use of org.jfree.chart.labels.XYItemLabelGenerator in project mzmine2 by mzmine.

the class TICPlot method addLabelledPeakDataset.

public synchronized void addLabelledPeakDataset(final XYDataset dataSet, final String label) {
    // Add standard peak data set.
    addPeakDataset(dataSet);
    // Do we have a label?
    if (label != null && label.length() > 0) {
        // Add peak label renderer and data set.
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(false, false);
        renderer.setDefaultItemLabelsVisible(labelsVisible == 2);
        renderer.setDefaultItemLabelPaint(LABEL_COLOR);
        addDataSetRenderer(dataSet, renderer);
        renderer.setDrawSeriesLineAsPath(true);
        renderer.setDefaultItemLabelGenerator(new XYItemLabelGenerator() {

            @Override
            public String generateLabel(final XYDataset xyDataSet, final int series, final int item) {
                return ((PeakDataSet) xyDataSet).isPeak(item) ? label : null;
            }
        });
        havePeakLabels = true;
    }
}
Also used : XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) XYItemLabelGenerator(org.jfree.chart.labels.XYItemLabelGenerator) XYDataset(org.jfree.data.xy.XYDataset) Paint(java.awt.Paint)

Example 5 with XYItemLabelGenerator

use of org.jfree.chart.labels.XYItemLabelGenerator in project SIMVA-SoS by SESoS.

the class ClusteredXYBarRenderer method drawItem.

/**
 * Draws the visual representation of a single data item. This method
 * is mostly copied from the superclass, the change is that in the
 * calculated space for a singe bar we draw bars for each series next to
 * each other. The width of each bar is the available width divided by
 * the number of series. Bars for each series are drawn in order left to
 * right.
 *
 * @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.
 * @param item  the item index.
 * @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) {
    IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
    double y0;
    double y1;
    if (getUseYInterval()) {
        y0 = intervalDataset.getStartYValue(series, item);
        y1 = intervalDataset.getEndYValue(series, item);
    } else {
        y0 = getBase();
        y1 = intervalDataset.getYValue(series, item);
    }
    if (Double.isNaN(y0) || Double.isNaN(y1)) {
        return;
    }
    double yy0 = rangeAxis.valueToJava2D(y0, dataArea, plot.getRangeAxisEdge());
    double yy1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    double x0 = intervalDataset.getStartXValue(series, item);
    double xx0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
    double x1 = intervalDataset.getEndXValue(series, item);
    double xx1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    // this may be negative
    double intervalW = xx1 - xx0;
    double baseX = xx0;
    if (this.centerBarAtStartValue) {
        baseX = baseX - intervalW / 2.0;
    }
    double m = getMargin();
    if (m > 0.0) {
        double cut = intervalW * getMargin();
        intervalW = intervalW - cut;
        baseX = baseX + (cut / 2);
    }
    // we don't need the sign
    double intervalH = Math.abs(yy0 - yy1);
    PlotOrientation orientation = plot.getOrientation();
    int numSeries = dataset.getSeriesCount();
    // may be negative
    double seriesBarWidth = intervalW / numSeries;
    Rectangle2D bar = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        double barY0 = baseX + (seriesBarWidth * series);
        double barY1 = barY0 + seriesBarWidth;
        double rx = Math.min(yy0, yy1);
        double rw = intervalH;
        double ry = Math.min(barY0, barY1);
        double rh = Math.abs(barY1 - barY0);
        bar = new Rectangle2D.Double(rx, ry, rw, rh);
    } else if (orientation == PlotOrientation.VERTICAL) {
        double barX0 = baseX + (seriesBarWidth * series);
        double barX1 = barX0 + seriesBarWidth;
        double rx = Math.min(barX0, barX1);
        double rw = Math.abs(barX1 - barX0);
        double ry = Math.min(yy0, yy1);
        double rh = intervalH;
        bar = new Rectangle2D.Double(rx, ry, rw, rh);
    } else {
        throw new IllegalStateException();
    }
    boolean positive = (y1 > 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 && getShadowsVisible()) {
        getBarPainter().paintBarShadow(g2, this, series, item, bar, barBase, !getUseYInterval());
    }
    if (pass == 1) {
        getBarPainter().paintBar(g2, this, series, item, bar, barBase);
        if (isItemLabelVisible(series, item)) {
            XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
            drawItemLabel(g2, dataset, series, item, plot, generator, bar, y1 < 0.0);
        }
        // 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());
            }
        }
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) EntityCollection(org.jfree.chart.entity.EntityCollection) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) Rectangle2D(java.awt.geom.Rectangle2D) XYItemLabelGenerator(org.jfree.chart.labels.XYItemLabelGenerator) RectangleEdge(org.jfree.ui.RectangleEdge)

Aggregations

XYItemLabelGenerator (org.jfree.chart.labels.XYItemLabelGenerator)6 Paint (java.awt.Paint)3 Rectangle2D (java.awt.geom.Rectangle2D)3 EntityCollection (org.jfree.chart.entity.EntityCollection)3 PlotOrientation (org.jfree.chart.plot.PlotOrientation)3 IntervalXYDataset (org.jfree.data.xy.IntervalXYDataset)3 RectangleEdge (org.jfree.ui.RectangleEdge)3 Font (java.awt.Font)2 Point2D (java.awt.geom.Point2D)2 ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)2 FontMetrics (java.awt.FontMetrics)1 GradientPaint (java.awt.GradientPaint)1 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)1 TableXYDataset (org.jfree.data.xy.TableXYDataset)1 XYDataset (org.jfree.data.xy.XYDataset)1