VUE中如何渲染Echarts动画柱状图

柱状图

效果图

-1

安装Echarts依赖

要在vue中使用Echarts 需要先安装依赖

  1. npm install echarts save

这是我的Echarts版本

  1. “echarts”: “^4.0.4”

第一步:在template减免 容器dom

  1. <template>
  2.      <div class=“min-body”>
  3.          <div style=width:80vw;height: 80vh id=“EchartsAnimate” ref=“docement”></div>
  4.      </div>
  5. </template>

第二步:初始化 Echarts

-2

注意:此处有一方法,是用于适配 Echarts 字体适配大屏的

  1. chartsRelativeSize(percent, derection) {
  2.                  var windowW = this.$refs.docement.offsetWidth;
  3.                  var windowH = this.$refs.docement.offsetHeight
  4.                  var r = 0;
  5.                  if (‘h’ === derection) {
  6.                      r = percent * windowH / 100;
  7.                  } else {
  8.                      r = percent * windowW / 100;
  9.                  }
  10.                  return r;
  11. },

使用:

-3

第三步:请求数据 加载柱状图动画

原理:设置定时器,删除Echarts 柱状图的第一个数据的同属压入数组的最后一个

  1. for(var i=0;i<this.echartsData.list.length && i<5;i++){
  2.                      option.xAxis[0].data.push( that.echartsData.list[i].mc);
  3.                      option.series[0].data.push( that.echartsData.list[i].sysl);
  4.                      option.series[1].data.push( that.echartsData.list[i].dysl);
  5.      }
  6.                  myCharts.setOption(option);
  7.                  if(window.hbfxInterval){
  8.                      clearInterval(window.hbfxInterval);
  9.                  }
  10.                  window.hbfxIndex = 0;
  11.                  window.hbfxInterval = setInterval(function () {
  12.                      option.xAxis[0].data.shift();
  13.                      option.xAxis[0].data.push( that.echartsData.list[window.hbfxIndex% that.echartsData.list.length].mc);
  14.                      option.series[0].data.shift();
  15.                      option.series[0].data.push( that.echartsData.list[window.hbfxIndex% that.echartsData.list.length].sysl);
  16.                      option.series[1].data.shift();
  17.                      option.series[1].data.push( that.echartsData.list[window.hbfxIndex% that.echartsData.list.length].dysl);
  18.                      myCharts.setOption(option);
  19.      window.hbfxIndex ++;
  20.                  },2000)

完整页面代码:

  1. <template>
  2.      <div class=“min-body”>
  3.          <div style=width:80vw;height: 80vh id=“EchartsAnimate” ref=“docement”></div>
  4.      </div>
  5. </template>
  6. <script>
  7.      export default {
  8.          name: “EchasrtsAnimate”,
  9.          data:function () {
  10.              return {
  11.                  echartsData:{“size”:21,“list”:[{“dysl”:“70”,“mc”:“刑事警情”,“dm”:“01”,“sysl”:“89”},{“dysl”:“75”,“mc”:“行政(治安)警情”,“dm”:“02”,“sysl”:“107”},{“dysl”:“4”,“mc”:“交通类警情”,“dm”:“03”,“sysl”:“21”},{“dysl”:“7”,“mc”:“火灾事故”,“dm”:“04”,“sysl”:“9”},{“dysl”:“43”,“mc”:“群众求助”,“dm”:“05”,“sysl”:“71”},{“dysl”:“5”,“mc”:“举报投诉”,“dm”:“06”,“sysl”:“10”},{“dysl”:“20”,“mc”:“纠纷”,“dm”:“08”,“sysl”:“44”},{“dysl”:“1”,“mc”:“灾害事故”,“dm”:“09”,“sysl”:“1”},{“dysl”:“56”,“mc”:“其他行政违法”,“dm”:“10”,“sysl”:“71”},{“dysl”:“56”,“mc”:“经济案件类警情”,“dm”:“11”,“sysl”:“65”},{“dysl”:“9429”,“mc”:“违法犯罪警情”,“dm”:“20”,“sysl”:“12320”},{“dysl”:“25597”,“mc”:“交通警情”,“dm”:“21”,“sysl”:“29575”},{“dysl”:“272”,“mc”:“火灾事故”,“dm”:“22”,“sysl”:“369”},{“dysl”:“6040”,“mc”:“群众求助”,“dm”:“23”,“sysl”:“7307”},{“dysl”:“4203”,“mc”:“举报投诉”,“dm”:“24”,“sysl”:“4983”},{“dysl”:“27”,“mc”:“群体事件”,“dm”:“25”,“sysl”:“44”},{“dysl”:“9679”,“mc”:“纠纷”,“dm”:“26”,“sysl”:“12396”},{“dysl”:“44”,“mc”:“灾害事故”,“dm”:“27”,“sysl”:“70”},{“dysl”:“109”,“mc”:“扬言”,“dm”:“28”,“sysl”:“146”},{“dysl”:“17”,“mc”:“警情备案”,“dm”:“30”,“sysl”:“23”},{“dysl”:“75”,“mc”:“其他警情”,“dm”:“90”,“sysl”:“69”}]}
  12.              }
  13.          },
  14.          mounted(){
  15.                  this.loadEchartsAnimate()
  16.          },
  17.          methods:{
  18.              chartsRelativeSize(percent, derection) {
  19.                  var windowW = this.$refs.docement.offsetWidth;
  20.                  var windowH = this.$refs.docement.offsetHeight
  21.                  var r = 0;
  22.                  if (‘h’ === derection) {
  23.                      r = percent * windowH / 100;
  24.                  } else {
  25.                      r = percent * windowW / 100;
  26.                  }
  27.                  return r;
  28.              },
  29.              loadEchartsAnimate(){
  30.                  var that = this
  31.                  var echarts = require(‘echarts’);
  32.                  var myCharts = echarts.init(document.getElementById(‘EchartsAnimate’));
  33.                  var option = {
  34.                      color:[‘#407FFF’,‘#00CCCD’],
  35.                      tooltip : {
  36.                      trigger: ‘axis’,
  37.                      textStyle:{
  38.                      color:‘#ffffff’,
  39.                      fontSize: this.chartsRelativeSize(0.5, ‘r’)
  40.                      },
  41.                      formatter: function (params) {
  42.                      var arr = [];
  43.                      params.forEach(function (item) {
  44.                      var N=item.seriesName;
  45.                      var V = ;
  46.                      if(item.value>100000000){//亿
  47.                      V += (item.value/100000000).toFixed(1) + ‘亿’;
  48.                      }else if( item.value>10000){//万
  49.                      V += (item.value/10000).toFixed(1) + ‘万’;
  50.                      }else{
  51.                      V += item.value;
  52.                      }
  53.                      var a = {name:N,value:V};
  54.                      arr.push(a);
  55.                      });
  56.                      var num = ;
  57.                      arr.forEach(function (item) {
  58.                      num += item.name+“:”+item.value+“<br/>”;
  59.                      });
  60.                      return params[0].name +“<br>”+ num;
  61.                      }
  62.                      },
  63.                      grid: {
  64.                      left: ’25vw’,
  65.                      right: ’25vw’,
  66.                      bottom: ‘9vh’,
  67.                      top:’30vh’,
  68.                      containLabel: true
  69.                      },
  70.                      /*dataZoom:[{
  71.                      show: true,
  72.                      height: 10,
  73.                      bottom: 0,
  74.                      startValue:0,
  75.                      endValue:4,
  76.                      handleIcon: ‘path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z’,
  77.                      handleSize: ‘100%’,
  78.                      handleStyle:{color:”#ddd” },
  79.                      textStyle:{color:”#666666″},
  80.                      borderColor:”#eee”
  81.                      }],*/
  82.                      legend: {
  83.                      orient: ‘horizontal’,
  84.                      top:‘0’,
  85.                      x: ‘right’,
  86.                      itemWidth:15,
  87.                      itemHeight:15,
  88.                      textStyle:{
  89.                      color:‘#fff’,
  90.                      fontSize: this.chartsRelativeSize(0.5, ‘r’)
  91.                      },
  92.                      data:[‘上月’,‘本月’]
  93.                      },
  94.                      xAxis: [
  95.                      {
  96.                      type: ‘category’,
  97.                      axisLine: {
  98.                      lineStyle: {
  99.                      type: ‘solid’,
  100.                      color: ‘#2E3950’,//左边线的颜色
  101.                      width: ‘1’//坐标线的宽度
  102.                      }
  103.                      },
  104.                      axisLabel: {
  105.                      textStyle: {
  106.                      color: ‘#fff’,//坐标值得具体的颜色,
  107.                      fontSize: this.chartsRelativeSize(0.5, ‘r’)
  108.                      },
  109.                      interval:0
  110.                      },
  111.                      axisTick:false,
  112.                      data: [/*’崇川区’,’港闸区’,’通州区’,’如东县’,’海安市’,’启东市’,’海门市’,’如皋市’*/]
  113.                      }
  114.                      ],
  115.                      yAxis: [
  116.                      {
  117.                      type: ‘value’,
  118.                      axisLine: {
  119.                      lineStyle: {
  120.                      type: ‘solid’,
  121.                      color: ‘#2E3950’,//左边线的颜色
  122.                      width: ‘1’//坐标线的宽度
  123.                      }
  124.                      },
  125.                      axisLabel: {
  126.                      textStyle: {
  127.                      color: ‘#e6e6e6’,//坐标值得具体的颜色,
  128.                      fontSize: this.chartsRelativeSize(0.5, ‘r’)
  129.                      }
  130.                      },
  131.                      axisTick:false,
  132.                      splitLine: { //坐标轴在grid区域中的分隔线(网格中的横线)
  133.                      show: true,
  134.                      lineStyle: {
  135.                      color: [‘#2E3950’],
  136.                      width: 1,
  137.                      type: ‘solid’,
  138.                      }
  139.                      },
  140.                      splitArea: {//坐标轴在grid区域中的分隔区域(间隔显示网格区域)
  141.                      interval: 1, //显示间隔的规律
  142.                      show: true,
  143.                      areaStyle: {//分隔区域的样式
  144.                      color: [‘rgba(255,255,255,0.03)’, ‘rgba(255,255,255,0)’]
  145.                      }
  146.                      }
  147.                      }
  148.                      ],
  149.                      series : [
  150.                      {
  151.                      name:‘上月’,
  152.                      type:‘bar’,
  153.                      barGap:0,
  154.                      barWidth:15,
  155.                  data:[/*15,48,40,32,21,41,38,65*/]
  156.                      },
  157.                      {
  158.                      name:‘本月’,
  159.                      type:‘bar’,
  160.                      barGap:0,
  161.                      barWidth:15,
  162.                      data:[/*43,12,56,67,12,45,41,15*/]
  163.                      }
  164.                      ]
  165.                  };
  166.                  for(var i=0;i<this.echartsData.list.length && i<5;i++){
  167.                      option.xAxis[0].data.push( that.echartsData.list[i].mc);
  168.                      option.series[0].data.push( that.echartsData.list[i].sysl);
  169.                      option.series[1].data.push( that.echartsData.list[i].dysl);
  170.                  }
  171.                  myCharts.setOption(option);
  172.                  if(window.hbfxInterval){
  173.                      clearInterval(window.hbfxInterval);
  174.                  }
  175.                  window.hbfxIndex = 0;
  176.                  window.hbfxInterval = setInterval(function () {
  177.                      option.xAxis[0].data.shift();
  178.                      option.xAxis[0].data.push( that.echartsData.list[window.hbfxIndex% that.echartsData.list.length].mc);
  179.                      option.series[0].data.shift();
  180.                      option.series[0].data.push( that.echartsData.list[window.hbfxIndex% that.echartsData.list.length].sysl);
  181.                      option.series[1].data.shift();
  182.                      option.series[1].data.push( that.echartsData.list[window.hbfxIndex% that.echartsData.list.length].dysl);
  183.                      myCharts.setOption(option);
  184.                      window.hbfxIndex ++;
  185.                  },2000)
  186.              }
  187.          }
  188.      }
  189. </script>
  190. <style scoped>
  191. .minbody{
  192.      width: 100%;
  193.      height: 100%;
  194.      overflow: hidden;
  195.      background: #010827;
  196. }
  197. </style>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

标签

发表评论