Search in sources :

Example 6 with CategoryItemLabelGenerator

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

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

the class LevelRenderer method drawItem.

/**
 * Draws the bar for a single (series, category) data item.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the data area.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @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 if the row index is not included in the list with
    // the indices of the visible rows...
    int visibleRow = state.getVisibleSeriesIndex(row);
    if (visibleRow < 0) {
        return;
    }
    // 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 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, visibleRow, column);
    RectangleEdge edge = plot.getRangeAxisEdge();
    double barL = rangeAxis.valueToJava2D(value, dataArea, edge);
    // draw the bar...
    Line2D line;
    double x, y;
    if (orientation == PlotOrientation.HORIZONTAL) {
        x = barL;
        y = barW0 + state.getBarWidth() / 2.0;
        line = new Line2D.Double(barL, barW0, barL, barW0 + state.getBarWidth());
    } else {
        x = barW0 + state.getBarWidth() / 2.0;
        y = barL;
        line = new Line2D.Double(barW0, barL, barW0 + state.getBarWidth(), barL);
    }
    Stroke itemStroke = getItemStroke(row, column);
    Paint itemPaint = getItemPaint(row, column);
    g2.setStroke(itemStroke);
    g2.setPaint(itemPaint);
    g2.draw(line);
    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null && isItemLabelVisible(row, column)) {
        drawItemLabel(g2, orientation, dataset, row, column, x, y, (value < 0.0));
    }
    // submit the current data point as a crosshair candidate
    int datasetIndex = plot.indexOf(dataset);
    updateCrosshairValues(state.getCrosshairState(), dataset.getRowKey(row), dataset.getColumnKey(column), value, datasetIndex, barW0, barL, orientation);
    // collect entity and tool tip information...
    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addItemEntity(entities, dataset, row, column, line.getBounds());
    }
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) Stroke(java.awt.Stroke) EntityCollection(org.jfree.chart.entity.EntityCollection) CategoryItemLabelGenerator(org.jfree.chart.labels.CategoryItemLabelGenerator) Paint(java.awt.Paint) Line2D(java.awt.geom.Line2D) Paint(java.awt.Paint) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 8 with CategoryItemLabelGenerator

use of org.jfree.chart.labels.CategoryItemLabelGenerator in project opennms by OpenNMS.

the class ChartUtils method customizeSeries.

/**
 * @param barChart TODO
 * @param chartConfig
 */
private static void customizeSeries(JFreeChart barChart, BarChart chartConfig) {
    /*
         * Set the series colors and labels
         */
    CategoryItemLabelGenerator itemLabelGenerator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0"));
    SeriesDef[] seriesDefs = chartConfig.getSeriesDef();
    CustomSeriesColors seriesColors = null;
    if (chartConfig.getSeriesColorClass().isPresent()) {
        try {
            seriesColors = (CustomSeriesColors) Class.forName(chartConfig.getSeriesColorClass().get()).newInstance();
        } catch (InstantiationException e) {
            LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
        } catch (IllegalAccessException e) {
            LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
        } catch (ClassNotFoundException e) {
            LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
        }
    }
    for (int i = 0; i < seriesDefs.length; i++) {
        SeriesDef seriesDef = seriesDefs[i];
        Paint paint = Color.BLACK;
        if (seriesColors != null) {
            Comparable<?> cat = (Comparable<?>) ((BarRenderer) barChart.getCategoryPlot().getRenderer()).getPlot().getCategories().get(i);
            paint = seriesColors.getPaint(cat);
        } else if (seriesDef.getRgb().isPresent()) {
            final Rgb rgb = seriesDef.getRgb().get();
            paint = new Color(rgb.getRed().getRgbColor(), rgb.getGreen().getRgbColor(), rgb.getBlue().getRgbColor());
        }
        ((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesPaint(i, paint);
        ((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesItemLabelsVisible(i, seriesDef.getUseLabels());
        ((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesItemLabelGenerator(i, itemLabelGenerator);
    }
}
Also used : StandardCategoryItemLabelGenerator(org.jfree.chart.labels.StandardCategoryItemLabelGenerator) DecimalFormat(java.text.DecimalFormat) Color(java.awt.Color) ChartBackgroundColor(org.opennms.netmgt.config.charts.ChartBackgroundColor) PlotBackgroundColor(org.opennms.netmgt.config.charts.PlotBackgroundColor) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StandardCategoryItemLabelGenerator(org.jfree.chart.labels.StandardCategoryItemLabelGenerator) CategoryItemLabelGenerator(org.jfree.chart.labels.CategoryItemLabelGenerator) Paint(java.awt.Paint) Rgb(org.opennms.netmgt.config.charts.Rgb) Paint(java.awt.Paint) SeriesDef(org.opennms.netmgt.config.charts.SeriesDef)

Example 9 with CategoryItemLabelGenerator

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

the class AbstractCategoryItemRenderer method drawItemLabel.

/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @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, CategoryDataset dataset, int row, int column, double x, double y, boolean negative) {
    CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
    if (generator != null) {
        Font labelFont = getItemLabelFont(row, column);
        Paint paint = getItemLabelPaint(row, column);
        g2.setFont(labelFont);
        g2.setPaint(paint);
        String label = generator.generateLabel(dataset, row, column);
        ItemLabelPosition position;
        if (!negative) {
            position = getPositiveItemLabelPosition(row, column);
        } else {
            position = getNegativeItemLabelPosition(row, column);
        }
        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) CategoryItemLabelGenerator(org.jfree.chart.labels.CategoryItemLabelGenerator) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font)

Example 10 with CategoryItemLabelGenerator

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

the class BarRenderer method drawItem.

/**
 * Draws the bar for a single (series, category) data item.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the data area.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @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 if the row index is not included in the list with
    // the indices of the visible rows...
    int visibleRow = state.getVisibleSeriesIndex(row);
    if (visibleRow < 0) {
        return;
    }
    // nothing is drawn for null values...
    Number dataValue = dataset.getValue(row, column);
    if (dataValue == null) {
        return;
    }
    final double value = dataValue.doubleValue();
    PlotOrientation orientation = plot.getOrientation();
    double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, visibleRow, column);
    double[] barL0L1 = calculateBarL0L1(value);
    if (barL0L1 == null) {
        // the bar is not visible
        return;
    }
    RectangleEdge edge = plot.getRangeAxisEdge();
    double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
    double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);
    // in the following code, barL0 is (in Java2D coordinates) the LEFT
    // end of the bar for a horizontal bar chart, and the TOP end of the
    // bar for a vertical bar chart.  Whether this is the BASE of the bar
    // or not depends also on (a) whether the data value is 'negative'
    // relative to the base value and (b) whether or not the range axis is
    // inverted.  This only matters if/when we apply the minimumBarLength
    // attribute, because we should extend the non-base end of the bar
    boolean positive = (value >= this.base);
    boolean inverted = rangeAxis.isInverted();
    double barL0 = Math.min(transL0, transL1);
    double barLength = Math.abs(transL1 - transL0);
    double barLengthAdj = 0.0;
    if (barLength > 0.0 && barLength < getMinimumBarLength()) {
        barLengthAdj = getMinimumBarLength() - barLength;
    }
    double barL0Adj = 0.0;
    RectangleEdge barBase;
    if (orientation == PlotOrientation.HORIZONTAL) {
        if (positive && inverted || !positive && !inverted) {
            barL0Adj = barLengthAdj;
            barBase = RectangleEdge.RIGHT;
        } else {
            barBase = RectangleEdge.LEFT;
        }
    } else {
        if (positive && !inverted || !positive && inverted) {
            barL0Adj = barLengthAdj;
            barBase = RectangleEdge.BOTTOM;
        } else {
            barBase = RectangleEdge.TOP;
        }
    }
    // draw the bar...
    Rectangle2D bar;
    if (orientation == PlotOrientation.HORIZONTAL) {
        bar = new Rectangle2D.Double(barL0 - barL0Adj, barW0, barLength + barLengthAdj, state.getBarWidth());
    } else {
        bar = new Rectangle2D.Double(barW0, barL0 - barL0Adj, state.getBarWidth(), barLength + barLengthAdj);
    }
    if (getShadowsVisible()) {
        this.barPainter.paintBarShadow(g2, this, row, column, bar, barBase, true);
    }
    this.barPainter.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));
    }
    // submit the current data point as a crosshair candidate
    int datasetIndex = plot.indexOf(dataset);
    updateCrosshairValues(state.getCrosshairState(), dataset.getRowKey(row), dataset.getColumnKey(column), value, datasetIndex, barW0, barL0, orientation);
    // add an item entity, if this information is being collected
    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) Paint(java.awt.Paint) RectangleEdge(org.jfree.ui.RectangleEdge)

Aggregations

CategoryItemLabelGenerator (org.jfree.chart.labels.CategoryItemLabelGenerator)17 EntityCollection (org.jfree.chart.entity.EntityCollection)15 Paint (java.awt.Paint)14 Rectangle2D (java.awt.geom.Rectangle2D)14 RectangleEdge (org.jfree.ui.RectangleEdge)13 PlotOrientation (org.jfree.chart.plot.PlotOrientation)9 Stroke (java.awt.Stroke)8 GradientPaint (java.awt.GradientPaint)6 GradientPaintTransformer (org.jfree.ui.GradientPaintTransformer)4 Line2D (java.awt.geom.Line2D)3 BasicStroke (java.awt.BasicStroke)2 Shape (java.awt.Shape)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Color (java.awt.Color)1 Font (java.awt.Font)1 GeneralPath (java.awt.geom.GeneralPath)1 Point2D (java.awt.geom.Point2D)1 DecimalFormat (java.text.DecimalFormat)1 CategoryItemEntity (org.jfree.chart.entity.CategoryItemEntity)1