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

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

<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>

展示效果图如下:

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts 是一个基于 JavaScript 的数据可视化库,可以帮助开发人员快速构建各种图表。在 Echarts 中,要实现柱状图加折线的效果,可以使用组合图(Mixed Chart)功能。 在 Vue 开发中使用 Echarts,可以按照以下步骤进行操作: 1. 首先,你需要在项目中引入 Echarts 库。可以通过 npm 安装 echarts,然后在项目中引入 echarts。 2. 在 Vue 组件中,按照 Echarts 的文档,设置好需要展示的数据和配置项。 3. 创建一个 div 元素作为图表的容器,通过 ref 属性获取该元素的引用。 4. 在 Vue 的 mounted 生命周期函数中,使用引入的 Echarts 库初始化图表,并将容器和配置项传递给 Echarts 实例。 5. 最后,将图表实例挂载到 Vue 组件中,即可在页面上显示柱状图加折线的效果。 下面是一个示例代码,展示了如何在 Vue 中实现柱状图加折线的效果: ```javascript <template> <div ref="chart" style="width: 100%; height: 400px;"></div> </template> <script> import echarts from 'echarts'; export default { mounted() { const chart = echarts.init(this.$refs.chart); const option = { xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'], }, yAxis: [ { type: 'value', name: '柱状图', }, { type: 'value', name: '折线图', }, ], series: [ { name: '柱状图', type: 'bar', data: [120, 200, 150, 80, 70, 110, 130], }, { name: '折线图', type: 'line', yAxisIndex: 1, data: [20, 40, 50, 30, 15, 25, 35], }, ], }; chart.setOption(option); }, }; </script> ``` 在上述代码中,通过设置 xAxis、yAxis 和 series 来配置柱状图折线图的相关参数。其中,xAxis 配置 x 轴数据,yAxis 配置 y 轴数据,series 配置图表的系列数据。将配置项传递给 Echarts 实例后,使用 setOption 方法来渲染图表。最后,将图表实例挂载到 Vue 组件中的 div 容器中,即可在页面上显示柱状图加折线的效果。 希望以上信息能够对你有所帮助,如果有任何疑问,请随时提问。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [Echarts柱状图+折线图合并---实现效果详解(vue+Echarts实现)](https://blog.csdn.net/wuzhiyue2/article/details/118530433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值