SalesCountServlet.java
上传用户:lsj999sz
上传日期:2022-06-15
资源大小:4717k
文件大小:2k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. package com.bjsxt.shopping.stat;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.List;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.jfree.chart.ChartFactory;
  10. import org.jfree.chart.ChartUtilities;
  11. import org.jfree.chart.JFreeChart;
  12. import org.jfree.chart.plot.PlotOrientation;
  13. import org.jfree.data.category.CategoryDataset;
  14. import org.jfree.data.category.DefaultCategoryDataset;
  15. import com.bjsxt.shopping.util.PropertyMgr;
  16. public class SalesCountServlet extends HttpServlet {
  17. @Override
  18. protected void doGet(HttpServletRequest request,
  19. HttpServletResponse response) throws ServletException, IOException {
  20. System.out.println("ok");
  21. CategoryDataset dataset = getDataSet();
  22. String fileName = "SalesCount.jpg";
  23. JFreeChart chart = ChartFactory.createBarChart3D("产品销量图", // 图表标题
  24. "产品", // 目录轴的显示标签
  25. "销量", // 数值轴的显示标签
  26. dataset, // 数据集
  27. PlotOrientation.VERTICAL, // 图表方向:水平、垂直
  28. true, // 是否显示图例(对于简单的柱状图必须是false)
  29. false, // 是否生成工具
  30. false // 是否生成URL链接
  31. );
  32. FileOutputStream fos_jpg = null;
  33. try {
  34. fos_jpg = new FileOutputStream(
  35. PropertyMgr.getProperty("statImagePath") + fileName);
  36. System.out.println(PropertyMgr.getProperty("statImagePath"));
  37. ChartUtilities.writeChartAsJPEG(fos_jpg, 0.5f, chart, 400, 300,
  38. null);
  39. } finally {
  40. try {
  41. fos_jpg.close();
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. request.setAttribute("imgName", fileName);
  47. this.getServletContext().getRequestDispatcher("/admin/SalesCount.jsp")
  48. .forward(request, response);
  49. }
  50. private CategoryDataset getDataSet() {
  51. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  52. List<ProductStatItem> items = StatService.getProductsBySaleCount();
  53. for (int i = 0; i < items.size(); i++) {
  54. ProductStatItem p = items.get(i);
  55. dataset.addValue(p.getTotalSalesCount(), "销量", p.getProductName());
  56. }
  57. return dataset;
  58. }
  59. }