struts+mybatis 根据遍历出的数据库数据画折线图

来源:互联网 发布:mac os x10.6下载 编辑:程序博客网 时间:2024/06/06 01:44

1.导包:

jcommon-1.0.16.jar
jfreechart-1.0.19.jar

2.写工具类:

public class ChartOne {//数据采集 (被调用)  private static CategoryDataset createDataset()  {    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();    //第一条折线数据 折线名Series 1    List<Customer> custlist=CustomerServiceImpl.getInstance().getDayPay();    for(Customer cust:custlist)    {    localDefaultCategoryDataset.addValue(cust.getSum(), "Series 1", cust.getCheck_out());    }    return localDefaultCategoryDataset;  }  /**   * 生成折线图(被调用)   * @param chartTitle 图的标题   * @param x          横轴标题   * @param y          纵轴标题   * @param dataset    数据集   * @return   */  private static JFreeChart createChart(String chartTitle, String x,       String y, CategoryDataset dataset)  {    // 构建一个chart   JFreeChart chart = ChartFactory.createLineChart(           chartTitle,//图的标题           x,  //x轴标题           y,  //y轴标题           dataset,//数据点           PlotOrientation.VERTICAL,           true,           true,           false);   //字体清晰   chart.setTextAntiAlias(false);   // 设置背景颜色   chart.setBackgroundPaint(Color.WHITE);   // 设置图标题的字体   Font font = new Font("隶书", Font.BOLD, 25);   chart.getTitle().setFont(font);   // 设置面板字体   Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);   // 设置图示的字体   chart.getLegend().setItemFont(labelFont);   CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();   // x轴 // 分类轴网格是否可见   categoryplot.setDomainGridlinesVisible(true);   // y轴 //数据轴网格是否可见   categoryplot.setRangeGridlinesVisible(true);   categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩   categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩   categoryplot.setBackgroundPaint(Color.lightGray);// 折线图的背景颜色   // 设置轴和面板之间的距离   // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));   // 横轴 x   CategoryAxis domainAxis = categoryplot.getDomainAxis();   domainAxis.setLabelFont(labelFont);// 轴标题   domainAxis.setTickLabelFont(labelFont);// 轴数值   // domainAxis.setLabelPaint(Color.BLUE);//轴标题的颜色   // domainAxis.setTickLabelPaint(Color.BLUE);//轴数值的颜色   // 横轴 lable 的位置 横轴上的 Lable 45度倾斜 DOWN_45   domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);   // 设置距离图片左端距离   domainAxis.setLowerMargin(0.0);   // 设置距离图片右端距离   domainAxis.setUpperMargin(0.0);   // 纵轴 y   NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();   numberaxis.setLabelFont(labelFont);   numberaxis.setTickLabelFont(labelFont);   numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());   numberaxis.setAutoRangeIncludesZero(true);   // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!   LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot           .getRenderer();   lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见   lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见   // 显示折点数据   lineandshaperenderer           .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());   lineandshaperenderer.setBaseItemLabelsVisible(true);   return chart;  }//获取JFreeChart  public static JFreeChart createDemoPanel()  { CategoryDataset database = createDataset();    JFreeChart localJFreeChart = createChart("每日收入","日期","营业额",database);    return localJFreeChart;  }
3.web.xml配置

<servlet>    <servlet-name>DisplayChart</servlet-name>    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>DisplayChart</servlet-name>    <url-pattern>/DisplayChart</url-pattern>  </servlet-mapping>
4.在action中

private String image;public String getImage() {return image;}public void setImage(String image) {this.image = image;}
public String getDayPayChart() {ServletActionContext.getRequest().getContextPath();// 保存图片 返回图片文件名String filename;try {filename = ServletUtilities.saveChartAsPNG(ChartOne.createDemoPanel(), 600, 400, null);// 获取图片路径(内存中)String graphURL = ServletActionContext.getRequest().getContextPath() + "/DisplayChart?filename="+ filename;System.out.println(graphURL);// 拼接<img src="  " />image = "<img src='" + graphURL+ "' width=600 height=400 border=0 usemap='#" + filename+ "'/>";} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "back_daychart";}
5.struts.xml配置

<package name="fcf" namespace="/" extends="base"><action name="pay_*" class="com.hotel.action.CustomerAction" method="{1}">   <result name="back_daychart">/frontPages/index.jsp</result></action></package>

6.页面

<!-- tasks --><div class="agile-last-grids"><div class="col-md-4 agile-last-left agile-last-middle"><div id="graph8"></div>${image }</div><div class="clearfix"> </div></div><!-- //tasks -->

7.效果:








原创粉丝点击