Search in sources :

Example 11 with LegendItem

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

the class XYLineAndShapeRenderer method getLegendItem.

/**
 * Returns a legend item for the specified series.
 *
 * @param datasetIndex  the dataset index (zero-based).
 * @param series  the series index (zero-based).
 *
 * @return A legend item for the series (possibly {@code null}).
 */
@Override
public LegendItem getLegendItem(int datasetIndex, int series) {
    XYPlot plot = getPlot();
    if (plot == null) {
        return null;
    }
    XYDataset dataset = plot.getDataset(datasetIndex);
    if (dataset == null) {
        return null;
    }
    if (!getItemVisible(series, 0)) {
        return null;
    }
    String label = getLegendItemLabelGenerator().generateLabel(dataset, series);
    String description = label;
    String toolTipText = null;
    if (getLegendItemToolTipGenerator() != null) {
        toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series);
    }
    String urlText = null;
    if (getLegendItemURLGenerator() != null) {
        urlText = getLegendItemURLGenerator().generateLabel(dataset, series);
    }
    boolean shapeIsVisible = getItemShapeVisible(series, 0);
    Shape shape = lookupLegendShape(series);
    boolean shapeIsFilled = getItemShapeFilled(series, 0);
    Paint fillPaint = (this.useFillPaint ? lookupSeriesFillPaint(series) : lookupSeriesPaint(series));
    boolean shapeOutlineVisible = this.drawOutlines;
    Paint outlinePaint = (this.useOutlinePaint ? lookupSeriesOutlinePaint(series) : lookupSeriesPaint(series));
    Stroke outlineStroke = lookupSeriesOutlineStroke(series);
    boolean lineVisible = getItemLineVisible(series, 0);
    Stroke lineStroke = lookupSeriesStroke(series);
    Paint linePaint = lookupSeriesPaint(series);
    LegendItem result = new LegendItem(label, description, toolTipText, urlText, shapeIsVisible, shape, shapeIsFilled, fillPaint, shapeOutlineVisible, outlinePaint, outlineStroke, lineVisible, this.legendLine, lineStroke, linePaint);
    result.setLabelFont(lookupLegendTextFont(series));
    Paint labelPaint = lookupLegendTextPaint(series);
    if (labelPaint != null) {
        result.setLabelPaint(labelPaint);
    }
    result.setSeriesKey(dataset.getSeriesKey(series));
    result.setSeriesIndex(series);
    result.setDataset(dataset);
    result.setDatasetIndex(datasetIndex);
    return result;
}
Also used : Stroke(java.awt.Stroke) Shape(java.awt.Shape) XYPlot(org.jfree.chart.plot.XYPlot) LegendItem(org.jfree.chart.legend.LegendItem) XYDataset(org.jfree.data.xy.XYDataset) Paint(java.awt.Paint)

Example 12 with LegendItem

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

the class XYPlot method getLegendItems.

/**
 * Returns the legend items for the plot.  Each legend item is generated by
 * the plot's renderer, since the renderer is responsible for the visual
 * representation of the data.
 *
 * @return The legend items.
 */
@Override
public LegendItemCollection getLegendItems() {
    if (this.fixedLegendItems != null) {
        return this.fixedLegendItems;
    }
    LegendItemCollection result = new LegendItemCollection();
    for (XYDataset<S> dataset : this.datasets.values()) {
        if (dataset == null) {
            continue;
        }
        int datasetIndex = indexOf(dataset);
        XYItemRenderer renderer = getRenderer(datasetIndex);
        if (renderer == null) {
            renderer = getRenderer(0);
        }
        if (renderer != null) {
            int seriesCount = dataset.getSeriesCount();
            for (int i = 0; i < seriesCount; i++) {
                if (renderer.isSeriesVisible(i) && renderer.isSeriesVisibleInLegend(i)) {
                    LegendItem item = renderer.getLegendItem(datasetIndex, i);
                    if (item != null) {
                        result.add(item);
                    }
                }
            }
        }
    }
    return result;
}
Also used : LegendItem(org.jfree.chart.legend.LegendItem) LegendItemCollection(org.jfree.chart.legend.LegendItemCollection) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer)

Example 13 with LegendItem

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

the class PolarPlot method getLegendItems.

/**
 * Returns the legend items for the plot.  Each legend item is generated by
 * the plot's m_Renderer, since the m_Renderer is responsible for the visual
 * representation of the data.
 *
 * @return The legend items.
 */
@Override
public LegendItemCollection getLegendItems() {
    if (this.fixedLegendItems != null) {
        return this.fixedLegendItems;
    }
    LegendItemCollection result = new LegendItemCollection();
    int count = this.datasets.size();
    for (int datasetIndex = 0; datasetIndex < count; datasetIndex++) {
        XYDataset dataset = getDataset(datasetIndex);
        PolarItemRenderer renderer = getRenderer(datasetIndex);
        if (dataset != null && renderer != null) {
            int seriesCount = dataset.getSeriesCount();
            for (int i = 0; i < seriesCount; i++) {
                LegendItem item = renderer.getLegendItem(i);
                result.add(item);
            }
        }
    }
    return result;
}
Also used : LegendItem(org.jfree.chart.legend.LegendItem) LegendItemCollection(org.jfree.chart.legend.LegendItemCollection) XYDataset(org.jfree.data.xy.XYDataset) PolarItemRenderer(org.jfree.chart.renderer.PolarItemRenderer) Point(java.awt.Point) Paint(java.awt.Paint)

Example 14 with LegendItem

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

the class DefaultPolarItemRenderer method getLegendItem.

/**
 * Return the legend for the given series.
 *
 * @param series  the series index.
 *
 * @return The legend item.
 */
@Override
public LegendItem getLegendItem(int series) {
    LegendItem result;
    PolarPlot plot = getPlot();
    if (plot == null) {
        return null;
    }
    XYDataset dataset = plot.getDataset(plot.getIndexOf(this));
    if (dataset == null) {
        return null;
    }
    String toolTipText = null;
    if (getLegendItemToolTipGenerator() != null) {
        toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series);
    }
    String urlText = null;
    if (getLegendItemURLGenerator() != null) {
        urlText = getLegendItemURLGenerator().generateLabel(dataset, series);
    }
    Comparable seriesKey = dataset.getSeriesKey(series);
    String label = seriesKey.toString();
    String description = label;
    Shape shape = lookupSeriesShape(series);
    Paint paint;
    if (this.useFillPaint) {
        paint = lookupSeriesFillPaint(series);
    } else {
        paint = lookupSeriesPaint(series);
    }
    Stroke stroke = lookupSeriesStroke(series);
    Paint outlinePaint = lookupSeriesOutlinePaint(series);
    Stroke outlineStroke = lookupSeriesOutlineStroke(series);
    boolean shapeOutlined = isSeriesFilled(series) && this.drawOutlineWhenFilled;
    result = new LegendItem(label, description, toolTipText, urlText, getShapesVisible(), shape, /* shapeFilled=*/
    true, paint, shapeOutlined, outlinePaint, outlineStroke, /* lineVisible= */
    true, this.legendLine, stroke, paint);
    result.setToolTipText(toolTipText);
    result.setURLText(urlText);
    result.setDataset(dataset);
    result.setSeriesKey(seriesKey);
    result.setSeriesIndex(series);
    return result;
}
Also used : Stroke(java.awt.Stroke) Shape(java.awt.Shape) LegendItem(org.jfree.chart.legend.LegendItem) PolarPlot(org.jfree.chart.plot.PolarPlot) XYDataset(org.jfree.data.xy.XYDataset) Paint(java.awt.Paint)

Example 15 with LegendItem

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

the class BarRendererTest method testGetLegendItemSeriesIndex.

/**
 * A check for the datasetIndex and seriesIndex fields in the LegendItem
 * returned by the getLegendItem() method.
 */
@Test
public void testGetLegendItemSeriesIndex() {
    DefaultCategoryDataset<String, String> dataset0 = new DefaultCategoryDataset<>();
    dataset0.addValue(21.0, "R1", "C1");
    dataset0.addValue(22.0, "R2", "C1");
    DefaultCategoryDataset<String, String> dataset1 = new DefaultCategoryDataset<>();
    dataset1.addValue(23.0, "R3", "C1");
    dataset1.addValue(24.0, "R4", "C1");
    dataset1.addValue(25.0, "R5", "C1");
    BarRenderer r = new BarRenderer();
    CategoryPlot<String, String> plot = new CategoryPlot<>(dataset0, new CategoryAxis("x"), new NumberAxis("y"), r);
    plot.setDataset(1, dataset1);
    /*JFreeChart chart =*/
    new JFreeChart(plot);
    LegendItem li = r.getLegendItem(1, 2);
    assertEquals("R5", li.getLabel());
    assertEquals(1, li.getDatasetIndex());
    assertEquals(2, li.getSeriesIndex());
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) CategoryAxis(org.jfree.chart.axis.CategoryAxis) LegendItem(org.jfree.chart.legend.LegendItem) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) CategoryPlot(org.jfree.chart.plot.CategoryPlot) JFreeChart(org.jfree.chart.JFreeChart) Test(org.junit.jupiter.api.Test)

Aggregations

LegendItem (org.jfree.chart.legend.LegendItem)58 Test (org.junit.jupiter.api.Test)32 Paint (java.awt.Paint)23 NumberAxis (org.jfree.chart.axis.NumberAxis)20 LegendItemCollection (org.jfree.chart.legend.LegendItemCollection)20 JFreeChart (org.jfree.chart.JFreeChart)19 XYPlot (org.jfree.chart.plot.XYPlot)18 Stroke (java.awt.Stroke)16 Shape (java.awt.Shape)15 CategoryPlot (org.jfree.chart.plot.CategoryPlot)15 XYDataset (org.jfree.data.xy.XYDataset)12 GradientPaint (java.awt.GradientPaint)9 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)9 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)9 CategoryAxis (org.jfree.chart.axis.CategoryAxis)8 CategoryDataset (org.jfree.data.category.CategoryDataset)8 XYSeries (org.jfree.data.xy.XYSeries)7 BasicStroke (java.awt.BasicStroke)6 Rectangle2D (java.awt.geom.Rectangle2D)6 Line2D (java.awt.geom.Line2D)5