Echarts后台获取数据

记录一下,毕竟不是专业搞前端的

后台代码

@Controller
public class EchartsDemo {

   @ResponseBody
    @RequestMapping("/hello")
    public Map hello(){
        Map map = new HashMap();
        List list = new ArrayList();
        list.add("哈哈");
        list.add("与配偶同住");
        list.add("与子女同住");
        list.add("仅与重度残疾子女共同居住");
        list.add("与配偶及子女同住");
        list.add("与其他亲属同住");
        list.add("与父母同住");
        list.add("与其他人同住");
        List<EchartsEntity> EchartsEntitys = new ArrayList<>();
        EchartsEntitys.add(new EchartsEntity("1001","哈哈"));
        EchartsEntitys.add(new EchartsEntity("1020","与配偶同住"));
        EchartsEntitys.add(new EchartsEntity("1300","与子女同住"));
        EchartsEntitys.add(new EchartsEntity("1100","仅与重度残疾子女共同居住"));
        EchartsEntitys.add(new EchartsEntity("1300","与配偶及子女同住"));
        EchartsEntitys.add(new EchartsEntity("1040","与父母同住"));
        EchartsEntitys.add(new EchartsEntity("10","与其他人同住"));
        map.put("EchartsEntitys",EchartsEntitys);
        map.put("str",list);
       return map ;
    }

}

页面代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
    <!-- 这里是加载刚下好的echarts.min.js,注意路径 -->
    <script src="../static/eCharts/js/echarts.min.js" th:src="@{/static/eCharts/js/echarts.min.js}"></script>
    <script src="../static/eCharts/js/jquery-1.11.0.min.js" th:src="@{/static/eCharts/js/jquery-1.11.0.min.js}"></script>
</head>
<body>

    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 800px;height:400px;"></div>

    <script type="text/javascript">

    $(function () {
        $.ajax({
            type : "get",//向后台请求的方式,有post,get两种方法
            url : "/hello",//url填写的是请求的路径
            cache : false,//缓存是否打开
            data : {

            },
            dataType : 'json',//请求的数据类型
            success : function(data) {//请求的返回成功的方法
                if (data!=null ) {
                  var  data1 = data.str;
                  var data2 = data.EchartsEntitys;
                    haha(data1,data2);
                } else {
                    alert("加载失败");
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {//请求的失败的返回的方法
                alert("小伙子,出异常了");
            }
        });

        function haha (data1,data2) {
            option = {
                backgroundColor: "#031845",
                color: ['#2edfa3', '#bce672', '#ff4777', '#70f3ff', '#4b5cc4', '#f47983', '#8d4bbb', '#6635EF', '#FFAFDA'],
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b}: {c} ({d}%)"
                },
                legend: {
                    orient: 'horizontal',
                    icon: 'circle',
                    bottom: 20,
                    x: 'center',
                    textStyle: {
                        color: '#fff'
                    },
                    data:   data1
                },

                series: [{
                    name: '访问来源',
                    type: 'pie',
                    selectedMode: 'single',
                    radius: [0, '38%'],

                    label: {
                        normal: {
                            show: false,
                            position: 'inner',
                            formatter: '{d}%',
                            textStyle: {
                                color: '#fff',
                                fontWeight: 'normal',
                                fontSize: 10
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:  data2
                },
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: ['40%', '42%'],
                        label: {
                            normal: {
                                formatter: '{b|{b}}\n{hr|}\n{d|{d}%}',
                                rich: {
                                    b: {
                                        fontSize: 10,
                                        color: '#fff',
                                        align: 'left',
                                        padding: 4
                                    },
                                    hr: {
                                        borderColor: '#12EABE',
                                        width: '100%',
                                        borderWidth: 2,
                                        height: 0
                                    },
                                    d: {
                                        fontSize: 10,
                                        color: '#fff',
                                        align: 'left',
                                        padding: 4
                                    },
                                    c: {
                                        fontSize: 10,
                                        color: '#fff',
                                        align: 'center',
                                        padding: 4
                                    }
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: true,
                                length: 20,
                                length2: 20,
                                lineStyle: {
                                    type: 'dashed',
                                    width: 2
                                }
                            }
                        },
                        data: data2
                    }
                ]
            };

            // 基于准备好的dom,初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }

    })


    </script>
</body>
</html>

效果显示

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在echarts饼图中后台获取数据,可以使用Ajax技术向后台发送请求,获取数据后再进行图表的渲染。具体步骤如下: 1. 在前端页面中引入echarts库和jQuery库。 2. 在页面中创建一个div元素,用于显示饼图。 3. 使用Ajax向后台发送请求,获取数据。 4. 在Ajax的回调函数中,解析后台返回的数据,并将数据转换为echarts所需的格式。 5. 使用echarts库中的API,将数据渲染成饼图,并显示在页面中。 以下是一个示例代码: ```javascript // 引入echarts库和jQuery库 <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> // 创建一个div元素,用于显示饼图 <div id="pieChart" style="width: 600px;height:400px;"></div> // 使用Ajax向后台发送请求,获取数据 $.ajax({ url: 'data.php', // 后台接口地址 type: 'get', dataType: 'json', success: function (res) { // 解析后台返回的数据,并将数据转换为echarts所需的格式 var data = []; for (var i = 0; i < res.length; i++) { data.push({ name: res[i].name, value: res[i].value }); } // 使用echarts库中的API,将数据渲染成饼图,并显示在页面中 var myChart = echarts.init(document.getElementById('pieChart')); var option = { title: { text: '饼图示例' }, series: [{ name: '访问来源', type: 'pie', radius: '55%', data: data }] }; myChart.setOption(option); } }); // 相关问题:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值