/**
   * A check for the datasetIndex and seriesIndex fields in the LegendItem returned by the
   * getLegendItem() method.
   */
  public void testGetLegendItemSeriesIndex() {
    XYSeriesCollection d1 = new XYSeriesCollection();
    XYSeries s1 = new XYSeries("S1");
    s1.add(1.0, 1.1);
    XYSeries s2 = new XYSeries("S2");
    s2.add(1.0, 1.1);
    d1.addSeries(s1);
    d1.addSeries(s2);

    XYSeriesCollection d2 = new XYSeriesCollection();
    XYSeries s3 = new XYSeries("S3");
    s3.add(1.0, 1.1);
    XYSeries s4 = new XYSeries("S4");
    s4.add(1.0, 1.1);
    XYSeries s5 = new XYSeries("S5");
    s5.add(1.0, 1.1);
    d2.addSeries(s3);
    d2.addSeries(s4);
    d2.addSeries(s5);

    XYDotRenderer r = new XYDotRenderer();
    XYPlot plot = new XYPlot(d1, new NumberAxis("x"), new NumberAxis("y"), r);
    plot.setDataset(1, d2);
    /*JFreeChart chart =*/ new JFreeChart(plot);
    LegendItem li = r.getLegendItem(1, 2);
    assertEquals("S5", li.getLabel());
    assertEquals(1, li.getDatasetIndex());
    assertEquals(2, li.getSeriesIndex());
  }
  public void updateAndSortLegend(LegendItemCollection legendItemsOld) {
    HashMap<String, LegendItem> oldIndexes = new HashMap<String, LegendItem>();

    // Finding old indexes and saving them for sorting (not including those starting with "var"
    for (int i = 0; i < legendItemsOld.getItemCount(); i++) {
      LegendItem item = legendItemsOld.get(i);
      if (!item.getLabel().equals("var")) {
        oldIndexes.put(trimLabel(item.getLabel()), item);
      }
    }

    // Sorting the new legend
    final LegendItemCollection legendItemsNew = new LegendItemCollection();
    Object[] keys = oldIndexes.keySet().toArray();
    Arrays.sort(keys);
    for (Object key : keys) {
      legendItemsNew.add(oldIndexes.get(key));
    }

    LegendItemSource source =
        new LegendItemSource() {
          LegendItemCollection lic = new LegendItemCollection();

          {
            lic.addAll(legendItemsNew);
          }

          public LegendItemCollection getLegendItems() {
            return lic;
          }
        };

    // Forcing legend to be in bottom
    LegendTitle legend = new LegendTitle(source);
    legend.setPosition(RectangleEdge.BOTTOM);
    legend.setBorder(1, 1, 1, 1);

    chart.removeLegend();
    chart.addLegend(legend);
  }
示例#3
0
 /**
  * A check for the datasetIndex and seriesIndex fields in the LegendItem returned by the
  * getLegendItem() method.
  */
 @Test
 public void testGetLegendItemSeriesIndex() {
   DefaultCategoryDataset dataset0 = new DefaultCategoryDataset();
   dataset0.addValue(21.0, "R1", "C1");
   dataset0.addValue(22.0, "R2", "C1");
   DefaultCategoryDataset 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 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());
 }
  /**
   * Creates a legend item block.
   *
   * @param item the legend item.
   * @return The block.
   */
  protected Block createLegendItemBlock(LegendItem item) {
    BlockContainer result = null;
    LegendGraphic lg = new LegendGraphic(item.getShape(), item.getFillPaint());
    lg.setFillPaintTransformer(item.getFillPaintTransformer());
    lg.setShapeFilled(item.isShapeFilled());
    lg.setLine(item.getLine());
    lg.setLineStroke(item.getLineStroke());
    lg.setLinePaint(item.getLinePaint());
    lg.setLineVisible(item.isLineVisible());
    lg.setShapeVisible(item.isShapeVisible());
    lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
    lg.setOutlinePaint(item.getOutlinePaint());
    lg.setOutlineStroke(item.getOutlineStroke());
    lg.setPadding(this.legendItemGraphicPadding);

    LegendItemBlockContainer legendItem =
        new LegendItemBlockContainer(
            new BorderArrangement(), item.getDataset(), item.getSeriesKey());
    lg.setShapeAnchor(getLegendItemGraphicAnchor());
    lg.setShapeLocation(getLegendItemGraphicLocation());
    legendItem.add(lg, this.legendItemGraphicEdge);
    Font textFont = item.getLabelFont();
    if (textFont == null) {
      textFont = this.itemFont;
    }
    Paint textPaint = item.getLabelPaint();
    if (textPaint == null) {
      textPaint = this.itemPaint;
    }
    LabelBlock labelBlock = new LabelBlock(item.getLabel(), textFont, textPaint);
    labelBlock.setPadding(this.itemLabelPadding);
    legendItem.add(labelBlock);
    legendItem.setToolTipText(item.getToolTipText());
    legendItem.setURLText(item.getURLText());

    result = new BlockContainer(new CenterArrangement());
    result.add(legendItem);

    return result;
  }