Vue + Echarts自定义图标颜色及柱状图动态伸缩(定时)

1、效果图:

2、Vue代码 

<template>
  <!--区域分布数量柱状图-->
  <div class="emp-area">

  </div>
</template>

<script>
  require('@/assets/theme/chalk')
  require('echarts/theme/macarons')
  export default {
    name: "StaffArea",
    data() {
      return {
        chart: null,
        examples: [],
        timerId: null,
      }
    },
    mounted() {
      // 动态效果,定时
      this.$nextTick(() => {
        this.initChart()
        this.timerId = setInterval(() => {
          this.examples = [];
          this.product();
          const option = this.chart.getOption();
          option.series[0].data = this.examples;
          this.chart.setOption(option);
          this.$forceUpdate()
        }, 5000);
      })
      window.addEventListener('resize', this.resize)
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
      window.removeEventListener('resize', this.resize)
    },
    created() {
      this.product()
    },
    destroyed() {
      clearInterval(this.timerId);
    },
    methods: {
      initChart() {
        this.chart = this.echarts.init(document.querySelector('.emp-area'), 'macarons')
        const cityColors = {
          '兰州市': '#CF4645',
          '嘉峪关市': '#B580B2',
          '金昌市': '#7cb9e8',
          '白银市': '#146A90',
          '天水市': '#8956FF',
          '武威市': '#70C9A8',
          '张掖市': '#bfbfbf',
          '平凉市': '#595959',
          '酒泉市': '#40a9ff',
          '庆阳市': '#1890ff',
          '定西市': '#ffd666',
          '陇南市': '#adf7b6',
          '临夏州': '#fa8c16',
          '甘南州': '#80ed99',
        };
        this.chart.setOption({
          title: {
            text: '甘肃省各地州市人员',
            padding: [0, 0, 0, 10],
            textStyle: {
              color: '#fff',
            },
          },
          legend: {
            show: false,
          },
          animationDuration: 0,
          animationDurationUpdate: 3000,
          animationEasing: 'linear',
          animationEasingUpdate: 'linear',
          grid: {
            top: '8%',
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            axisLabel: {
              rotate: 0,
              color: '#fff'
            },
            splitLine: {
              show: false,
            },
          },
          yAxis: {
            type: 'category',
            axisLabel: {
              rotate: 30,
              color: '#fff'
            },
            data: ['兰州市', '嘉峪关市', '金昌市', '白银市', '天水市', '武威市', '张掖市', '平凉市', '酒泉市', '庆阳市', '定西市', '陇南市', '临夏州', '甘南州'],
            inverse: true,
            animationDuration: 0,
            animationDurationUpdate: 300,
            max: 13 // only the largest 3 bars will be displayed
          },
          series: [
            {
              realtimeSort: true,
              name: '',
              type: 'bar',
              itemStyle: {
                opacity: 1,
                barBorderRadius: 3,
                color: function (param) {
                  return cityColors[param.name] || '#5470c6';
                },
              },
              data: this.examples,
              label: {
                show: true,
                position: 'right',
                valueAnimation: true,
              }
            }
          ],
        });
      },
      product() {
        for (let i = 0; i < 14; ++i) {
          this.examples.push(Math.round(Math.random() * 200));
        }
      },
      resize() {
        this.chart.resize()
      },
    }
  }
</script>

<style lang="less" scoped>
  .emp-area {
    width: 100%;
    height: 100%;
  }
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤来利用VueEcharts写一个3D柱: 1. 安装Echarts 可以通过npm安装Echarts: ``` npm install echarts --save ``` 2. 在Vue组件中引入EchartsVue组件中,通过import引入Echarts: ``` import echarts from 'echarts' ``` 3. 创建一个div元素 在Vue的template中创建一个div元素,用于放置Echarts表: ``` <template> <div ref="mychart" style="width: 500px;height:500px;"></div> </template> ``` 4. 在Vue的mounted钩子函数中初始化EchartsVue的mounted钩子函数中,使用Echarts的init方法初始化表,并将其挂载到之前创建的div元素中: ``` mounted() { let myChart = echarts.init(this.$refs.mychart) // 在这里添加Echarts的配置项和数据 myChart.setOption(option) } ``` 5. 添加Echarts的配置项和数据 在初始化Echarts之后,需要添加Echarts的配置项和数据。下面是一个简单的3D柱的例子: ``` let option = { tooltip: {}, visualMap: { max: 100, inRange: { color: ['#e0ffff', '#006edd'] } }, xAxis3D: { type: 'category', data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] }, yAxis3D: { type: 'value' }, zAxis3D: { type: 'category', data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, grid3D: { boxWidth: 200, boxDepth: 80, viewControl: { // 使用鼠标控制视角 autoRotate: true, // 默认视角 distance: 150 } }, series: [{ type: 'bar3D', data: [ ['A', 0, 'Jan', 66], ['B', 1, 'Feb', 79], ['C', 2, 'Mar', 70], ['D', 3, 'Apr', 61], ['E', 4, 'May', 85], ['F', 5, 'Jun', 51], ['G', 6, 'Jul', 77], ['H', 7, 'Aug', 93], ['A', 8, 'Sep', 82], ['B', 9, 'Oct', 55], ['C', 10, 'Nov', 45], ['D', 11, 'Dec', 68] ], shading: 'lambert', label: { show: true, textStyle: { fontSize: 16, borderWidth: 1 } }, emphasis: { label: { textStyle: { fontSize: 20, color: '#900' } }, itemStyle: { color: '#900' } } }] } ``` 这里的配置项和数据可以根据需要自行调整,具体的配置项和数据格式可以参考Echarts官方文档。 6. 运行程序 完成以上步骤后,可以运行程序,在浏览器中查看生成的3D柱。如果需要对表进行交互或者动态更新,可以利用Echarts提供的API实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值