Search in sources :

Example 21 with ItemLabelPosition

use of org.jfree.chart.labels.ItemLabelPosition in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class BarRenderer method drawItemLabel.

/**
 * Draws an item label.  This method is overridden so that the bar can be
 * used to calculate the label anchor point.
 *
 * @param g2  the graphics device.
 * @param data  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @param plot  the plot.
 * @param generator  the label generator.
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 */
protected void drawItemLabel(Graphics2D g2, CategoryDataset data, int row, int column, CategoryPlot plot, CategoryItemLabelGenerator generator, Rectangle2D bar, boolean negative) {
    String label = generator.generateLabel(data, row, column);
    if (label == null) {
        // nothing to do
        return;
    }
    Font labelFont = getItemLabelFont(row, column);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(row, column);
    g2.setPaint(paint);
    // find out where to place the label...
    ItemLabelPosition position;
    if (!negative) {
        position = getPositiveItemLabelPosition(row, column);
    } else {
        position = getNegativeItemLabelPosition(row, column);
    }
    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar, plot.getOrientation());
    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtils.calculateRotatedStringBounds(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(), position.getRotationAnchor());
        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                } else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar, plot.getOrientation());
                }
            }
        }
    }
    if (position != null) {
        TextUtils.drawRotatedString(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(), position.getRotationAnchor());
    }
}
Also used : Shape(java.awt.Shape) Point2D(java.awt.geom.Point2D) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) Paint(java.awt.Paint) Font(java.awt.Font)

Example 22 with ItemLabelPosition

use of org.jfree.chart.labels.ItemLabelPosition in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

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);
        TextUtils.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 23 with ItemLabelPosition

use of org.jfree.chart.labels.ItemLabelPosition in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class ChartFactory method createWaterfallChart.

/**
 * Creates a waterfall chart.  The chart object returned by this method
 * uses a {@link CategoryPlot} instance as the plot, with a
 * {@link CategoryAxis} for the domain axis, a {@link NumberAxis} as the
 * range axis, and a {@link WaterfallBarRenderer} as the renderer.
 *
 * @param title  the chart title ({@code null} permitted).
 * @param categoryAxisLabel  the label for the category axis
 *                           ({@code null} permitted).
 * @param valueAxisLabel  the label for the value axis ({@code null}
 *                        permitted).
 * @param dataset  the dataset for the chart ({@code null} permitted).
 * @param orientation  the plot orientation (horizontal or vertical)
 *                     ({@code null} NOT permitted).
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param urls  configure chart to generate URLs?
 *
 * @return A waterfall chart.
 */
public static JFreeChart createWaterfallChart(String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) {
    Args.nullNotPermitted(orientation, "orientation");
    CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
    categoryAxis.setCategoryMargin(0.0);
    ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
    WaterfallBarRenderer renderer = new WaterfallBarRenderer();
    if (orientation == PlotOrientation.HORIZONTAL) {
        ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, Math.PI / 2.0);
        renderer.setDefaultPositiveItemLabelPosition(position);
        renderer.setDefaultNegativeItemLabelPosition(position);
    } else if (orientation == PlotOrientation.VERTICAL) {
        ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0);
        renderer.setDefaultPositiveItemLabelPosition(position);
        renderer.setDefaultNegativeItemLabelPosition(position);
    }
    if (tooltips) {
        StandardCategoryToolTipGenerator generator = new StandardCategoryToolTipGenerator();
        renderer.setDefaultToolTipGenerator(generator);
    }
    if (urls) {
        renderer.setDefaultItemURLGenerator(new StandardCategoryURLGenerator());
    }
    CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
    plot.clearRangeMarkers();
    Marker baseline = new ValueMarker(0.0);
    baseline.setPaint(Color.BLACK);
    plot.addRangeMarker(baseline, Layer.FOREGROUND);
    plot.setOrientation(orientation);
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    currentTheme.apply(chart);
    return chart;
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) WaterfallBarRenderer(org.jfree.chart.renderer.category.WaterfallBarRenderer) CategoryAxis(org.jfree.chart.axis.CategoryAxis) StandardCategoryToolTipGenerator(org.jfree.chart.labels.StandardCategoryToolTipGenerator) ValueAxis(org.jfree.chart.axis.ValueAxis) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) StandardCategoryURLGenerator(org.jfree.chart.urls.StandardCategoryURLGenerator) ValueMarker(org.jfree.chart.plot.ValueMarker) Marker(org.jfree.chart.plot.Marker) ValueMarker(org.jfree.chart.plot.ValueMarker) CategoryPlot(org.jfree.chart.plot.CategoryPlot)

Example 24 with ItemLabelPosition

use of org.jfree.chart.labels.ItemLabelPosition in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class ChartFactory method createBarChart.

/**
 * Creates a bar chart.  The chart object returned by this method uses a
 * {@link CategoryPlot} instance as the plot, with a {@link CategoryAxis}
 * for the domain axis, a {@link NumberAxis} as the range axis, and a
 * {@link BarRenderer} as the renderer.
 *
 * @param title  the chart title ({@code null} permitted).
 * @param categoryAxisLabel  the label for the category axis
 *                           ({@code null} permitted).
 * @param valueAxisLabel  the label for the value axis
 *                        ({@code null} permitted).
 * @param dataset  the dataset for the chart ({@code null} permitted).
 * @param orientation  the plot orientation (horizontal or vertical)
 *                     ({@code null} not permitted).
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param urls  configure chart to generate URLs?
 *
 * @return A bar chart.
 */
public static JFreeChart createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) {
    Args.nullNotPermitted(orientation, "orientation");
    CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
    ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
    BarRenderer renderer = new BarRenderer();
    if (orientation == PlotOrientation.HORIZONTAL) {
        ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT);
        renderer.setDefaultPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT);
        renderer.setDefaultNegativeItemLabelPosition(position2);
    } else if (orientation == PlotOrientation.VERTICAL) {
        ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
        renderer.setDefaultPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
        renderer.setDefaultNegativeItemLabelPosition(position2);
    }
    if (tooltips) {
        renderer.setDefaultToolTipGenerator(new StandardCategoryToolTipGenerator());
    }
    if (urls) {
        renderer.setDefaultItemURLGenerator(new StandardCategoryURLGenerator());
    }
    CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
    plot.setOrientation(orientation);
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    currentTheme.apply(chart);
    return chart;
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) CategoryAxis(org.jfree.chart.axis.CategoryAxis) StandardCategoryToolTipGenerator(org.jfree.chart.labels.StandardCategoryToolTipGenerator) ValueAxis(org.jfree.chart.axis.ValueAxis) WaterfallBarRenderer(org.jfree.chart.renderer.category.WaterfallBarRenderer) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) XYBarRenderer(org.jfree.chart.renderer.xy.XYBarRenderer) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) StandardCategoryURLGenerator(org.jfree.chart.urls.StandardCategoryURLGenerator) CategoryPlot(org.jfree.chart.plot.CategoryPlot)

Example 25 with ItemLabelPosition

use of org.jfree.chart.labels.ItemLabelPosition in project seng438-a3-R41Ryan by seng438-winter-2022.

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)

Aggregations

ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)72 Font (java.awt.Font)30 Paint (java.awt.Paint)26 Point2D (java.awt.geom.Point2D)22 BarRenderer (org.jfree.chart.renderer.category.BarRenderer)21 NumberAxis (org.jfree.chart.axis.NumberAxis)20 CategoryPlot (org.jfree.chart.plot.CategoryPlot)18 CategoryAxis (org.jfree.chart.axis.CategoryAxis)15 JFreeChart (org.jfree.chart.JFreeChart)13 GradientPaint (java.awt.GradientPaint)11 ValueAxis (org.jfree.chart.axis.ValueAxis)11 StandardCategoryToolTipGenerator (org.jfree.chart.labels.StandardCategoryToolTipGenerator)11 StackedBarRenderer (org.jfree.chart.renderer.category.StackedBarRenderer)11 Color (java.awt.Color)10 Test (org.junit.Test)10 Shape (java.awt.Shape)8 WaterfallBarRenderer (org.jfree.chart.renderer.category.WaterfallBarRenderer)8 StandardCategoryURLGenerator (org.jfree.chart.urls.StandardCategoryURLGenerator)8 Test (org.junit.jupiter.api.Test)8 BasicStroke (java.awt.BasicStroke)7