改变legend(图释的位置)

 1 import java.awt.Color;
 2 import java.awt.Graphics;
 3 import java.awt.Graphics2D;
 4 import java.awt.Rectangle;
 5 import java.util.Map;
 6 
 7 import javax.swing.JComponent;
 8 import javax.swing.JFrame;
 9 import javax.swing.border.LineBorder;
10 
11 import org.jfree.chart.ChartFactory;
12 import org.jfree.chart.ChartFrame;
13 import org.jfree.chart.ChartTheme;
14 import org.jfree.chart.JFreeChart;
15 import org.jfree.chart.StandardChartTheme;
16 import org.jfree.chart.block.BlockFrame;
17 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
18 import org.jfree.chart.labels.StandardPieToolTipGenerator;
19 import org.jfree.chart.plot.PiePlot;
20 import org.jfree.chart.title.LegendTitle;
21 import org.jfree.data.general.DefaultPieDataset;
22 import org.jfree.ui.RectangleEdge;
23 import org.jfree.ui.RectangleInsets;
24 import org.jfree.util.Rotation;
25 
26 
27 import com.lowagie.text.Font;
28 
29 public class Main {
30    public static void main(String[] argv) {
31        DefaultPieDataset dataset = new DefaultPieDataset();
32             dataset.setValue("a",15);
33             dataset.setValue("b",20);
34             dataset.setValue("c",25);
35             dataset.setValue("d",30);
36             dataset.setValue("e",3);
37             dataset.setValue("f",2);
38             dataset.setValue("g",4);
39             dataset.setValue("h",5);
40 
41         PiePlot plot = new PiePlot(dataset);
42         JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

//set legend
43 LegendTitle legend = chart.getLegend(); 44 legend.setPosition(RectangleEdge.RIGHT);

//invisible the frames around legend
      legend.setBorder(0, 0, 0, 0);

45 ChartTheme currentTheme = new StandardChartTheme("JFree"); 46 currentTheme.apply(chart); 47 48 plot.setStartAngle(290); 49 plot.setDirection(Rotation.CLOCKWISE); 50 plot.setForegroundAlpha(0.7f); 51 plot.setBackgroundAlpha(0.0f); 52 // plot.setSimpleLabels(true); 53 54 //disable labels 55 plot.setLabelLinksVisible(false); 56 plot.setLabelPaint(Color.white); 57 plot.setLabelShadowPaint(Color.white); 58 plot.setLabelBackgroundPaint(Color.white); 59 // plot.setLabelLinkPaint(Color.white); 60 plot.setLabelOutlinePaint(Color.white); 61 62 //set outline for pie 63 plot.setOutlinePaint(Color.white); 64 65 66 67 68 // plot.setLabelFont(new Font("Arial", Font.BOLD, 10)); 69 plot.setLegendLabelGenerator( 70 new StandardPieSectionLabelGenerator("{0}: {1}({2})")); 71 // plot.setLegendLabelToolTipGenerator( 72 // new StandardPieSectionLabelGenerator("{0} {1}, {2}")); 73 plot.setNoDataMessage("No data to display"); 74 ChartFrame frame = new ChartFrame("chart",chart); 75 frame.setVisible(true); 76 frame.setSize(500,500); 77 } 78 }

This pie does not has lebal,all of which has been set white or transparent. And put the legend on the right side.

posted on 2013-05-31 05:45  Step-BY-Step  阅读(683)  评论(0编辑  收藏  举报

导航