<chart> <set label="Paper Cost" value="25" /> <set label="Binding" value="20" /> <set label="Printing Cost" value="20" /> <set label="Royality" value="15" /> <set label="Transportation Cost" value="10" /> <set label="Promotion Cost" value="10" /> </chart>
代码如下
package com.oracle.bingTu; import java.io.File; import java.util.ArrayList; import java.util.List; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Polygon; import java.awt.Shape; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.DefaultDrawingSupplier; import org.jfree.chart.plot.DrawingSupplier; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class Test { private static List<String> lable1 = new ArrayList<String>(); private static List<Integer> value1 = new ArrayList<Integer>(); /** * 读XML文件 */ public static void readXML() { SAXReader saxReader = new SAXReader(); try { Document doc = saxReader.read(new File("321.xml")); Element element = doc.getRootElement(); List list = element.elements(); for (int i = 0; i < list.size(); i++) { Element elementz = (Element) list.get(i); List list1 = elementz.elements(); if (elementz.getName().equals("set")) { lable1.add(elementz.attributeValue("label")); value1.add(Integer.parseInt(elementz .attributeValue("value"))); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i = 0 ;i<lable1.size();i++){ System.out.println(lable1.get(i)+" "+value1.get(i)); } } public static void main(String[] args) { // TODO Auto-generated method stub Test.readXML(); DefaultPieDataset dpd = new DefaultPieDataset(); // 建立一个默认的饼图 for(int i = 0 ;i<lable1.size();i++){ dpd.setValue(lable1.get(i),value1.get(i)); } JFreeChart chart = ChartFactory.createPieChart("某公司人员组织数据图",dpd,true,false); // 可以查具体的API文档,第一个参数是标题,第二个参数是一个数据集,第三个参数表示是否显示Legend,第四个参数表示是否显示提示,第五个参数表示图中是否存在URL Font titleFont = new Font("黑体",Font.BOLD,20); TextTitle textTitle = chart.getTitle(); textTitle.setFont(titleFont);// 为标题设置上字体 Font plotFont = new Font("宋体",Font.PLAIN,16); PiePlot plot = (PiePlot) chart.getPlot(); plot.setLabelFont(plotFont); // 为饼图元素设置上字体 Font LegendFont = new Font("楷体",18); LegendTitle legend = chart.getLegend(0); legend.setItemFont(LegendFont);// 为图例说明设置字体 ChartFrame chartFrame = new ChartFrame("某公司人员组织数据图",chart); // chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。 chartFrame.pack(); // 以合适的大小展现图形 chartFrame.setVisible(true);// 图形是否可见 } }