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

eachart多层级X轴箱体图

image

 

<template><div class="chart-wrapper"><div ref="chartRef" style="width: 100%; height: 550px;"></div></div>
</template><script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import * as echarts from 'echarts';const chartRef = ref(null);
let myChart = null;/*** 注意:箱线图通常需要较多数据点来计算 [最小值, 下四分位数, 中位数, 上四分位数, 最大值]* 如果您的 value 只有 3 个值,箱体看起来会比较窄。*/
const rawData = [{name: '批次1',listArr: [{ name: 'a1', value: [12, 25, 26, 27, 40, 45, 50] },{ name: 'a2', value: [5, 15, 16, 17, 22, 30, 35] }]},{name: '批次2',listArr: [{ name: 'b1', value: [10, 25, 26, 27, 33, 40, 55] },{ name: 'b2', value: [8, 15, 16, 17, 25, 28, 32] },{ name: 'b3', value: [2, 10, 12, 14, 20, 25, 28] }]},{name: '批次3',listArr: [{ name: 'c1', value: [15, 20, 22, 24, 30, 35, 42] },{ name: 'c2', value: [10, 18, 19, 20, 26, 30, 33] }]}
];const initChart = () => {if (!chartRef.value) return;myChart = echarts.init(chartRef.value);// 1. 数据转换const childCategories = [];const boxData = []; // 存储箱线图原始数组const groupInfos = [];let currentIndex = 0;rawData.forEach((parent) => {const start = currentIndex;const len = parent.listArr.length;parent.listArr.forEach((child) => {childCategories.push(child.name);boxData.push(child.value); // 箱线图的数据是一个数组});groupInfos.push({name: parent.name,start: start,end: start + len - 1});currentIndex += len;});const option = {title: {text: '多层级 X 轴箱体图方案',left: 'center',top: 10},tooltip: {trigger: 'item',axisPointer: { type: 'shadow' }},grid: {top: 80,bottom: 120, // 留够空间给底部的括号和文字left: '10%',right: '10%'},xAxis: {type: 'category',data: childCategories,boundaryGap: true,nameGap: 30,axisLabel: { interval: 0, margin: 15 },axisTick: { alignWithLabel: true },axisLine: { lineStyle: { color: '#ccc' } }},yAxis: {type: 'value',name: '数值',splitLine: { lineStyle: { type: 'dashed' } }},series: [{name: '箱线图',type: 'boxplot',data: boxData,itemStyle: {color: '#b8c5f2',borderColor: '#5470c6'}},// 自定义系列:绘制半包围括号和居中文字{type: 'custom',renderItem: (params, api) => {const group = groupInfos[params.dataIndex];const startPos = api.coord([group.start, 0]);const endPos = api.coord([group.end, 0]);const width = api.size([1, 0])[0];// 几何中心计算const xLeft = startPos[0] - width / 2 + 5; // 稍微内缩一点点好看const xRight = endPos[0] + width / 2 - 5;const xCenter = (xLeft + xRight) / 2;const yBase = params.coordSys.y + params.coordSys.height;const yTop = yBase + 15;    // 括号横线离 X 轴的距离const yBottom = yBase + 40; // 括号竖线下伸的长度const yText = yBase + 65;   // 文字高度return {type: 'group',children: [// 绘制括号 [左下, 左上, 右上, 右下]{type: 'polyline',shape: {points: [[xLeft, yBottom],[xLeft, yTop],[xRight, yTop],[xRight, yBottom]]},style: {stroke: '#888',lineWidth: 2,fill: 'none'}},// 绘制居中文字{type: 'text',style: {text: group.name,x: xCenter,y: yText,textAlign: 'center',textFont: 'bold 14px sans-serif',fill: '#333'}}]};},data: groupInfos.map((_, i) => i),clip: false,silent: true}]};myChart.setOption(option);
};onMounted(() => {initChart();window.addEventListener('resize', () => myChart && myChart.resize());
});onUnmounted(() => {if (myChart) myChart.dispose();
});
</script><style scoped>
.chart-wrapper {background: #ffffff;padding: 20px;border-radius: 12px;box-shadow: 0 4px 20px rgba(0,0,0,0.05);
}
</style>

  

 

http://www.jsqmd.com/news/698273/

相关文章:

  • Visual Syslog Server:5分钟掌握Windows免费日志监控神器的终极指南
  • 别再死记硬背了!用生活中的例子轻松搞懂C#的int、string、bool这些数据类型
  • Unity透明窗口终极指南:5步打造桌面悬浮神器
  • 2026年最新好用的OA系统推荐!6款热门OA系统盘点
  • 从爱迪生到特斯拉:聊聊那些年我们差点错过的交流电(附变压器工作原理图解)
  • 福卡回收价92%起!避开高价陷阱,跟着教程走,小白也能秒上手 - 可可收
  • 聊聊2026年江浙沪做环保绿色生产的家具品牌,价格如何 - 工业设备
  • PAJ7620手势模块的5个实战技巧:从STM32驱动到减少误触发的心得
  • 数学分析进阶书单:从经典原理到现代流形(2024精选)
  • WSL2下snap报错‘no such file or directory’?手把手教你启用systemd搞定PyCharm安装
  • airPLS算法革新:自适应迭代加权惩罚最小二乘法突破基线校正技术瓶颈
  • 4月25日新闻速览:从航天、金融到国际局势,一天看懂世界变化
  • 黑芝麻智能C1200汽车SoC:跨域计算与异构架构解析
  • 2026年洛阳商务宴请与商务聚餐完全指南:江浙菜高端定制如何破局性价比困局 - 年度推荐企业名录
  • 2026年上海灭火设备公司榜单分析,烟罩灭火设备/灶台灭火设备/食堂灭火设备/学校厨房灭火设备/学校食堂灭火设备 - 品牌策略师
  • 你的PWM脉冲数真的准吗?用STM32CubeMX和HAL库调试PWM输出个数的避坑实战记录
  • Qt使用http发送与解析json数据二(使用Qt网络编程API调用post、get方法)———附送完整源代码
  • HEIF Utility:打破Windows平台HEIF图片兼容壁垒的免费利器
  • 全方位解析百联OK卡回收平台,让你的卡不再闲置 - 团团收购物卡回收
  • 从Civitai与HuggingFace看AIGC模型生态:技术普惠、内容边界与本土化挑战
  • 2026年甘肃性价比高的KBSG矿用隔爆型负荷中心用干式变压器厂家排名 - 工业品牌热点
  • 别再硬编码了!用FlexSim脚本函数动态处理‘多品种小批量’订单组合
  • 从零到一:在CentOS 7上实战部署若依前后端分离项目(ruoyi-vue)全流程解析
  • 图像篡改检测的“火眼金睛”是如何炼成的?深入浅出解读MVSS-Net的多视图与多尺度设计
  • 2026年珠三角海运与多式联运全链路方案对比指南:找到最稳妥的货代伙伴 - 企业名录优选推荐
  • AltSnap:让Windows窗口管理像Linux一样高效
  • Python ERA5 水汽通量散度图实战:从数据下载到SCI级地图绘制的完整流程
  • 如何通过Pomotroid实现高效时间管理:番茄工作法新手指南
  • HarmonyOS在金融嵌入式设备中的应用开发实践:从入门到精通
  • 解决研发打样难:苏州支持“1件起订”的精密零件加工厂 - 莱图加精密零件加工