Ejemplo n.º 1
0
  /** Fetches the latest legend items. */
  protected void fetchLegendItems() {
    this.items.clear();
    RectangleEdge p = getPosition();
    if (RectangleEdge.isTopOrBottom(p)) {
      this.items.setArrangement(this.hLayout);
    } else {
      this.items.setArrangement(this.vLayout);
    }

    if (this.sortOrder.equals(SortOrder.ASCENDING)) {
      for (int s = 0; s < this.sources.length; s++) {
        LegendItemCollection legendItems = this.sources[s].getLegendItems();
        if (legendItems != null) {
          for (int i = 0; i < legendItems.getItemCount(); i++) {
            addItemBlock(legendItems.get(i));
          }
        }
      }
    } else {
      for (int s = this.sources.length - 1; s >= 0; s--) {
        LegendItemCollection legendItems = this.sources[s].getLegendItems();
        if (legendItems != null) {
          for (int i = legendItems.getItemCount() - 1; i >= 0; i--) {
            addItemBlock(legendItems.get(i));
          }
        }
      }
    }
  }
  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);
  }