收款定制开发怎么让 echarts 图表动起来?定时器解决它 —— 大屏展示案例(动态仪表盘、动态柱状图)

        收款定制开发该案例为了实现效果采收款定制开发用的是随机生成数据,收款定制开发比较适用于偏向展示效收款定制开发果的静态页面如门户网站的首页、收款定制开发登录页等等。收款定制开发颜色样式自调。

        收款定制开发需要注意在有些项目中收款定制开发仪表盘可能无法正常显示,收款定制开发这是因为你在项目中引入的 echarts 版本太低,需要引入新版本 echarts5。


目录


一、案例效果

做案例之前正常引入 echarts 图表,echarts 依赖包的下载和安装此处省略,详情可参见文章:

二、实现步骤

1.创建页面结构 

        两个带有 id 名的容器,样式自定。 

  1. <template>
  2. <div style="width: 100%;">
  3. <!--仪表盘-->
  4. <div id="gauge"></div>
  5. <!--柱图-->
  6. <div id="bar"></div>
  7. </div>
  8. </template>
  9. <style scoped>
  10. #gauge {
  11. width: 8rem;
  12. height: 5.5rem;
  13. position: absolute;
  14. top: 2.55rem;
  15. left: 5.7rem;
  16. }
  17. #bar {
  18. width: 8rem;
  19. height: 2.2rem;
  20. position: relative;
  21. top: 2.8rem;
  22. left: 5.7rem;
  23. }
  24. </style>

2.创建方法绘制图表并调用

        methods 中分别创建绘制图表的方法 ,然后在挂载阶段 mounted 中分别调用。

  1. <script>
  2. export default {
  3. data() {
  4. return {};
  5. },
  6. methods: {
  7. //绘制柱状图
  8. draw_bar() {
  9. let myEchart = this.$echarts.init(document.getElementById("bar"));
  10. var option = {};
  11. myEchart.setOption(option);
  12. },
  13. //绘制仪表盘
  14. draw_gauge() {
  15. let myEchart = this.$echarts.init(document.getElementById("gauge"));
  16. var option = {};
  17. myEchart.setOption(option);
  18. },
  19. },
  20. mounted() {
  21. //调用绘制图表的方法
  22. this.draw_bar();
  23. this.draw_gauge();
  24. },
  25. };
  26. </script>

3.在option设置图表及其样式

        可直接将官网案例的代码复制到 option 处后自行修改。 

三、要点知识总结

        实现图表动态变化的原理其实就是基于定时器 setInterval() ,它与 setTimeout() 区别是 setInterval() 是周期性的,按照给定的时间周期反复循环的执行包含在它里面的程序,而setTimeout() 是在给定的时间后执行一次程序就结束。

        所以我们的做法就是,设置循环定时器,每隔一定的时间便获取一次图表中的数据且数据完全随机,并重新显示图表,然后在设置合适的动画和间隔时间,这样就实现了图表的动态变化。

比如的定时器设置如下:

  1. setInterval(() => {
  2. for (let i = 0; i <= 11; i++) { //定义i确保柱图的每一项都能被刷新
  3. option.series[0].data[i] = (Math.round(Math.random() * 600) + 1); //数据随机取值1-600,不要为0,如果为0的话该柱就会消失
  4. }
  5. myEchart.setOption(option, true); //每刷新一次重新显示图表
  6. }, 200);

        每隔200毫秒重新定义一次柱状图中的数据(option.series[0].data[i]) ,此处为1-600的随机数,每次数据更新后重新显示图表(myEchart.setOption(option, true)),这样就达到了数据不停变化的效果。

        然后就是动画,在echarts官网中配置项文档中有该类属性,可以设置仪表盘指针的变换速度、柱图中的柱变换速度等。

animation: true是否开启动画

animationDuration: 1020

初始动画的时长
animationDurationUpdate: 1020数据更新动画的时长
animationEasingUpdate: "quadraticIn"数据更新动画的缓动效果

        最后将动画时长与定时器间隔时长合理搭配即可实现动态效果。

四、完整代码+详细注释

  1. <template>
  2. <div style="width: 100%;">
  3. <!--仪表盘-->
  4. <div id="gauge"></div>
  5. <!--柱图-->
  6. <div id="bar"></div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {}
  13. },
  14. methods: {
  15. // 绘制柱状图
  16. draw_bar() {
  17. let myEchart = this.$echarts.init(document.getElementById("bar"));
  18. var option = {
  19. xAxis: {
  20. type: 'category',
  21. data: ['银宝', '个险', '团险', '银宝', '个险', '团险', '银宝', '个险', '团险', '银宝', '个险', '团险'],
  22. axisLine: {
  23. show: true,
  24. onZero: true,
  25. symbol: "none",
  26. lineStyle: {
  27. color: "#e5e5e5"
  28. }
  29. },
  30. axisTick: {
  31. show: false
  32. },
  33. },
  34. yAxis: {
  35. show: false,
  36. type: 'value',
  37. axisTick: {
  38. show: false
  39. },
  40. axisLine: {
  41. show: false
  42. },
  43. axisLabel: {
  44. show: false
  45. }
  46. },
  47. //图表与容器的位置关系
  48. grid: {
  49. left: '3%', // 与容器左侧的距离
  50. right: '3%', // 与容器右侧的距离
  51. top: '11%', // 与容器顶部的距离
  52. bottom: '12%', // 与容器底部的距离
  53. },
  54. series: [
  55. {
  56. data: [520, 600, 450, 380, 370, 510, 120, 200, 150, 620, 600, 450,],
  57. type: 'bar',
  58. backgroundStyle: {
  59. color: "rgba(111, 111, 22, 1)"
  60. },
  61. //坐标轴显示器的文本标签
  62. label: {
  63. show: true,
  64. position: 'top',
  65. color: '#e5e5e5'
  66. },
  67. //柱条颜色
  68. itemStyle: {
  69. color: {
  70. type: 'linear',
  71. x: 0,
  72. y: 0,
  73. x2: 0,
  74. y2: 1,
  75. colorStops: [{
  76. offset: 0, color: 'rgba(0,234,223,0.9)' // 0% 处的颜色
  77. }, {
  78. offset: 1, color: 'rgba(0,234,223,0.3)' // 100% 处的颜色
  79. }],
  80. global: false // 缺省为 false
  81. }
  82. },
  83. animationEasing: "linear",
  84. animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
  85. animationDurationUpdate: 300, //数据更新动画的时长
  86. animation: true //开启动画
  87. }
  88. ]
  89. };
  90. //此处使用定时器setInterval循环刷新柱状图的值,每次刷新数据均不同
  91. setInterval(() => {
  92. for (let i = 0; i <= 11; i++) { //定义i确保柱图的每一项都能被刷新
  93. option.series[0].data[i] = (Math.round(Math.random() * 600) + 1); //数据随机取值1-600,不要为0,如果为0的话该柱就会消失
  94. }
  95. myEchart.setOption(option, true); //每刷新一次重新显示图表
  96. }, 200);
  97. },
  98. //绘制仪表盘
  99. draw_gauge() {
  100. let myEchart = this.$echarts.init(document.getElementById("gauge"));
  101. var option = {
  102. series: [
  103. //左侧仪表盘
  104. {
  105. name: 'gauge 1',
  106. type: 'gauge',
  107. min: 0,
  108. max: 150,
  109. startAngle: 230,
  110. endAngle: -310,
  111. splitNumber: 5,
  112. radius: '35%',
  113. center: ['21%', '55%'],
  114. axisLine: {
  115. lineStyle: {
  116. color: [[1, '#34FFCA']],
  117. width: 12,
  118. }
  119. },
  120. splitLine: {
  121. distance: -7,
  122. length: 16,
  123. lineStyle: {
  124. color: '#fff',
  125. width: 1
  126. }
  127. },
  128. axisLabel: {
  129. distance: 2,
  130. fontSize: 10,
  131. fontWeight: 400,
  132. fontFamily: 'Arial',
  133. color: '#fff'
  134. },
  135. anchor: {},
  136. pointer: {
  137. width: 5,
  138. length: '60%',
  139. itemStyle: {
  140. color: '#fff'
  141. }
  142. },
  143. detail: {
  144. show: false
  145. },
  146. data: [
  147. {
  148. value: 20
  149. }
  150. ],
  151. animationEasing: "linear",
  152. animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
  153. animationDurationUpdate: 1000, //数据更新动画的时长
  154. animation: true //开启动画
  155. },
  156. //中间的仪表盘
  157. {
  158. name: 'gauge 2',
  159. type: 'gauge',
  160. min: 0,
  161. max: 180,
  162. z: 10,
  163. startAngle: 210,
  164. endAngle: -30,
  165. splitNumber: 9,
  166. radius: '50%',
  167. center: ['50%', '50%'],
  168. axisLine: {
  169. show: false,
  170. lineStyle: {
  171. width: 2,
  172. color: [
  173. [0.825, '#fff'],
  174. ]
  175. }
  176. },
  177. splitLine: {
  178. distance: 35,
  179. length: 22,
  180. lineStyle: {
  181. color: '#fff',
  182. width: 1
  183. }
  184. },
  185. axisLabel: {
  186. distance: 3,
  187. fontSize: 12,
  188. fontWeight: 400,
  189. fontFamily: 'Arial',
  190. color: '#fff'
  191. },
  192. anchor: {},
  193. pointer: {
  194. width: 6,
  195. offsetCenter: [0, '-10%'],
  196. length: '75%',
  197. itemStyle: {
  198. color: '#fff'
  199. }
  200. },
  201. data: [
  202. {
  203. value: 130
  204. // name: '1/min x 1000'
  205. }
  206. ],
  207. detail: {
  208. show: false
  209. },
  210. animationEasing: "linear",
  211. animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
  212. animationDurationUpdate: 1000, //数据更新动画的时长
  213. animation: true //开启动画
  214. },
  215. {
  216. name: 'gauge 3',
  217. type: 'gauge',
  218. min: 0,
  219. max: 8,
  220. z: 10,
  221. splitNumber: 8,
  222. radius: '50%',
  223. axisLine: {
  224. lineStyle: {
  225. width: 12,
  226. color: [[1, '#34FFCA']]
  227. }
  228. },
  229. splitLine: {
  230. show: false,
  231. },
  232. axisTick: {
  233. show: false
  234. },
  235. axisLabel: {
  236. show: false
  237. },
  238. anchor: {},
  239. pointer: {
  240. show: false
  241. },
  242. title: {
  243. show: false
  244. },
  245. detail: {
  246. show: false,
  247. offsetCenter: ['0', '70%'],
  248. color: '#FFF',
  249. fontSize: 18,
  250. formatter: '{value}.00'
  251. },
  252. // value is speed
  253. data: [
  254. {
  255. value: 130,
  256. }
  257. ],
  258. animationEasing: "linear",
  259. animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
  260. animationDurationUpdate: 1000, //数据更新动画的时长
  261. animation: true //开启动画
  262. },
  263. //右侧的仪表盘
  264. {
  265. name: 'gauge 4',
  266. type: 'gauge',
  267. min: 0,
  268. max: 8,
  269. startAngle: 135,
  270. endAngle: -50,
  271. radius: '37%',
  272. center: ['79%', '55%'],
  273. //右侧表盘颜色
  274. axisLine: {
  275. lineStyle: {
  276. color: [[1, '#34FFCA']],
  277. width: 12
  278. }
  279. },
  280. detail: {
  281. show: false
  282. },
  283. splitLine: {
  284. show: false,
  285. length: 6
  286. },
  287. axisTick: {
  288. show: false
  289. },
  290. axisLabel: {
  291. show: false
  292. },
  293. anchor: {},
  294. pointer: {
  295. show: true,
  296. width: 5,
  297. itemStyle: {
  298. color: '#fff'
  299. }
  300. },
  301. data: [
  302. {
  303. value: 6,
  304. name: ''
  305. }
  306. ],
  307. animationEasing: "linear",
  308. animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
  309. animationDurationUpdate: 1000, //数据更新动画的时长
  310. animation: true //开启动画
  311. },
  312. {
  313. name: 'gauge 5',
  314. type: 'gauge',
  315. min: 0,
  316. max: 8,
  317. splitNumber: 4,
  318. startAngle: 132,
  319. endAngle: -45,
  320. radius: '30%',
  321. center: ['79%', '55.3%'],
  322. axisLine: {
  323. lineStyle: {
  324. width: 0,
  325. color: [
  326. [0.15, '#f00'],
  327. [1, 'rgba(255, 0, 0, 0)']
  328. ]
  329. }
  330. },
  331. axisLabel: {
  332. distance: 1,
  333. fontSize: 10,
  334. fontWeight: 400,
  335. fontFamily: 'Arial',
  336. color: '#fff',
  337. },
  338. splitLine: {
  339. distance: 35,
  340. length: 12,
  341. lineStyle: {
  342. color: '#fff',
  343. width: 1
  344. }
  345. },
  346. animationEasing: "linear",
  347. animationEasingUpdate: "quadraticIn", //数据更新时的缓动效果
  348. animationDurationUpdate: 1000, //数据更新动画的时长
  349. animation: true //开启动画
  350. },
  351. ]
  352. };
  353. //使用定时器setInterval循环刷新仪表盘的值
  354. setInterval(() => {
  355. option.series[0].data[0].value = (Math.random() * 150).toFixed(2) - 0; //表盘1
  356. option.series[1].data[0].value = (Math.random() * 180).toFixed(2) - 0; //表盘2
  357. option.series[3].data[0].value = (Math.random() * 8).toFixed(2) - 0; //表盘3
  358. myEchart.setOption(option, true); //每次刷新后重新显示图表
  359. }, 500);
  360. }
  361. },
  362. mounted() {
  363. //调用绘制图表的方法
  364. this.draw_bar();
  365. this.draw_gauge()
  366. }
  367. }
  368. </script>
  369. <style scoped>
  370. #gauge {
  371. width: 8rem;
  372. height: 5.5rem;
  373. position: absolute;
  374. top: 2.55rem;
  375. left: 5.7rem;
  376. }
  377. #bar {
  378. width: 8rem;
  379. height: 2.2rem;
  380. position: relative;
  381. top: 2.8rem;
  382. left: 5.7rem;
  383. }
  384. </style>
网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发