echarts如何画柱状图,以及参数解释

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery1.3.2.min.js"></script>
    <script src="echarts.js"></script>
</head>
<body>
<div id="product1" style="width: 20%;height: 300px;"></div><!--柱状图容器-->
<script>
    $(function(){
        var datalist = [];
        $.ajax({
            type:'GET',
            url:'test.json',
            dataType:'json',
            success:function(res){
                datalist = res;
                //产品的名称、数量、单价分别提取出来作为三个新数组
                var namelist = [];
                var countlist = [];
                var pricelist = [];
                $.each(datalist,function(index,val){
                    namelist.push(val.name);
                    countlist.push(val.counts);
                    pricelist.push(val.price);
                });
                //以上都是在获取绘制图形所需要的数据

                /*
                 * 柱状图
                 */

                // 基于准备好的dom,初始化echarts实例
                var productChart1 = echarts.init(document.getElementById('product1'));

                // 指定图表的配置项和数据
                var option1 = {
                    title: {
                        text: 'product -- 柱状图',
                        x:'left',
                        y:'top',
                        textStyle:{fontSize:14}
                    },
                    tooltip: {},
                    legend: {            //顶部提示分类、对应颜色及提示位置
                        data:['数量','单价'],
                        x: 'right', // 'center' | 'left' | {number}
                        y: 'top', // 'center' | 'bottom' | {number
                    },
                    xAxis: {
                        data: namelist
                    },
                    yAxis: {},
                    color:['#cfc','#fcc'],
                    series: [
                        {
                            name: '数量',
                            type: 'bar',
                            label: {      //每个柱子顶端显示数值
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            data: countlist
                        },
                        {
                            name: '单价',
                            type: 'bar',
                            label: {      //每个柱子顶端显示数值
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            data: pricelist
                        }
                    ],

                };
                // 使用刚指定的配置项和数据显示图表。
                productChart1.setOption(option1);
                window.onresize = function () {
                    productChart1.resize(); //使图表适应
                };
            }
        });
    })
</script>
</body>
</html>

json文件:

[{
  "name": "apple",
  "counts": "12",
  "price": "10"
},
  {
    "name": "banana",
    "counts": "20",
    "price": "20"
  },
  {
    "name": "age",
    "counts": "69",
    "price": "17"
  },
  {
    "name": "pencil",
    "counts": "100",
    "price": "6"
  },
  {
    "name": "rice",
    "counts": "52",
    "price": "8"
  }
]

绘制结果:

 

如果对哪个参数的解释不是很理解,那就自己试着去修改一下这个参数,然后就get了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值