public String barChart() throws Exception{ DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(200, "上海", "苹果"); dataset.addValue(150, "广州", "苹果"); dataset.addValue(260, "北京", "梨子"); dataset.addValue(210, "上海", "梨子"); dataset.addValue(120, "广州", "梨子"); dataset.addValue(370, "北京", "葡萄"); dataset.addValue(310, "上海", "葡萄"); dataset.addValue(340, "广州", "葡萄"); dataset.addValue(410, "北京", "香蕉"); dataset.addValue(480, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(530, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(580, "广州", "荔枝"); JFreeChart jfree = ChartFactory.createBarChart3D("水果产量图", "水果", "产量", dataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot categoryplot = (CategoryPlot) jfree.getPlot(); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); CategoryAxis domainAxis = categoryplot.getDomainAxis(); TextTitle textTitle = jfree.getTitle(); textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 14)); domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16)); numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 14)); numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 16)); jfree.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 16)); //path代表当前服务器上下文路径 String path = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/"); if(imgName==null || "".equals(imgName)){ imgName = "default"; } //保存图片 ChartUtilities.saveChartAsJPEG(new File(path + "img/" + imgName + "Bar.jpg"), jfree, 535, 400); return "success"; }
效果图在附件里