echarts 柱形

在这里插入图片描述

上图是最终实现的效果,话不多说,直接上代码记录下

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>渐变柱状</title>
		<!-- 引入 echarts.js -->
		<script src="./js/echarts4.60.min.js" type="text/javascript"></script>
		<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    </head>
    <style>
        body{
            background-color: #00062E;
        }
    </style>

	<body>
		<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
		<div id="main" style="width: 600px;height:400px;"></div>
		<script type="text/javascript">
			// 基于准备好的dom,初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));
            var data =  [50, 200, 400, 600];
            var sum = 0;
            for(var i = 0;i < data.length;i++){
                sum += data[i];
            }
           // 指定图表的配置项和数据
			myChart.setOption({
				title: {
					// text: '柱状图渐变'
				},
				tooltip: {},
				legend: {
					// data: ['销量']
				},
				xAxis: {
                    data: ["上年同期", "本年计划", "本年实际"],
                    axisTick: {
                        alignWithLabel: true,
                        show: false // 纵轴
                    },
                    axisLine: { // 横轴
                        show: true,
                        lineStyle: {
                            color:'#212A51'
                        }
                    }, 
                    axisLabel: {
                        textStyle: {
                            show:true,
                            fontFamily:'微软雅黑',
                            color: "#fff",
                            fontSize: '14',
                        },                           
                    }
				},
				yAxis: {
                   
                    type: 'value',
                    splitLine: {
                        show: true,
                        lineStyle: {
                            type: 'dashed',
                            color:["#20294F"],
                            width:2.5,
                        }
                    },
                    axisLabel: {
                        textStyle: {
                            show:true,
                            fontFamily:'微软雅黑',
                            color: "#fff",
                            fontSize: '12',
                        },                           
                    },
                    axisTick: {
                        alignWithLabel: false,
                        show: false 
                    },
                    axisLine: {
                        show: false
                    },
                },
				series: [{
					// name: '销量',
                    type: 'bar',
                    barWidth:20,
  	                barGap:'80%',/*多个并排柱子设置柱子之间的间距*/
  	                barCategoryGap:'50%',/*多个并排柱子设置柱子之间的间距*/
					itemStyle: {
						normal: {    
                            barBorderRadius:[15, 15, 0, 0],柱形图圆角,初始化效果
                            width:5,
                            label: {
                                show: true, //开启显示
                                position: 'top', //在上方显示
                                textStyle: { //数值样式
                                    color: '#FFCA4A',
                                    fontSize: 12
                                },
                                distance:20,
                                formatter: function(params) {//核心部分 formatter 可以为字符串也可以是回调 
                              if(params.value){//如果当前值存在则拼接
                                return params.value + '万元' 
                              }else{//否则返回个空
                                return '';
                              }
                            },
                            },
                            color: { //图形渐变颜色方法,四个数字分别代表,右,下,左,上,offset表示0%到100%
                                    type: 'linear',
                                    x: 0,
                                    y: 1,
                                    x2: 0, //从左到右 0-1
                                    y2: 0,
                                    colorStops: [
                                        {
                                            offset: 0,
                                            color: "#FFCA4A" // 0% 处的颜色
                                        },
                                        {
                                            offset: 0.2,
                                            color: "#FFCA4A" // 60% 处的颜色
                                        }, 
                                        {
                                            offset: 0.4,
                                            color: "#FFCA4A" // 60% 处的颜色
                                        }, 
                                        {
                                            offset: 0.6,
                                            color: "#FFCA4A" // 60% 处的颜色
                                        },
                                        {
                                            offset: 0.8,
                                            color: "#88DDA2" // 60% 处的颜色
                                        },
                                        {
                                            offset: 1,
                                            color: "#30EBE2" // 100% 处的颜色
                                        }
                                    ],
                                },
							// color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                            //     {
                            //         offset: 0,
                            //         color: "#FFCA4A" // 0% 处的颜色
                            //     },
                            //     {
                            //         offset: 0.2,
                            //         color: "#FFCA4A" // 60% 处的颜色
                            //     }, 
                            //     {
                            //         offset: 0.4,
                            //         color: "#FFCA4A" // 60% 处的颜色
                            //     }, 
                            //     {
                            //         offset: 0.6,
                            //         color: "#FFCA4A" // 60% 处的颜色
                            //     },
                            //     {
                            //         offset: 0.8,
                            //         color: "#88DDA2" // 60% 处的颜色
                            //     },
                            //     {
                            //         offset: 1,
                            //         color: "#30EBE2" // 100% 处的颜色
                            //     }
                            // ], false)
                        }, 
                    },
                    data: data,
                    markPoint: {
                        symbol:'arrow',
                        // symbol:'image://http://echarts.baidu.com/images/favicon.png',
                        itemStyle:{
                            normal:{
                                label:{
                                    show:true,
                                    color:'#fff',
                                },
                                color:'#00F5F0',
                            }
                        },
                        symbolSize:[8, 8],
                        symbolOffset:[0,-10],
                        data:[
                            {name:'上年同期',coord:[0,50]},
                            {name:'本年计划',coord:[1,200]},
                            {name:'本年实际',coord:[2,400]},
                        ],
                    },
                    markLine : {
                        data : [
                            {
                                yAxis: 400,
                                name: '标杆水平',
                                lineStyle: {
                                    type: 'solid',
                                    color: '#C14833'
                                },
                                label:{
                                    show:true,
                                    position:'end',
                                    // formatter:"平均值 : "+sum/data.length,
                                    formatter:"标杆水平",
                                }
                            },
                            // {
                            // yAxis: 100,
                            // name: '上限',
                            // lineStyle: {
                            //     type: 'dashed',
                            //     color: '#b17063'
                            // },
                             {
                                type :'average', 
                                name: '平均值',
                                label:{
                                    show:true,
                                    position:'end',
                                    // formatter:"平均值 : "+sum/data.length,
                                    formatter:"平均水平",
                                }
                            },
                       ],
                        symbol: ['none', 'none'],
                        position:"insideTopCenter",
                        itemStyle:{
                            normal:{
                                lineStyle:{
                                    type:'solid',
                                    color:'#CCA607'
                                },
                                // label:{
                                //     sh   ow:true,
                                //     position:'end',
                                //     // formatter:"平均值 : "+sum/data.length,
                                //     // formatter:"平均水平",
                                // }
                            }
                        },
                        large:true,
                        effect:{
                            show: false,
                            loop: true,
                            period: 0,
                            scaleSize : 2,
                            color : null,
                            shadowColor : null,
                            shadowBlur : null
                        },
                    },         
                    // markLine: {
                    //     data: [
                    //         {type: 'average', name: '平均值'}
                    //     ]
                    // } 
				}]
			});
		</script>
	</body>
</html>
### ECharts 柱形图动画效果实现方法 #### 初始化ECharts实例并设置基础配置项 为了创建带有动画效果的柱形图,首先需要初始化ECharts实例,并指定DOM容器。通过`setOption()`函数来定义图表的具体样式和数据。 ```javascript var myChart = echarts.init(document.getElementById('main')); myChart.setOption({ title: { text: '动态加载示例' }, tooltip: {}, xAxis: { data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20], animationDuration: 2800, animationEasing: "quadraticOut" }] }); ``` 上述代码展示了如何构建一个简单的柱状图,并为其应用了渐入式的动画效果[^3]。 #### 添加自定义动画属性 对于更复杂的动画需求,可以在`series`对象内增加特定参数来自定义动画行为: - `animation`: 是否开启动画,默认开启。 - `animationThreshold`: 开启动画的数据量阈值,在此数量以上关闭所有图形的过渡动画优化性能表现。 - `animationDuration`: 初始动画持续时间;可以是一个固定的毫秒数或是回调函数返回不同系列的不同数值。 - `animationEasing`: 定义初始动画缓动方式,支持更多种类的选择如线性、弹性等。 - `animationDelay`: 设置延迟执行的时间间隔,同样接受固定值或计算逻辑作为输入。 例如,要让柱体逐渐增长而不是瞬间显示出来,则可以通过调整`animationDuration`以及选择合适的`animationEasing`曲线达成理想的效果。 #### 特殊视觉特效——闪烁效果 如果希望给某些特殊情况下突出显示某根柱条,还可以利用额外插件或者自定义渲染器添加特殊的视觉反馈机制。比如下面的例子实现了当鼠标悬停时触发短暂的闪光提示功能[^2]: ```javascript itemStyle:{ emphasis : { shadowBlur: 10, shadowColor: '#fff', borderWidth: 0, borderColor: '#fff' } } ``` 这里并没有直接使用官方文档提到过的`twinkle`选项而是采用了阴影叠加的方式来模拟类似的视觉感受。实际开发过程中可以根据具体场景灵活运用多种手段组合创造出更加丰富的交互体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值