List of usage examples for org.jfree.chart LegendItemSource getLegendItems
public LegendItemCollection getLegendItems();
From source file:com.hmsinc.epicenter.webapp.chart.ChartService.java
/** * @param chart// w w w. j av a 2 s . c om * @return */ private static void configureTitleAndLegend(final JFreeChart chart, final AbstractChart adapter) { final TextTitle title = chart.getTitle(); if (title != null) { title.setFont(new Font("Arial", Font.BOLD, 12)); // title.setBackgroundPaint(Color.CYAN); title.setTextAlignment(HorizontalAlignment.LEFT); title.setHorizontalAlignment(HorizontalAlignment.CENTER); title.setMargin(0, 4, 5, 6); } if (chart.getLegend() != null) { chart.removeLegend(); final LegendTitle legend = new LegendTitle(chart.getPlot(), new SNColumnArrangement(0, 0), new ColumnArrangement(HorizontalAlignment.CENTER, VerticalAlignment.CENTER, 0, 0)); legend.setItemFont(LEGEND_FONT); legend.setPosition(RectangleEdge.BOTTOM); legend.setHorizontalAlignment(HorizontalAlignment.CENTER); legend.setBackgroundPaint(Color.WHITE); legend.setFrame(new LineBorder()); legend.setMargin(0, 4, 5, 6); chart.addLegend(legend); // Now we'll try to remove any duplicate items from the legend.. final Map<String, Integer> keys = new HashMap<String, Integer>(); final LegendItemCollection items = new LegendItemCollection(); for (LegendItemSource source : legend.getSources()) { for (int i = 0; i < source.getLegendItems().getItemCount(); i++) { final LegendItem item = source.getLegendItems().get(i); if (!keys.containsKey(item.getLabel())) { keys.put(item.getLabel(), i); items.add(item); } } } legend.setSources(new LegendItemSource[] { new LegendItemSource() { /* * (non-Javadoc) * * @see org.jfree.chart.LegendItemSource#getLegendItems() */ public LegendItemCollection getLegendItems() { return items; } } }); } }
From source file:org.talend.dataprofiler.chart.ChartDecorator.java
/** * //from w w w . ja v a 2 s. c o m * if it contians CJK, set Font to "Arial Unicode MS".Or else, the Font is "Tahoma". * * @param chart */ private static void setLegendFont(JFreeChart chart) { Font font; LegendTitle legend = chart.getLegend(); if (legend != null) { font = new Font("Tahoma", Font.PLAIN, BASE_LEGEND_LABEL_SIZE);//$NON-NLS-1$ // get legend label to judge if it contains CJK. LegendItemSource[] sources = legend.getSources(); Set<String> itemLabels = new HashSet<String>(); for (LegendItemSource source : sources) { LegendItemCollection legendItems = source.getLegendItems(); for (int i = 0; i < legendItems.getItemCount(); i++) { String label = legendItems.get(i).getLabel(); if (label != null) { itemLabels.add(label); } } } if (isContainCJKCharacter(itemLabels.toArray())) { font = getCJKFont(Font.PLAIN, BASE_LEGEND_LABEL_SIZE); } legend.setItemFont(font); } }