Search in sources :

Example 6 with LegendItem

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

the class CategoryPlotTest method testCloning3.

/**
 * Some more cloning checks.
 */
@Test
public void testCloning3() {
    LegendItemCollection c1 = new LegendItemCollection();
    CategoryPlot<String, String> p1 = new CategoryPlot<>();
    p1.setFixedLegendItems(c1);
    CategoryPlot<String, String> p2;
    try {
        p2 = CloneUtils.clone(p1);
    } catch (CloneNotSupportedException e) {
        fail("Cloning failed.");
        return;
    }
    assertNotSame(p1, p2);
    assertSame(p1.getClass(), p2.getClass());
    assertEquals(p1, p2);
    c1.add(new LegendItem("X", "XX", "tt", "url", true, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true, Color.RED, true, Color.YELLOW, new BasicStroke(1.0f), true, new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(1.0f), Color.GREEN));
    assertNotEquals(p1, p2);
    p2.getFixedLegendItems().add(new LegendItem("X", "XX", "tt", "url", true, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true, Color.RED, true, Color.YELLOW, new BasicStroke(1.0f), true, new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(1.0f), Color.GREEN));
    assertEquals(p1, p2);
}
Also used : BasicStroke(java.awt.BasicStroke) LegendItem(org.jfree.chart.legend.LegendItem) LegendItemCollection(org.jfree.chart.legend.LegendItemCollection) Test(org.junit.jupiter.api.Test)

Example 7 with LegendItem

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

the class PolarPlotTest method testGetLegendItems2.

/**
 * Some checks for the getLegendItems() method with multiple datasets.
 */
@Test
public void testGetLegendItems2() {
    XYSeriesCollection<String> d1 = new XYSeriesCollection<>();
    d1.addSeries(new XYSeries<>("A"));
    d1.addSeries(new XYSeries<>("B"));
    XYSeriesCollection<String> d2 = new XYSeriesCollection<>();
    d2.addSeries(new XYSeries<>("C"));
    d2.addSeries(new XYSeries<>("D"));
    DefaultPolarItemRenderer r = new DefaultPolarItemRenderer();
    PolarPlot plot = new PolarPlot();
    plot.setDataset(d1);
    plot.setDataset(1, d2);
    plot.setRenderer(r);
    plot.setRenderer(1, new DefaultPolarItemRenderer());
    LegendItemCollection items = plot.getLegendItems();
    assertEquals(4, items.getItemCount());
    LegendItem item1 = items.get(0);
    assertEquals("A", item1.getLabel());
    LegendItem item2 = items.get(1);
    assertEquals("B", item2.getLabel());
    LegendItem item3 = items.get(2);
    assertEquals("C", item3.getLabel());
    LegendItem item4 = items.get(3);
    assertEquals("D", item4.getLabel());
}
Also used : LegendItem(org.jfree.chart.legend.LegendItem) LegendItemCollection(org.jfree.chart.legend.LegendItemCollection) DefaultPolarItemRenderer(org.jfree.chart.renderer.DefaultPolarItemRenderer) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection) Test(org.junit.jupiter.api.Test)

Example 8 with LegendItem

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

the class SpiderWebPlotTest method testGetLegendItems.

/**
 * Fetches the legend items and checks the values.
 */
@Test
public void testGetLegendItems() {
    DefaultCategoryDataset<String, String> dataset = new DefaultCategoryDataset<>();
    dataset.addValue(35.0, "S1", "C1");
    dataset.addValue(45.0, "S1", "C2");
    dataset.addValue(55.0, "S2", "C1");
    dataset.addValue(15.0, "S2", "C2");
    SpiderWebPlot plot = new SpiderWebPlot(dataset);
    JFreeChart chart = new JFreeChart(plot);
    LegendItemCollection legendItems = plot.getLegendItems();
    assertEquals(2, legendItems.getItemCount());
    LegendItem item1 = legendItems.get(0);
    assertEquals("S1", item1.getLabel());
    assertEquals("S1", item1.getSeriesKey());
    assertEquals(0, item1.getSeriesIndex());
    assertEquals(dataset, item1.getDataset());
    assertEquals(0, item1.getDatasetIndex());
    LegendItem item2 = legendItems.get(1);
    assertEquals("S2", item2.getLabel());
    assertEquals("S2", item2.getSeriesKey());
    assertEquals(1, item2.getSeriesIndex());
    assertEquals(dataset, item2.getDataset());
    assertEquals(0, item2.getDatasetIndex());
}
Also used : LegendItem(org.jfree.chart.legend.LegendItem) LegendItemCollection(org.jfree.chart.legend.LegendItemCollection) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) JFreeChart(org.jfree.chart.JFreeChart) Test(org.junit.jupiter.api.Test)

Example 9 with LegendItem

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

the class XYBarRenderer method getLegendItem.

/**
 * Returns a default legend item for the specified series.  Subclasses
 * should override this method to generate customised items.
 *
 * @param datasetIndex  the dataset index (zero-based).
 * @param series  the series index (zero-based).
 *
 * @return A legend item for the series.
 */
@Override
public LegendItem getLegendItem(int datasetIndex, int series) {
    XYPlot xyplot = getPlot();
    if (xyplot == null) {
        return null;
    }
    XYDataset dataset = xyplot.getDataset(datasetIndex);
    if (dataset == null) {
        return null;
    }
    LegendItem result;
    XYSeriesLabelGenerator lg = getLegendItemLabelGenerator();
    String label = lg.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);
    }
    Shape shape = this.legendBar;
    Paint paint = lookupSeriesPaint(series);
    Paint outlinePaint = lookupSeriesOutlinePaint(series);
    Stroke outlineStroke = lookupSeriesOutlineStroke(series);
    if (this.drawBarOutline) {
        result = new LegendItem(label, description, toolTipText, urlText, shape, paint, outlineStroke, outlinePaint);
    } else {
        result = new LegendItem(label, description, toolTipText, urlText, shape, paint);
    }
    result.setLabelFont(lookupLegendTextFont(series));
    Paint labelPaint = lookupLegendTextPaint(series);
    if (labelPaint != null) {
        result.setLabelPaint(labelPaint);
    }
    result.setDataset(dataset);
    result.setDatasetIndex(datasetIndex);
    result.setSeriesKey(dataset.getSeriesKey(series));
    result.setSeriesIndex(series);
    if (getGradientPaintTransformer() != null) {
        result.setFillPaintTransformer(getGradientPaintTransformer());
    }
    return result;
}
Also used : Stroke(java.awt.Stroke) Shape(java.awt.Shape) XYPlot(org.jfree.chart.plot.XYPlot) LegendItem(org.jfree.chart.legend.LegendItem) IntervalXYDataset(org.jfree.data.xy.IntervalXYDataset) XYDataset(org.jfree.data.xy.XYDataset) Paint(java.awt.Paint) XYSeriesLabelGenerator(org.jfree.chart.labels.XYSeriesLabelGenerator)

Example 10 with LegendItem

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

the class XYDifferenceRenderer method getLegendItem.

/**
 * Returns a default legend item for the specified series.  Subclasses
 * should override this method to generate customised items.
 *
 * @param datasetIndex  the dataset index (zero-based).
 * @param series  the series index (zero-based).
 *
 * @return A legend item for the series.
 */
@Override
public LegendItem getLegendItem(int datasetIndex, int series) {
    LegendItem result = null;
    XYPlot p = getPlot();
    if (p != null) {
        XYDataset dataset = p.getDataset(datasetIndex);
        if (dataset != null) {
            if (getItemVisible(series, 0)) {
                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);
                }
                Paint paint = lookupSeriesPaint(series);
                Stroke stroke = lookupSeriesStroke(series);
                Shape line = getLegendLine();
                result = new LegendItem(label, description, toolTipText, urlText, line, stroke, paint);
                result.setLabelFont(lookupLegendTextFont(series));
                Paint labelPaint = lookupLegendTextPaint(series);
                if (labelPaint != null) {
                    result.setLabelPaint(labelPaint);
                }
                result.setDataset(dataset);
                result.setDatasetIndex(datasetIndex);
                result.setSeriesKey(dataset.getSeriesKey(series));
                result.setSeriesIndex(series);
            }
        }
    }
    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)

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