当前位置:   article > 正文

java通过AmCharts生成饼状图_amcharts饼图示例

amcharts饼图示例

1、创建数据库,创建表(订单表)

具体语句附在项目源码中。

2、创建web项目,搭建ssm环境(最后附上项目下载地址)

需求:生成每种房间类型的订单数量饼状图

效果展示:

具体实现,项目结构如下:

1、引入外部文件,在webapp目录下导入js和charts文件。编写各个表对象的实体类。

2、编写封装了饼状图信息的实体类

  1. package com.scce.pojo;
  2. /**
  3. * @program: IdeaProjects
  4. * @description:
  5. * @author: Lxy
  6. * @create: 2019-06-07 12:13
  7. **/
  8. //封装饼状图所需信息
  9. public class Roomchartwithbill {
  10. //房间类型
  11. private String roomtype;
  12. //订单数量
  13. private Integer billnumber;
  14. public String getRoomtype() {
  15. return roomtype;
  16. }
  17. public void setRoomtype(String roomtype) {
  18. this.roomtype = roomtype;
  19. }
  20. public Integer getBillnumber() {
  21. return billnumber;
  22. }
  23. public void setBillnumber(Integer billnumber) {
  24. this.billnumber = billnumber;
  25. }
  26. @Override
  27. public String toString() {
  28. return "Roomchartwithbill{" +
  29. "roomtype='" + roomtype + '\'' +
  30. ", billnumber=" + billnumber +
  31. '}';
  32. }
  33. }

3、编写dao层,BillDao

  1. package com.scce.dao;
  2. import org.apache.ibatis.annotations.Select;
  3. /**
  4. * @program: IdeaProjects
  5. * @description:
  6. * @author: Lxy
  7. * @create: 2019-06-02 21:36
  8. **/
  9. public interface BillDao {
  10. //根据房间类型,查询订单数量
  11. @Select("select count(1) from bill where roomType=#{roomType}")
  12. public Integer getBillNumByRoomType(Integer roomtype);
  13. }

4、Service层,BillService以及实现类

  1. package com.scce.service;
  2. import com.scce.pojo.Roomchartwithbill;
  3. import java.util.List;
  4. /**
  5. * @program: IdeaProjects
  6. * @description:
  7. * @author: Lxy
  8. * @create: 2019-06-03 21:16
  9. **/
  10. public interface BillService {
  11. //根据房间类型,查询订单数量
  12. public List<Roomchartwithbill> getBillNumByRoomType();
  13. }
  1. package com.scce.service.impl;
  2. import com.scce.dao.BillDao;
  3. import com.scce.dao.RoomTypeDao;
  4. import com.scce.pojo.RoomType;
  5. import com.scce.pojo.Roomchartwithbill;
  6. import com.scce.service.BillService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * @program: IdeaProjects
  14. * @description:
  15. * @author: Lxy
  16. * @create: 2019-06-03 21:17
  17. **/
  18. @Service
  19. @Transactional
  20. public class BillServiceImpl implements BillService {
  21. @Autowired
  22. private BillDao billDao;
  23. @Autowired
  24. private RoomTypeDao roomTypeDao;
  25. @Override
  26. public List<Roomchartwithbill> getBillNumByRoomType() {
  27. List<Roomchartwithbill> list = new ArrayList<Roomchartwithbill>();
  28. List<RoomType> roomTypeList = roomTypeDao.getRoomType();
  29. try {
  30. for (RoomType roomType : roomTypeList) {
  31. Integer num = billDao.getBillNumByRoomType(roomType.getRid());
  32. Roomchartwithbill roomchartwithbill = new Roomchartwithbill();
  33. roomchartwithbill.setBillnumber(num);
  34. roomchartwithbill.setRoomtype(roomType.getType());
  35. System.out.println(roomchartwithbill);
  36. list.add(roomchartwithbill);
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. return list;
  42. }
  43. }

5、controller层

  1. package com.scce.controller;
  2. import com.scce.pojo.Roomchartwithbill;
  3. import com.scce.service.BillService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.List;
  8. /**
  9. * @program: IdeaProjects
  10. * @description:
  11. * @author: Lxy
  12. * @create: 2019-06-04 00:06
  13. **/
  14. @RestController
  15. @RequestMapping("/bill")
  16. public class BillController {
  17. @Autowired
  18. private BillService billService;
  19. //饼状图 房间类型订单数量图
  20. @RequestMapping("/getBillNumByRoomType")
  21. public List<Roomchartwithbill> getBillNumByRoomType() {
  22. System.out.println("进入getBillNumByRoomType");
  23. List<Roomchartwithbill> list = billService.getBillNumByRoomType();
  24. return list;
  25. }
  26. }

6、页面代码:AmPieChart.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>饼状图</title>
  6. <link rel="stylesheet" href="../charts/style.css" type="text/css"/>
  7. <script type="text/javascript" src="../js/jquery-2.1.4.min.js"></script>
  8. <script type="text/javascript" src="../charts/amcharts.js"></script>
  9. <script type="text/javascript" src="../charts/pie.js"></script>
  10. <script type="text/javascript" src="../charts/serial.js"></script>
  11. </head>
  12. <script>
  13. //AmPieChart.html
  14. var chart1;
  15. var chartData1;
  16. AmCharts.ready(function () {
  17. $.getJSON("../bill/getBillNumByRoomType", null, function (data) {
  18. console.log(data);
  19. chart1 = new AmCharts.AmPieChart(); // 饼状图
  20. chart1.addTitle("房间类型订单数量图", 16); // 添加标题
  21. chart1.dataProvider = data; //指定数据来源,一般指向一个数组对象
  22. chart1.titleField = "roomtype"; //饼状每一块的标题
  23. chart1.valueField = "billnumber"; //饼状每一块的值
  24. chart1.sequencedAnimation = true; //指定动画应该被排序还是所有对象应该同时出现。
  25. chart1.startEffect = "elastic"; //动画效果。可能的值是:easeoutsin, easeinsin, elastic, bounce
  26. chart1.innerRadius = "30%";
  27. chart1.startDuration = 2; //Duration of the animation, in seconds.
  28. chart1.labelRadius = 15;
  29. chart1.balloonText = "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>";//节点显示的文本内容
  30. chart1.depth3D = 10; //设置为3d图像的厚度值
  31. chart1.angle = 15; //角度,当设置图像为3d图时使用该属性,默认为0
  32. chart1.write("chartdiv");
  33. });
  34. });
  35. </script>
  36. <body>
  37. <div id="chartdiv" style="height:600px;width:900px;float: left;"></div>
  38. </body>
  39. </html>

源码:https://github.com/LuoXuYang1997/AmCharts.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/114706
推荐阅读
相关标签
  

闽ICP备14008679号