//饼状图 public class PieCharts { public static void main(String[] args) throws IOException { PieDataset pieDataset=getDataset(); JFreeChart jfreechart = ChartFactory.createPieChart("CityInfoPort公司组织架构图",pieDataset,true,true,false); jfreechart.getTitle().setFont(new Font("宋体", Font.BOLD,14)); /*------这句代码解决了底部汉字乱码的问题-----------*/ jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); //获得图表显示对象 PiePlot pieplot = (PiePlot)jfreechart.getPlot(); //设置图表标签字体 pieplot.setLabelFont(new Font("SansSerif", Font.BOLD,12)); pieplot.setNoDataMessage("No data available"); pieplot.setCircular(true); pieplot.setLabelGap(0.01D);//间距 FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D://chart.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg,1,jfreechart,400,300,null); } finally { try { fos_jpg.close(); } catch (Exception e) {} } } public static PieDataset getDataset(){ DefaultPieDataset defaultpiedataset=new DefaultPieDataset(); defaultpiedataset.setValue("管理人员",10.02D); defaultpiedataset.setValue("市场人员",20.23D); defaultpiedataset.setValue("开发人员",60.02D); defaultpiedataset.setValue("OEM人员",10.02D); defaultpiedataset.setValue("其他人员",5.11D); return defaultpiedataset; } } //柱状图 public class ZhuCharts { public static void main(String[] args) throws IOException{ CategoryDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createBarChart3D( "水果产量图", // 图表标题 "水果", // 目录轴的显示标签 "产量", // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) false, // 是否生成工具 false // 是否生成URL链接 ); chart.getTitle().setFont(new Font("宋体", Font.BOLD,12)); //解决标题乱码 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); CategoryAxis domainAxis = categoryplot.getDomainAxis(); TextTitle textTitle = chart.getTitle(); textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D://fruit.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null); } finally { try { fos_jpg.close(); } catch (Exception e) {} } } /** * 获取一个演示用的简单数据集对象 * @return */ private static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "成都", "苹果"); dataset.addValue(200, "成都", "梨子"); dataset.addValue(300, "成都", "葡萄"); dataset.addValue(400, "成都", "香蕉"); dataset.addValue(500, "西昌", "苹果"); dataset.addValue(200, "西昌", "梨子"); dataset.addValue(300, "西昌", "葡萄"); dataset.addValue(400, "西昌", "香蕉"); return dataset; } /** * 获取一个演示用的组合数据集对象 * @return */ private static CategoryDataset getDataSet2() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(100, "上海", "苹果"); dataset.addValue(100, "广州", "苹果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "广州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "广州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "广州", "荔枝"); return dataset; } } //折线图 public class LineCharts { public static void main(String[] args) throws IOException { CategoryDataset linedataset = getDataset(); //定义图表对象 JFreeChart chart = ChartFactory.createLineChart("折线图", // chart title "时间", // domain axis label "销售额(百万)", // range axis label linedataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); chart.getTitle().setFont(new Font("宋体", Font.BOLD,12)); //解决标题乱码 CategoryPlot plot = chart.getCategoryPlot(); // customise the range axis... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(true); rangeAxis.setUpperMargin(0.20); rangeAxis.setLabelAngle(Math.PI / 2.0); CategoryAxis domainAxis = plot.getDomainAxis(); TextTitle textTitle = chart.getTitle(); textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); rangeAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D://line.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null); } finally { try { fos_jpg.close(); } catch (Exception e) {} } } public static DefaultCategoryDataset getDataset(){ DefaultCategoryDataset linedataset=new DefaultCategoryDataset(); // 各曲线名称 String series1 = "冰箱"; String series2 = "彩电"; String series3 = "洗衣机"; // 横轴名称(列名称) String type1 = "1月"; String type2 = "2月"; String type3 = "3月"; linedataset.addValue(0.0, series1, type1); linedataset.addValue(4.2, series1, type2); linedataset.addValue(3.9, series1, type3); linedataset.addValue(1.0, series2, type1); linedataset.addValue(5.2, series2, type2); linedataset.addValue(7.9, series2, type3); linedataset.addValue(2.0, series3, type1); linedataset.addValue(9.2, series3, type2); linedataset.addValue(8.9, series3, type3); return linedataset; } } //jsp页面显示JfreeChart //产品统计分析图表 public String charts() throws Exception{ CategoryDataset linedataset = getDataset(); //定义图表对象 JFreeChart chart = ChartFactory.createLineChart("折线图", // chart title "时间", // domain axis label "销售额(百万)", // range axis label linedataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); chart.getTitle().setFont(new Font("宋体", Font.BOLD,12)); //解决标题乱码 CategoryPlot plot = chart.getCategoryPlot(); // customise the range axis... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(true); rangeAxis.setUpperMargin(0.20); rangeAxis.setLabelAngle(Math.PI / 2.0); CategoryAxis domainAxis = plot.getDomainAxis(); TextTitle textTitle = chart.getTitle(); textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); rangeAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); HttpServletResponse response=ServletActionContext.getResponse(); response.reset(); response.setContentType("image/jpeg"); ServletOutputStream outs = response.getOutputStream(); ChartUtilities.writeChartAsJPEG(response.getOutputStream(),chart,500,300); outs.flush();//清空缓存 return null; }