/** * Creates a ring chart with default settings. * <P> * The chart object returned by this method uses a {@link RingPlot} * instance as the plot. * * @param title the chart title (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A pie chart. */ public static JFreeChart createRingChart(String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) { RingPlot plot = new RingPlot(dataset); plot.setLabelGenerator(new StandardPieSectionLabelGenerator()); plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); if (tooltips) { plot.setToolTipGenerator(new StandardPieToolTipGenerator( StandardPieToolTipGenerator.DEFAULT_SECTION_LABEL_FORMAT)); } if (urls) { plot.setURLGenerator(new StandardPieURLGenerator()); } return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); }
public static void setPieRender(Plot plot) { plot.setNoDataMessage(NO_DATA_MSG); plot.setInsets(new RectangleInsets(10, 10, 5, 10)); PiePlot piePlot = (PiePlot) plot; piePlot.setInsets(new RectangleInsets(0, 0, 0, 0)); piePlot.setCircular(true); piePlot.setLabelGap(0.01); piePlot.setInteriorGap(0.05D); piePlot.setLegendItemShape(new Rectangle(10, 10)); piePlot.setIgnoreNullValues(true); piePlot.setLabelBackgroundPaint(null); piePlot.setLabelShadowPaint(null); piePlot.setLabelOutlinePaint(null); piePlot.setShadowPaint(null); // 0:category 1:value:2 :percentage piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{2}")); }
public PieChart() { DefaultPieDataset data = getDataSet(); JFreeChart chart = ChartFactory.createPieChart3D("水果产量", data, true, false, false); //设置百分比 PiePlot pieplot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题 NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象 StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//获得StandardPieSectionLabelGenerator对象 pieplot.setLabelGenerator(sp1);//设置饼图显示百分比 //没有数据的时候显示的内容 pieplot.setNoDataMessage("无数据显示"); pieplot.setCircular(false); pieplot.setLabelGap(0.02D); pieplot.setIgnoreNullValues(true);//设置不显示空值 pieplot.setIgnoreZeroValues(true);//设置不显示负值 frame1 = new ChartPanel(chart, true); chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));//设置标题字体 PiePlot piePlot = (PiePlot) chart.getPlot();//获取图表区域对象 piePlot.setLabelFont(new Font("宋体", Font.BOLD, 10));//解决乱码 chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 10)); }
/** * Creates a pie chart with default settings. * <P> * The chart object returned by this method uses a {@link PiePlot} instance * as the plot. * * @param title the chart title (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A pie chart. */ public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) { PiePlot plot = new PiePlot(dataset); plot.setLabelGenerator(new StandardPieSectionLabelGenerator()); plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); if (tooltips) { plot.setToolTipGenerator(new StandardPieToolTipGenerator( StandardPieToolTipGenerator.DEFAULT_SECTION_LABEL_FORMAT)); } if (urls) { plot.setURLGenerator(new StandardPieURLGenerator()); } return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); }
/** * Check cloning of the legendLabelGenerator field. */ @Test public void testCloning_LegendLabelGenerator() throws CloneNotSupportedException { StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(); PiePlot p1 = new PiePlot(); p1.setLegendLabelGenerator(generator); PiePlot p2 = (PiePlot) p1.clone(); assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // change the generator and make sure it only affects p1 generator.getNumberFormat().setMinimumFractionDigits(2); assertFalse(p1.equals(p2)); }
/** * Check cloning of the legendLabelToolTipGenerator field. */ @Test public void testCloning_LegendLabelToolTipGenerator() throws CloneNotSupportedException { StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(); PiePlot p1 = new PiePlot(); p1.setLegendLabelToolTipGenerator(generator); PiePlot p2 = (PiePlot) p1.clone(); assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // change the generator and make sure it only affects p1 generator.getNumberFormat().setMinimumFractionDigits(2); assertFalse(p1.equals(p2)); }
/** * Creates a pie chart with default settings. * <P> * The chart object returned by this method uses a {@link PiePlot} instance * as the plot. * * @param title the chart title (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A pie chart. */ public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) { PiePlot plot = new PiePlot(dataset); plot.setLabelGenerator(new StandardPieSectionLabelGenerator()); plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); if (tooltips) { plot.setToolTipGenerator(new StandardPieToolTipGenerator()); } if (urls) { plot.setURLGenerator(new StandardPieURLGenerator()); } JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
/** * Creates a ring chart with default settings. * <P> * The chart object returned by this method uses a {@link RingPlot} * instance as the plot. * * @param title the chart title (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A ring chart. */ public static JFreeChart createRingChart(String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) { RingPlot plot = new RingPlot(dataset); plot.setLabelGenerator(new StandardPieSectionLabelGenerator()); plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); if (tooltips) { plot.setToolTipGenerator(new StandardPieToolTipGenerator()); } if (urls) { plot.setURLGenerator(new StandardPieURLGenerator()); } JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
/** * Creates a pie chart with default settings. * <P> * The chart object returned by this method uses a {@link PiePlot} instance * as the plot. * * @param title the chart title ({@code null} permitted). * @param dataset the dataset for the chart ({@code null} permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A pie chart. */ public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) { PiePlot plot = new PiePlot(dataset); plot.setLabelGenerator(new StandardPieSectionLabelGenerator()); plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); if (tooltips) { plot.setToolTipGenerator(new StandardPieToolTipGenerator()); } if (urls) { plot.setURLGenerator(new StandardPieURLGenerator()); } JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
/** * Creates a ring chart with default settings. * <P> * The chart object returned by this method uses a {@link RingPlot} * instance as the plot. * * @param title the chart title ({@code null} permitted). * @param dataset the dataset for the chart ({@code null} permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A ring chart. */ public static JFreeChart createRingChart(String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) { RingPlot plot = new RingPlot(dataset); plot.setLabelGenerator(new StandardPieSectionLabelGenerator()); plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0)); if (tooltips) { plot.setToolTipGenerator(new StandardPieToolTipGenerator()); } if (urls) { plot.setURLGenerator(new StandardPieURLGenerator()); } JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); currentTheme.apply(chart); return chart; }
private JFreeChart createChart(PieDataset dataset, String title) { JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title dataset, // data true, // include legend true, true); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator( "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%")); plot.setLabelGenerator(gen); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); return chart; }
public String PieChart() { DefaultPieDataset dataset = new DefaultPieDataset(); List<Datas> list = new IncomeDao().getDatas2(park.getParkid()); for (Datas datas : list) { dataset.setValue(datas.getYear() + "年" + datas.getMonth() + "月", datas.getMoney()); } ChartFactory.setChartTheme(getStandardChartTheme()); chart = ChartFactory.createPieChart3D("停车场饼图", dataset, true, true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setBaseSectionOutlinePaint(Color.RED);// 设置图形边框颜色 plot.setBaseSectionOutlineStroke(new BasicStroke(1.0f));// 设置图形边框粗细 plot.setForegroundAlpha(0.5f);// 数据区的前景透明度(0.0~1.0) plot.setCircular(true);// 饼图是否一定是正圆 plot.setStartAngle(360);// 饼图的初始角度 plot.setToolTipGenerator(new StandardPieToolTipGenerator());// 设置鼠标悬停提示 plot.setLabelFont(new Font("微软雅黑", Font.PLAIN, 14)); StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator( "{0}:({1},{2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()); plot.setLabelGenerator(standarPieIG); chart.setTitle(getTitle()); return SUCCESS; }
public void makeChartByMap(Map<String, Object> map,String title) { DefaultPieDataset dpd = new DefaultPieDataset(); //建立一个默认的饼图 System.out.println(map); dpd.setValue("优", Integer.parseInt(map.get("优").toString())); dpd.setValue("良", Integer.parseInt(map.get("良").toString())); dpd.setValue("中", Integer.parseInt(map.get("中").toString())); dpd.setValue("差", Integer.parseInt(map.get("差").toString())); dpd.setValue("不及格", Integer.parseInt(map.get("不及格").toString())); JFreeChart chart = ChartFactory.createPieChart(title, dpd, true, true, false); PiePlot piePlot = (PiePlot) chart.getPlot(); piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}:({2})"), NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); //可以查具体的API文档,第一个参数是标题,第二个参数是一个数据集,第三个参数表示是否显示Legend,第四个参数表示是否显示提示,第五个参数表示图中是否存在URL ChartFrame chartFrame = new ChartFrame(title, chart); //chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。 chartFrame.pack(); //以合适的大小展现图形 chartFrame.setVisible(true);//图形是否可见 }
private static JFreeChart createPieChart(PieDataset piedataset, GraphDataItem item) { JFreeChart jfreechart = ChartFactory.createPieChart(item.getTitle(), piedataset, true, true, false); Font font = new Font("宋体", Font.PLAIN, 13); jfreechart.getTitle().setFont(font); jfreechart.getLegend().setItemFont(font); PiePlot pieplot = (PiePlot) jfreechart.getPlot(); pieplot.setBackgroundPaint(ChartColor.WHITE); pieplot.setLabelFont(font); pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}: ({2})"), NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); pieplot.setLabelBackgroundPaint(new Color(220, 220, 220)); pieplot.setSimpleLabels(true); pieplot.setInteriorGap(0.0D); int index = 0; for (Object name : item.getDatas().keySet()) { pieplot.setSectionPaint((String) name, COLORS[index % COLORS.length]); index++; } return jfreechart; }
private static JFreeChart createChart(final PieDataset dataset, String str) { final JFreeChart chart = ChartFactory.createPieChart3D( str, // chart title dataset, // data true, // include legend true, false ); final PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); StandardPieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0} {2}"); plot.setLabelGenerator(labelGenerator); plot.setNoDataMessage("No data to display"); return chart; }