经常会见到小伙伴做出来的柱形图带有动态,闪烁的效果,小编本着好学好问(工作要求)的精神,终于找到了方法。下面就跟小编一起来看如何用echarts实现柱形图的闪烁效果吧。
1.echarts安装
npm install echarts
2.在node_modules中找到echarts依赖包
3.打开src->chart->bar->BarView.js
function updateStyle( el, data, dataIndex, itemModel, layout, seriesModel, isHorizontal, isPolar ) { var color = data.getItemVisual(dataIndex, 'color'); var opacity = data.getItemVisual(dataIndex, 'opacity'); var stroke = data.getVisual('borderColor'); var itemStyleModel = itemModel.getModel('itemStyle'); //声明一个变量 var twinkle = itemModel.getModel('emphasis.itemStyle.twinkle'); var hoverStyle = itemModel.getModel('emphasis.itemStyle').getBarItemStyle(); if (!isPolar) { el.setShape('r', itemStyleModel.get('barBorderRadius') || 0); } el.useStyle(zrUtil.defaults( { stroke: isZeroOnPolar(layout) ? 'none' : stroke, fill: isZeroOnPolar(layout) ? 'none' : color, opacity: opacity }, itemStyleModel.getBarItemStyle() )); var cursorStyle = itemModel.getShallow('cursor'); cursorStyle && el.attr('cursor', cursorStyle); var labelPositionOutside = isHorizontal ? (layout.height > 0 ? 'bottom' : 'top') : (layout.width > 0 ? 'left' : 'right'); if (!isPolar) { setLabel( el.style, hoverStyle, itemModel, color, seriesModel, dataIndex, labelPositionOutside ); } if (isZeroOnPolar(layout)) { hoverStyle.fill = hoverStyle.stroke = 'none'; } //使用动画animate函数,用js传参控制柱状图特效 if(twinkle){ el.animate('style', true) when(500, { borderWidth:10, borderColor:"red", barBorderRadius:5, lineWidth:7.5, opacity:0.4 }) .when(1500, { lineWidth:0, borderColor:"red", barBorderRadius:5, opacity:0.25 }) .start(); } graphic.setHoverStyle(el, hoverStyle); }
4.打开lib->chart->bar->BarView.js,做跟上面一样的修改
5.components/echarts/bar.vue文件:
<template> <div style="height:500px" ref="echart" id="bar">柱状图</div> </template> <script> export default { name: "bar", data() { return { option: null }; }, methods: { draw() { let myChart = this.$echarts.init(document.getElementById("bar")); this.option = { title: { text: "成交量", left: 10 }, xAxis: { type: "category", data: [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "151", "16", "17", "18", "19", "20" ] }, yAxis: { type: "value", min: 0, max: 100, splitNumber: 2 }, series: [ { data: [120, 200, 150, 80, 70, 110, 130], type: "bar", itemStyle: { color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 1, [ { offset: 0, color: "rgba(159,230,184,0.7)" }, { offset: 0.5, color: "rgb(141,193,169)" }, { offset: 1, color: "rgb(115,163,115)" } ]), barBorderRadius: 5 }, label: { show: true, position: "top", textStyle: { color: "black", fontSize: 16 } }, emphasis: { itemStyle: { //颜色渐变函数LinearGradient,4和参数分别表示四个位置依次为左、下、右、上 color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 1, [ { offset: 0, color: "rgba(221,107,102,0.7)" }, { offset: 0.8, color: "rgb(238,64,61)" }, { offset: 1, color: "rgb(186,39,38)" } ]), barBorderRadius: 5, borderColor: new this.$echarts.graphic.LinearGradient( 0, 0, 1, 1, [ { offset: 0, color: "rgba(221,107,102,0.7)" }, { offset: 0.8, color: "rgb(238,64,61)" }, { offset: 1, color: "rgb(186,39,38)" } ] ), borderWidth: 3, lineWidth: 2, twinkle: { //闪烁 enabled: true, //启用 period: 2000 //动画时间 } }, label: { show: true } } } ] }; let _self = this; setInterval(function() { _self.option.series[0].data = _self.$options.methods.getArrRandomly(20); console.log(_self.option.series[0].data); _self.option.series[0].data.forEach((item, curidx) => { //取消之前的高亮 myChart.dispatchAction({ type: "downplay", seriesIndex: 0, dataIndex: curidx }); if (item > 50) { myChart.dispatchAction({ type: "highlight", dataIndex: curidx }); } }); myChart.setOption(_self.option, true); }, 2000); window.addEventListener("resize", function() { myChart.resize(); }); }, getArrRandomly(len) { let arr = []; for (let i = len - 1; i >= 0; i--) { let itemIndex = Math.floor(Math.random() * 100); arr[i] = itemIndex; } return arr; } }, mounted() { this.draw(); } }; </script> <style scoped></style>来看一下最后的效果:
怎么样,是不是效果很酷炫,动态效果的柱形图更能吸引人的眼球,大家一起用echarts实际操作一下吧!
数据分析咨询请扫描二维码
大数据分析师证书 针对不同知识,掌握程度的要求分为【领会】、【熟知】、【应用】三个级别,考生应按照不同知识要求进行学习。 ...
2024-10-29拥抱数据分析的世界 - 成为一名数据分析工程师是一个充满挑战和机遇的职业选择。要成功地进入这个领域,你需要掌握一系列关键技 ...
2024-10-28降本增效:管理战略的关键 企业管理中的降本增效不仅是一项重要的战略举措,更是激发竞争力、提高盈利能力的关键。这一理念在当 ...
2024-10-28企业数字化是指利用数字技术和信息化手段,对企业的各个方面进行改造和优化,以提升生产效率、服务质量和市场竞争力的过程。实现 ...
2024-10-28数据科学专业毕业后,毕业生可以选择从事多种不同的岗位和领域。数据科学是一个快速发展且广泛应用的领域,毕业生在企业、学术界 ...
2024-10-28学习数据科学与大数据技术是当今职业发展中至关重要的一环。从基础到高级,以下是一些建议的课程路径: 基础课程: Python编程 ...
2024-10-28在信息技术和数据科学领域,数据架构师扮演着至关重要的角色。他们负责设计和管理企业中复杂的数据基础设施,以支持数据驱动的决 ...
2024-10-28进入21世纪以来,随着信息技术的迅猛发展,大数据已经成为全球最具影响力的技术之一,并成为企业数字化转型的核心驱动力。大数据 ...
2024-10-28随着科技的迅猛发展,数字化转型已成为现代企业保持竞争力和推动增长的关键战略之一。数字化不仅仅是技术的应用,它代表着一种全 ...
2024-10-28银行业正处于一个前所未有的数字化转型时期。在数字经济的驱动下,金融科技如大数据、人工智能、生物识别、物联网和云计算等技术 ...
2024-10-28数据分析可视化是一门艺术与科学相结合的技术,其主要目标是将复杂的数据变得更易于理解和分析。通过将数据以图表的形式呈现,我 ...
2024-10-28数据分析师在现代信息密集型的商业世界中扮演着至关重要的角色。他们通过专业的技能和敏锐的商业洞察力,帮助企业从大量数据中提 ...
2024-10-28在当今快速发展的数据驱动世界中,数据专员的角色变得愈发重要。无论是在企业决策、市场分析还是产品开发中,数据专员都扮演着不 ...
2024-10-27在当今迅速发展的科技时代,数字化对企业的意义无比深远。它不仅提升了企业的竞争力和运营效率,还显著改善了客户体验,推动了企 ...
2024-10-27企业数字化转型是一个全方位的变革过程,旨在通过应用新兴数字技术,重新设计企业的业务流程、组织结构、产品和服务,以在竞争激 ...
2024-10-27数据挖掘是一种集成了统计学、人工智能和机器学习等多种技术的过程,其主要目标是从大量数据中提取有价值的信息和知识。通过分析 ...
2024-10-27数字经济是一种新型的经济形态,以数字技术为基础,通过数据的获取、存储、加工、传输和应用进行经济发展。其核心在于利用数字化 ...
2024-10-27数据科学无疑是现代数字化社会的中流砥柱。随着大数据和人工智能技术的持续飞跃,各行各业对具备数据分析和管理能力的人才需求呈 ...
2024-10-25在当今快速发展的商业环境中,数字化转型已经成为企业保持竞争力和促进业务增长的必然选择。数字化转型不仅意味着技术的变革,更 ...
2024-10-25在当今数据驱动的商业环境中,数据分析已经成为企业决策过程中的核心要素。企业需要处理海量数据,从中提炼出有价值的见解,以支 ...
2024-10-25