java通过AmCharts生成饼状图

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

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

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

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

效果展示:

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

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

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

package com.scce.pojo;

/**
 * @program: IdeaProjects
 * @description:
 * @author: Lxy
 * @create: 2019-06-07 12:13
 **/
//封装饼状图所需信息
public class Roomchartwithbill {
    //房间类型
    private String roomtype;
    //订单数量
    private Integer billnumber;

    public String getRoomtype() {
        return roomtype;
    }

    public void setRoomtype(String roomtype) {
        this.roomtype = roomtype;
    }

    public Integer getBillnumber() {
        return billnumber;
    }

    public void setBillnumber(Integer billnumber) {
        this.billnumber = billnumber;
    }

    @Override
    public String toString() {
        return "Roomchartwithbill{" +
                "roomtype='" + roomtype + '\'' +
                ", billnumber=" + billnumber +
                '}';
    }
}

3、编写dao层,BillDao

package com.scce.dao;

import org.apache.ibatis.annotations.Select;

/**
 * @program: IdeaProjects
 * @description:
 * @author: Lxy
 * @create: 2019-06-02 21:36
 **/
public interface BillDao {

    //根据房间类型,查询订单数量
    @Select("select count(1) from bill where roomType=#{roomType}")
    public Integer getBillNumByRoomType(Integer roomtype);
    
}

4、Service层,BillService以及实现类

package com.scce.service;

import com.scce.pojo.Roomchartwithbill;

import java.util.List;

/**
 * @program: IdeaProjects
 * @description:
 * @author: Lxy
 * @create: 2019-06-03 21:16
 **/
public interface BillService {

    //根据房间类型,查询订单数量
    public List<Roomchartwithbill> getBillNumByRoomType();

}
package com.scce.service.impl;

import com.scce.dao.BillDao;
import com.scce.dao.RoomTypeDao;
import com.scce.pojo.RoomType;
import com.scce.pojo.Roomchartwithbill;
import com.scce.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: IdeaProjects
 * @description:
 * @author: Lxy
 * @create: 2019-06-03 21:17
 **/
@Service
@Transactional
public class BillServiceImpl implements BillService {

    @Autowired
    private BillDao billDao;

    @Autowired
    private RoomTypeDao roomTypeDao;
    
    @Override
    public List<Roomchartwithbill> getBillNumByRoomType() {
        List<Roomchartwithbill> list = new ArrayList<Roomchartwithbill>();
        List<RoomType> roomTypeList = roomTypeDao.getRoomType();
        try {
            for (RoomType roomType : roomTypeList) {
                Integer num = billDao.getBillNumByRoomType(roomType.getRid());
                Roomchartwithbill roomchartwithbill = new Roomchartwithbill();
                roomchartwithbill.setBillnumber(num);
                roomchartwithbill.setRoomtype(roomType.getType());
                System.out.println(roomchartwithbill);
                list.add(roomchartwithbill);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
}

5、controller层

package com.scce.controller;

import com.scce.pojo.Roomchartwithbill;
import com.scce.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: IdeaProjects
 * @description:
 * @author: Lxy
 * @create: 2019-06-04 00:06
 **/
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;
    
    //饼状图 房间类型订单数量图
    @RequestMapping("/getBillNumByRoomType")
    public List<Roomchartwithbill> getBillNumByRoomType() {
        System.out.println("进入getBillNumByRoomType");
        List<Roomchartwithbill> list = billService.getBillNumByRoomType();
        return list;
    }
    
}

6、页面代码:AmPieChart.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>饼状图</title>
    <link rel="stylesheet" href="../charts/style.css" type="text/css"/>
    <script type="text/javascript" src="../js/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="../charts/amcharts.js"></script>
    <script type="text/javascript" src="../charts/pie.js"></script>
    <script type="text/javascript" src="../charts/serial.js"></script>
</head>
<script>
    //AmPieChart.html

    var chart1;
    var chartData1;
    AmCharts.ready(function () {
        $.getJSON("../bill/getBillNumByRoomType", null, function (data) {
            console.log(data);

            chart1 = new AmCharts.AmPieChart();  // 饼状图

            chart1.addTitle("房间类型订单数量图", 16);  // 添加标题
            chart1.dataProvider = data; //指定数据来源,一般指向一个数组对象
            chart1.titleField = "roomtype"; //饼状每一块的标题
            chart1.valueField = "billnumber"; //饼状每一块的值
            chart1.sequencedAnimation = true;  //指定动画应该被排序还是所有对象应该同时出现。
            chart1.startEffect = "elastic";   //动画效果。可能的值是:easeoutsin, easeinsin, elastic, bounce
            chart1.innerRadius = "30%";
            chart1.startDuration = 2;  //Duration of the animation, in seconds.
            chart1.labelRadius = 15;
            chart1.balloonText = "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>";//节点显示的文本内容
            chart1.depth3D = 10; //设置为3d图像的厚度值
            chart1.angle = 15;  //角度,当设置图像为3d图时使用该属性,默认为0
            chart1.write("chartdiv");
        });
    });
</script>
<body>
<div id="chartdiv" style="height:600px;width:900px;float: left;"></div>
</body>
</html>

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

xml <!-- [xml] (xml / csv) 数据类型xml/csv--> ; <!-- 如果使用csv作为数据的话,需要使用这个属性;表示文件数据分隔符,(平常以";"和","为主) [;] (string) csv file data separator (you need it only if you are using csv file for your data) --> 1 <!-- 如果使用的是csv数据,可以设置跳过几行再显示数据,默认为0表示csv中的数据全部显示,大于n(n>0);表示前面n行都不显示[0] (Number) if you are using csv data type, you can set the number of rows which should be skipped here --> <!-- 设置系统中的字体[Arial] (font name) use device fonts, such as Arial, Times New Roman, Tahoma, Verdana... --> <!-- 设置所有文本的大小,默认为11,具体的文本的字体大小也可以在下面的设置中设置[11] (Number) text size of all texts. Every text size can be set individually in the settings below --> <!-- 同上[#000000] (hex color code) main text color. Every text color can be set individually in the settings below--> . <!-- 小数分隔符,默认为[,]注:该属性只是用来显示,而在csv数据文件中,必须使用[.] (string) decimal separator. Note, that this is for displaying data only. Decimals in data xml file must be separated with a dot --> <!-- 千位分隔符,默认为空[ ] (string) thousand separator. use "none" if you don't want to separate --> 3 <!-- 如果百分数格式的数字,后面的小数位小于该属性的值,则在小数后面加0补充。如54.2%,该属性设置为3,那么显示的效果为54.200%。[] (Number) if your value has less digits after decimal then is set here, zeroes will be added --> <!--设置科学记数法的最小值 [0.000001] If absolute value of your number is equal or less then scientific_min, this number will be form
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值