Echart柱状图和饼状图

用文字记录自己的程序人生
有时候再项目中会应用到一些前端的知识,为了避免以后找不到这些知识,特此记录一下。

前端:

//简化代码
<style>
	.pillar{
            width: 50%;
            height: 430px;
            display: inline-block;
        }
     .pie{
         width: 50%;
         height: 430px;
         display: inline-block;
     }
</style>
<div>
<div class="pillar" id="div_pillar"></div>
<div class="pie" id="div_pie"></div>
<div>

引入echarts.js

<script type="text/javascript" src="../js/echarts/echarts.js"></script>
<script type="text/javascript">
	var pillar= document.getElementById("div_pillar");
    var myChart = echarts.init(pillar);
    var pie = document.getElementById("div_pie");
    var myChartPie = echarts.init(pie);
    //定义不同的颜色
    const colors = ['#CA8622','#61A0A8', '#BDA29A', '#D48265', '#6E7074','#749F83'];
    $(function(){
    	init_pillar();
    	init_pie();
    });
    function init_pillar(){
        var option = {
            color: colors,
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                type: 'category',
                data: ['直达','营销', '搜索', '邮件','联盟','视频']
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: [10, 20, 30, 40, 50, 60],
                type: 'bar',
                barWidth: '40',
                showBackground: false,

                label: {
                    show: true,
                    position: 'top'
                },
                itemStyle:{
                    normal:{
                        color: function(params){
                            return colors[params.dataIndex];
                        }
                    }
                }
            }]
        };
        myChart.setOption(option);
    }
    function init_pie(){
    	var option = {
            tooltip: {
                trigger: 'item',
                formatter: '{a} <br/>{b} : {c} ({d}%)'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                data: ['直达','营销', '搜索', '邮件','联盟','视频']
            },
            series: [
                {
                    name: '访问来源',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '60%'],
                    data: [
                        {value: 10, name: '直达',itemStyle: {color:'#CA8622'}},
                        {value: 20, name: '营销',  itemStyle: {color:'#61A0A8'}},
                        {value: 30, name: '搜索',itemStyle: {color:'#BDA29A'}},
                        {value: 40, name: '邮件',  itemStyle: {color:'#D48265'}},
                        {value: 50, name: '联盟',  itemStyle: {color:'#6E7074'}},
                        {value: 60, name: '视频',  itemStyle: {color:'#749F83'}}
                    ],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        myChartPie.setOption(option);
    }
</script>

动态填充数据
需要去请求数据库,返回的数据弄成数组的格式,自己放进去就好了,这里就不一一写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值