java中jfreechart的兑现.柱状图+饼状图
java中jfreechart的实现.柱状图+饼状图
jar包:jfreechart-1.0.8a.jar
java类1
以下类是柱状图和饼状图实现的公共类,要使用的时候直接调用以下类的方法就是
java实现类2
调用以上方法实现。
jsp页面:
自己写的..以后用的着
jar包:jfreechart-1.0.8a.jar
java类1
以下类是柱状图和饼状图实现的公共类,要使用的时候直接调用以下类的方法就是
package com.hz.util; import java.awt.Font; import java.io.IOException; import java.text.DecimalFormat; import java.text.NumberFormat; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.AxisLocation; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.block.BlockContainer; import org.jfree.chart.block.BorderArrangement; import org.jfree.chart.block.EmptyBlock; import org.jfree.chart.entity.StandardEntityCollection; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.DatasetRenderingOrder; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.chart.title.CompositeTitle; import org.jfree.chart.title.LegendTitle; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.ui.RectangleEdge; import org.jfree.ui.RectangleInsets; import org.jfree.ui.TextAnchor; public class BarChart { protected HttpServletRequest request = ServletActionContext.getRequest(); //柱状图的实现方法 public JFreeChartPojo barCharts(String title,String x,String y,DefaultCategoryDataset dataset) throws IOException{ JFreeChartPojo jFreeChartPojo = new JFreeChartPojo(); JFreeChart chart = ChartFactory.createBarChart3D(title, x, y, dataset, PlotOrientation.VERTICAL, false, false, false); CategoryPlot plot = chart.getCategoryPlot(); CategoryAxis categoryaxis = plot.getDomainAxis(); NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis(); categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); categoryaxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));//X说明的字体 categoryaxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//X显示的字体 numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));//Y显示的字体 numberaxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//Y说明的字体 numberaxis.setUpperMargin(0.1); BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer(); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelFont(new Font("TimesRoman", Font.PLAIN, 12)); //柱状图上面的字体 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); renderer.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字偏离值 int k = dataset.getColumnCount(); int b; //以下判断是控制柱子的多少在页面上显示的距离 if (k == 1) { plot.getDomainAxis().setLowerMargin(0.2);//设置柱子距离左端距离 plot.getDomainAxis().setUpperMargin(0.2);//设置柱子距离右端距离 ((BarRenderer3D) plot.getRenderer()).setItemMargin(0.5); b=200; }else if(k<4){ plot.getDomainAxis().setLowerMargin(0.2);//设置柱子距离左端距离 plot.getDomainAxis().setUpperMargin(0.2);//设置柱子距离右端距离 renderer.setItemMargin(0.001); b=250; } else if (k < 8) { b=500; renderer.setMaximumBarWidth(0.04);//设置柱子的宽度 renderer.setMinimumBarLength(0.1); double margin = (1.0 - k * 0.08) / 3; plot.getDomainAxis().setLowerMargin(0.05); plot.getDomainAxis().setUpperMargin(0.05); ((BarRenderer3D) plot.getRenderer()).setItemMargin(margin); } else if(k<15){ b=800; renderer.setMaximumBarWidth(0.04);//设置柱子的最大宽度 ((BarRenderer3D) plot.getRenderer()).setItemMargin(0.1); }else if(k<40){ b=1000; renderer.setMaximumBarWidth(0.03);//设置柱子的最大宽度 plot.getDomainAxis().setLowerMargin(0.02); plot.getDomainAxis().setUpperMargin(0.02); ((BarRenderer3D) plot.getRenderer()).setItemMargin(0.0001); }else{ b=1000; } plot.setRenderer(renderer); HttpSession session = request.getSession(); String filename = ServletUtilities.saveChartAsPNG(chart, b, 400, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; jFreeChartPojo.setFilename(filename); jFreeChartPojo.setGraphURL(graphURL); return jFreeChartPojo; } //------- //饼状图实现方法 //------- public JFreeChartPojo pieCharts(DefaultPieDataset data,String title) throws IOException{ JFreeChartPojo jFreeChartPojo = new JFreeChartPojo(); PiePlot plot = new PiePlot(data); plot.setCircular(true); plot.setLabelFont(new Font("宋体", Font.PLAIN, 13)); StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(("{0}={1}({2})"), NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")); plot.setLabelGenerator(generator); JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true); chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色 chart.setTitle(title);//可选,设置图片标题 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); //500是图片长度,300是图片高度 HttpSession session = request.getSession(); String filename = ServletUtilities.saveChartAsPNG(chart, 600, 400, info, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; jFreeChartPojo.setFilename(filename); jFreeChartPojo.setGraphURL(graphURL); return jFreeChartPojo; } public JFreeChartPojo dualAxisCharts() throws IOException{ JFreeChartPojo jFreeChartPojo = new JFreeChartPojo(); String s = "S1"; String s3 = "无锡--上海"; String s4 = "Category 2"; String s5 = "Category 3"; String s6 = "Category 4"; String s7 = "Category 5"; String s8 = "Category 6"; String s9 = "Category 7"; String s10 = "Category 8"; DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); defaultcategorydataset.addValue(1, s, s3); defaultcategorydataset.addValue(4, s, s4); defaultcategorydataset.addValue(3, s, s5); defaultcategorydataset.addValue(5, s, s6); defaultcategorydataset.addValue(5, s, s7); defaultcategorydataset.addValue(7, s, s8); defaultcategorydataset.addValue(7, s, s9); defaultcategorydataset.addValue(8, s, s10); JFreeChart chart = ChartFactory.createBarChart("Dual Axis Chart", "Category", "Value", defaultcategorydataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot categoryplot = (CategoryPlot)chart.getPlot(); categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); //CategoryDataset categorydataset =defaultcategorydataset_1; //categoryplot.setDataset(1, categorydataset); categoryplot.mapDatasetToRangeAxis(1, 1); CategoryAxis categoryaxis = categoryplot.getDomainAxis(); categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); NumberAxis numberaxis = new NumberAxis("Secondary"); categoryplot.setRangeAxis(1, numberaxis); LineAndShapeRenderer lineandshaperenderer =new LineAndShapeRenderer(); // lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); lineandshaperenderer.setBaseLinesVisible(true); // lineandshaperenderer.setShapesFilled(true); // lineandshaperenderer.setShapesVisible(true); // lineandshaperenderer.setItemLabelsVisible(true); categoryplot.setRenderer(1, lineandshaperenderer); categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); LegendTitle legendtitle = new LegendTitle(categoryplot.getRenderer(0)); legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D)); //legendtitle.setBorder(new BlockBorder()); LegendTitle legendtitle1 = new LegendTitle(categoryplot.getRenderer(1)); legendtitle1.setMargin(new RectangleInsets(2D, 2D, 2D, 2D)); //legendtitle1.setBorder(new BlockBorder()); BlockContainer blockcontainer = new BlockContainer(new BorderArrangement()); blockcontainer.add(legendtitle, RectangleEdge.LEFT); blockcontainer.add(legendtitle1, RectangleEdge.RIGHT); blockcontainer.add(new EmptyBlock(2000D, 0.0D)); CompositeTitle compositetitle = new CompositeTitle(blockcontainer); compositetitle.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(compositetitle); ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); //500是图片长度,300是图片高度 HttpSession session = request.getSession(); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; jFreeChartPojo.setFilename(filename); jFreeChartPojo.setGraphURL(graphURL); return jFreeChartPojo; } }
java实现类2
调用以上方法实现。
public String timeQuery() throws Exception { String qssj = request.getParameter("qssj"); String jzsj = request.getParameter("jzsj"); String cycle = request.getParameter("cycle"); if (qssj.equals("")) { qssj = "1980-01-01 00:00:00"; } if (jzsj.equals("")) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); jzsj = formatter.format(date); } List<?> list = iAnalysisSpeedingCarBo.queryTime(qssj, jzsj, cycle); List<SpaceCarPojo> timeQuerySpeedingCar = new ArrayList<SpaceCarPojo>(); DatePojo datePojo = new DatePojo(); UtilMethod utilMethod = new UtilMethod(); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); try { List<?> arylist = (ArrayList<?>) list.get(0); for (int i = 0; i < arylist.size(); i++) { String a = ((Object[]) arylist.get(i))[0].toString(); String b = ((Object[]) arylist.get(i))[1].toString(); SpaceCarPojo spaceCarPojo = new SpaceCarPojo(); String q = null; String year = null; if (cycle.equals("year")) { a = a + "年"; } if (cycle.equals("q")) { q = a.substring(5); year = a.substring(0, 4); if (q.equals("1")) { a = year + "年" + "春季(第一季)"; } if (q.equals("2")) { a = year + "年" + "夏季(第二季)"; } if (q.equals("3")) { a = year + "年" + "秋季(第三季)"; } if (q.equals("4")) { a = year + "年" + "冬季(第四季)"; } } if (cycle.equals("month")) { a = a + "月"; } if (cycle.equals("iw")) { String years = a.substring(0, 4); String weeks = a.substring(5); a = years + "年第" + weeks + "周"; } if (cycle.equals("day")) { a = a + "日"; } datePojo.setDateBegin(qssj); datePojo.setDateEnd(jzsj); spaceCarPojo.setDate(utilMethod.trim(a)); spaceCarPojo.setCount(utilMethod.trim(b)); dataset.addValue(Integer.parseInt(utilMethod.trim(b)), "1", utilMethod.trim(a)); timeQuerySpeedingCar.add(spaceCarPojo); } BarChart BarChart = new BarChart(); //这里调用了java类1 JFreeChartPojo jFreeChartPojo = BarChart.barCharts("时间特征分析", "时间", "车辆数", dataset); request.setAttribute("jFreeChart", jFreeChartPojo); request.setAttribute("datePojo", datePojo); // request.setAttribute("speedingCar", timeQuerySpeedingCar); } catch (Exception e) { e.printStackTrace(); } return "xx";//返回stucts2配置 }
jsp页面:
<table id="table1" style="display: none" width="100%"> <tr> <td width="10%"></td> <td width="90%"> <img id="img1" style="display: none" src="${jFreeChart.graphURL }" border="0" usemap="${jFreeChart.filename }"> <br> </td> </tr> <tr> <td></td> <td style="color: #DD0919;"> ${jFreeChart.description } </td> </tr> </table> //注:action url调用就不加以说明了.. //直接上效果图
1 楼
xn792474
2010-12-02