当前位置: 首页 > backend >正文

【echarts】堆叠柱形图

主要功能

  1. 数据可视化:以堆叠柱形图形式展示两组数据(销售和技术)的月度分布情况。
  2. 响应式设计:图表会根据窗口大小自动调整。
  3. 数据交互:鼠标悬停时显示详细数据(包括每项的具体数值和总计)。
  4. 数据更新:当传入的 props 数据变化时,图表会自动更新。

 组件代码

<template><div class="chart-container" ref="chartRef"></div>
</template><script setup>
import { ref, onMounted, watch } from "vue";
import * as echarts from "echarts";const props = defineProps({// 接收秘密和机密数据secretData: {type: Array,default: () => [],},confidentialData: {type: Array,default: () => [],},// 可选:自定义标题title: {type: String,default: "堆叠柱形图",},// 可选:自定义x轴标签xAxisLabels: {type: Array,default: () => ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月",],},
});const chartRef = ref(null);
let chartInstance = null;// 初始化图表
const initChart = () => {if (!chartRef.value) return;chartInstance = echarts.init(chartRef.value);const option = {backgroundColor: "#fff",title: {text: props.title,left: "center",textStyle: {color: "#333",fontSize: 16,},},tooltip: {trigger: "axis",axisPointer: {type: "cross",crossStyle: {color: "#999",},},formatter: (params) => {let result = `<div class="font-bold">${params[0].name}</div>`;let total = 0;params.forEach((param) => {total += param.value;result += `<div class="flex items-center"><span class="inline-block w-3 h-3 rounded-full mr-2" style="background-color: ${param.color}"></span>${param.seriesName}: ${param.value}</div>`;});result += `<div class="font-bold mt-1">总计: ${total}</div>`;return result;},},legend: {data: ["销售", "技术"],top: "5%",right: "0",itemWidth: 10,itemHeight: 10,},grid: {left: "3%",right: "4%",bottom: "3%",containLabel: true,},xAxis: {type: "category",data: props.xAxisLabels,axisTick: false,axisLabel: {color: "#999999",},axisLine: {show: true, // 显示轴线lineStyle: {color: "#DDDDDD", // 轴线颜色width: 1, // 轴线宽度type: "solid", // 轴线样式:solid, dashed, dotted},},},yAxis: {type: "value",min: 0,axisLabel: {formatter: "{value}",color: "#999999",},splitLine: {show: false, // 隐藏y轴横线(网格线)},},series: [{name: "销售",type: "bar",stack: "总量",data: props.secretData,color: "#FC1705",barWidth: "30%",},{name: "技术",type: "bar",stack: "总量",data: props.confidentialData,color: "#970E02",barWidth: "30%",},],};chartInstance.setOption(option);// 监听窗口大小变化,调整图表window.addEventListener("resize", () => {if (chartInstance) chartInstance.resize();});
};// 初始化和更新图表
onMounted(() => {initChart();
});// 监听props变化,更新图表
watch([() => props.secretData, () => props.confidentialData], () => {if (chartInstance) {chartInstance.setOption({series: [{data: props.secretData,},{data: props.confidentialData,},],});}
});// 组件卸载时销毁图表
onUnmounted(() => {if (chartInstance) {chartInstance.dispose();chartInstance = null;}
});
</script><style scoped>
.chart-container {width: 100%;height: 360px;margin: 0 auto;
}
</style>

组件引用

<StackedBarChart:secretData="[0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0]":confidentialData="[0, 0, 0, 0, 0, 251, 0, 0, 0, 0, 0, 0]"
/>

http://www.xdnf.cn/news/11971.html

相关文章:

  • ubuntu自定义服务自动启动
  • Python绘图库及图像类型
  • SARIMA时间序列分析:三大模型对比
  • AWS中国区IAM相关凭证自行管理策略(只读CodeCommit版)
  • 【通用技巧】使用 AI 进行技术写作:使用 AI 写作助手编写技术内容的方法
  • MySQL 核心知识点解析
  • 高防IP可以防护什么攻击类型?企业网络安全的第一道防线
  • 找到每一个单词+模拟的思路和算法
  • Elasticsearch的写入性能优化
  • 常见优化器Optimizer总结
  • 网络编程之TCP编程
  • shell脚本一步完成批量创建删除Linux用户
  • 3D动画在微信小程序的实现方法
  • C语言数据结构笔记3:Union联合体+结构体取8位Bool量
  • 109页PPT华为流程模块L1-L4级梳理及研发采购服务资产5级建模
  • 如何通过RL真正提升大模型的推理能力?NVIDIA提出长期强化学习训练框架ProRL
  • Learning a Discriminative Prior for Blind Image Deblurring论文阅读
  • nest实现前端图形校验
  • Linux磁盘管理 - RAID
  • macOS 上使用 Homebrew 安装redis-cli
  • 我们来学zookeeper -- 集群搭建
  • Monorepo架构: 项目管理模式对比与考量
  • 详解ZYNQ中的 RC 和 EP
  • 解决idea编译运行项目时间长的问题
  • 深入理解C#中的Web API:构建现代化HTTP服务的完整指南
  • Redis 集群批量删除key报错 CROSSSLOT Keys in request don‘t hash to the same slot
  • leetcode删除排序链表中的重复元素-小白初学简单解说
  • JavaScript 深入探索:高级应用与前沿技术
  • 佰力博科技与您探讨半导体电阻测试的基本原理
  • 数据分析之OLTP vs OLAP