VUE3 ECharts 实现阶梯瀑布图(附源码和效果)

<template>
  <div id="main" class="echart-style">
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';
import { onMounted, ref } from 'vue';
let myChart = ref()
let option = ref({})
let data = ref([900, 345, 393, -108, -154, 135, 178, 286, -119, -361, -203]);
let help: any = [];
let positive: any = [];
let negative: any = [];
for (let i = 0, sum = 0; i < data.value.length; ++i) {
  if (data.value[i] >= 0) {
    positive.push(data.value[i]);
    negative.push('-');
  } else {
    positive.push('-');
    negative.push(-data.value[i]);
  }

  if (i === 0) {
    help.push(0);
  } else {
    sum += data.value[i - 1];
    if (data.value[i] < 0) {
      help.push(sum + data.value[i]);
    } else {
      help.push(sum);
    }
  }
}
onMounted(() => {
  init()
})
const init = () => {
  // 基于准备好的dom,初始化echarts实例
  myChart.value = echarts.init(document.getElementById('main'));
  // 绘制图表
  option.value = {
    title: {
      text: 'Waterfall'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      splitLine: { show: false },
      data: (function () {
        var list = [];
        for (var i = 1; i <= 11; i++) {
          list.push('Oct/' + i);
        }
        return list;
      })()
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        type: 'bar',
        stack: 'all',
        itemStyle: {
          normal: {
            barBorderColor: 'rgba(0,0,0,0)',
            color: 'rgba(0,0,0,0)'
          },
          emphasis: {
            barBorderColor: 'rgba(0,0,0,0)',
            color: 'rgba(0,0,0,0)'
          }
        },
        data: help
      },
      {
        name: 'positive',
        type: 'bar',
        stack: 'all',
        data: positive
      },
      {
        name: 'negative',
        type: 'bar',
        stack: 'all',
        data: negative,
        itemStyle: {
          color: '#f33'
        }
      }
    ]
  };
  myChart.value.setOption(option.value)
};
</script>
<style scoped>
.echart-style {
  width: 1000px;
  height: 900px;
  background: skyblue;
}
</style>

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 13:24:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 13:24:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 13:24:03       82 阅读
  4. Python语言-面向对象

    2024-03-21 13:24:03       91 阅读

热门阅读

  1. 后端异常处理:全局异常处理器

    2024-03-21 13:24:03       48 阅读
  2. 亚信安慧AntDB全景观察:数据库领域的创新者

    2024-03-21 13:24:03       41 阅读
  3. FPGA_AD9361

    2024-03-21 13:24:03       44 阅读
  4. 力扣126双周赛

    2024-03-21 13:24:03       45 阅读
  5. electron-builder打包

    2024-03-21 13:24:03       46 阅读
  6. 物理设计概念 -- 物理层次结构

    2024-03-21 13:24:03       34 阅读
  7. [Open3d]: 知识记录

    2024-03-21 13:24:03       40 阅读
  8. mybatis---->tx中weekend类

    2024-03-21 13:24:03       43 阅读
  9. Mac传文件到云服务器

    2024-03-21 13:24:03       45 阅读
  10. 二叉树遍历144、94、145

    2024-03-21 13:24:03       41 阅读
  11. Python基础算法解析:决策树

    2024-03-21 13:24:03       37 阅读
  12. css第一个元素first-child匹配失败原因

    2024-03-21 13:24:03       38 阅读