vue echarts 柱状图和折线图的组合

柱状图和折线图的组合代码如下:

html 复制代码
<template>
  <div>
    <div 
      id="barLineChart" 
      ref="barLineChartRef" 
      style="width: 100%; height: 450px"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  name: 'positonStockTrunoverRate',
  components: {},
  props: {},
  data () {
    return {
      chart: null,
      _thisForChart: null,
      _thisForWindow: null,
      barLineChartData: {},
    }
  },
  created () {},
  mounted() {
    this.$nextTick(() => {
      this.initBarLineChart()
      this.addEventListenerToSidebarContainer(this)
      this.addEventListenerToWindowResize(this)
    })
  },
  beforeDestroy () {
    this.removeEventListenerToSidebarContainer()
    this.removeEventListenerToWindowResize()
  },
  computed: {},
  watch: {},
  methods: {
    initBarLineChart() {
      var chartDom = document.getElementById('barLineChart');
      this.chart = echarts.init(chartDom);
      let option = {
        // color: ['#57a1ff','#e58a96'],
        color: ['#57a1ff','#eb9912'],
        legend: {
          x: 'left',
          y: 'bottom',
          padding: [0, 0, 0, 25],
          itemWidth: 15, // 图例标记的图形宽度
          itemHeight:8, // 图例标记的图形高度
          data: [
            {name: '换手率', icon: 'circle'},
            {name: '全部换手率中位数',icon: 'rect'}
          ]
        },
        grid: {
          left: '3%',
          right: '1%',
          bottom: '12%',
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba( 0, 0, 0,0.7)',
          borderColor: 'rgba( 0, 0, 0,0.7)',
          formatter:function(params) {
            var str = params[0].name + '</br>'
            for(let item of params) {
              str = `<span style='color: #fff;'>${str}</span><div style='display:flex;align-items:center;justify-content:space-between;'><span>${item.marker}<span style='color: #fff;'>${item.seriesName}</span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style='color: #fff;'>${item.value}%</span></div>`
            }
            return str;
          }
        },
        xAxis: {
          data: [ 
            '2019-01',
            '2019-02',
            '2019-03',
            '2019-04',
            '2019-05',
            '2019-06',
            '2019-07',
            '2022-01',
            '2022-02',
            '2022-03',
          ],
          axisLabel: {
            show: true,
            color: '#606266',
          },
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '',
            }
          },  
        },
        yAxis: {
          name: '换手率:%',
            nameTextStyle: {
              color: '#000',
              padding: [0, 0, 0, -10] // 上右下左的间距,单位为像素
            },
          axisLabel: {
            show: true,
            color: '#000',
            formatter: (value) => {
              return `${value}%`
            }
          },
          splitLine: {
              lineStyle: {
                type: 'dashed'
              }
            },
        },
        series: [
          {
            name: "换手率",
            
            type: "bar", 
            data: [5, 20, 36, 10, 10, 20,33,16,52,10] 
          },
          {
            name: "全部换手率中位数",
            type: "line",
            symbol: 'none',
            // smooth: true,
            itemStyle: {
              normal: {
                color: "#eb9912"
              }
            },
            data: [5, 20, 36, 10, 10, 20,77,16,52,10] 
          }
        ]
      }
      this.chart.setOption(option,true);
    },



     // 监听侧边栏导航的宽度发生变化
     addEventListenerToSidebarContainer(_this) {
      let sidebarContainer = document.getElementsByClassName("sidebar-container")[0];
      this._thisForChart = _this;
      sidebarContainer &&
        sidebarContainer.addEventListener("transitionend", this.sidebarResizeHandler);
    },
    removeEventListenerToSidebarContainer() {
      let sidebarContainer = document.getElementsByClassName("sidebar-container")[0];
      this._thisForChart = null
      sidebarContainer &&
        sidebarContainer.removeEventListener("transitionend", this.sidebarResizeHandler);
    },

    sidebarResizeHandler(e) {
      if (e.propertyName === "width") {
        this._thisForChart.chart.resize();
      }
    },

    // window 的尺寸发生变化的时候 会执行图表的resize
    addEventListenerToWindowResize(_this) {
      this._thisForWindow = _this;
      window.addEventListener("resize", this.windowResizeHandler);
    },
    removeEventListenerToWindowResize(_this) {
     this. _thisForWindow = null
      window.removeEventListener("resize", this.windowResizeHandler);
    },

    windowResizeHandler(e) {
      this._thisForWindow.chart.resize();
    },
  },
}
</script>

<style lang="scss" scoped>
  
</style>

展示效果图如下:

相关推荐
胡西风_foxww4 分钟前
【ES6复习笔记】箭头函数(5)
javascript·笔记·es6·函数·箭头·箭头函数
编程百晓君24 分钟前
OpenHarmony开发板环境搭建
vue.js
LCG元26 分钟前
Vue.js组件开发-实现列表无缝动态滚动
vue.js
残花月伴1 小时前
axios
javascript
GISer_Jing1 小时前
前端经典面试合集(二)——Vue/React/Node/工程化工具/计算机网络
前端·vue.js·react.js·node.js
van叶~2 小时前
仓颉语言实战——2.名字、作用域、变量、修饰符
android·java·javascript·仓颉
泯泷2 小时前
JS代码混淆器:JavaScript obfuscator 让你的代码看起来让人痛苦
开发语言·javascript·ecmascript
高兴蛋炒饭9 小时前
RouYi-Vue框架,环境搭建以及使用
前端·javascript·vue.js
ᥬ 小月亮10 小时前
Vue中接入萤石等直播视频(更新中ing)
前端·javascript·vue.js
夜斗(dou)10 小时前
node.js文件压缩包解析,反馈解析进度,解析后的文件字节正常
开发语言·javascript·node.js