echarts树状图与vue3

父组件 - 使用RadialTreeView

<template><div class="page-container"><div class="header-title">美国产品图谱 (ECharts Radial Tree)</div><RadialTreeView :chart-data="radialData" background-color="#E6E6FA" /></div>
</template><script setup lang="ts">
import RadialTreeView from "@/views/knowledgeGraph/radialTreeView/RadialTreeView.vue";
import {mockRadialData,TreeNode,
} from "@/views/knowledgeGraph/radialTreeView/mockRadialTreeData"; // 确保路径正确
import { ref } from "vue";const radialData = ref<TreeNode>(mockRadialData);// If you need to update data later:
// setTimeout(() => {
//   const newData = JSON.parse(JSON.stringify(mockRadialData)); // Deep copy
//   newData.children[0].name = 'Updated Customers';
//   newData.children[0].children.push({ name: 'New Customer Segment', id: 'new_cust', nodeType: 'customer' });
//   radialData.value = newData;
// }, 5000);
</script><style scoped>
.page-container {width: 100%;height: 90vh; /* Adjust as needed */display: flex;flex-direction: column;padding: 20px;box-sizing: border-box;background-color: #f0f2f5; /* A light background for the page */
}.header-title {font-size: 24px;font-weight: bold;text-align: center;margin-bottom: 20px;color: #333;
}/* RadialTreeView will take full width/height of its parent if not constrained */
/* Ensure its container in this page has defined dimensions or it fills the space */
</style>

子组件-创建RadialTreeView

<template><div class="radial-tree-view-container"><div ref="chartRef" class="chart-container"></div></div>
</template><script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch, nextTick } from "vue";
import * as echarts from "echarts/core";
import { TreeChart, TreeSeriesOption } from "echarts/charts";
import {TooltipComponent,TooltipComponentOption,LegendComponent,LegendComponentOption,
} from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";
import type { EChartsOption, ECBasicOption } from "echarts/types/dist/shared";echarts.use([TreeChart, TooltipComponent, LegendComponent, CanvasRenderer]);type ECOption = EChartsOption &ECBasicOption & {series?: TreeSeriesOption | TreeSeriesOption[];} & TooltipComponentOption &LegendComponentOption;interface TreeNode {name: string;id: string;value?: number; // Can be used for sizing or other metricsitemStyle?: {color?: string;borderColor?: string;borderWidth?: number;};label?: {show?: boolean;position?: any;formatter?: string | ((_params: any) => string);color?: string;fontSize?: number;overflow?: string;width?: number;ellipsis?: string;};lineStyle?: {color?: string;width?: number;curveness?: number;};children?: TreeNode[];// Custom propertiesnodeType:| "country"| "category"| "brand"| "product"| "regulation"| "customer"| "patent"| "contributor";isCollapsed?: boolean; // For manual collapse/expand if needed beyond ECharts defaultsymbol?: string; // e.g., 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none' or 'image://path'symbolSize?: number | number[];emphasis?: {focus?: string;scale?: boolean | number;itemStyle?: {color?: string;borderColor?: string;borderWidth?: number;};label?: {color?: string;fontSize?: number;};lineStyle?: {color?: string;width?: number;};};
}const props = defineProps<{chartData: TreeNode;backgroundColor?: string;
}>();const chartRef = ref<HTMLDivElement | null>(null);
let chartInstance: echarts.ECharts | null = null;// --- Style Definitions (based on UI) ---
const COLORS = {country: "#FFD700", // Yellow for country (中心节点)categoryPurple: "#DA70D6", // Purple for main categories (一级节点)brandRed: "#FF6347", // Red for brands (二级节点)productBlue: "#87CEFA", // Light blue for products (二级节点)contributorGreen: "#32CD32", // Green for contributors (特殊二级节点)defaultNode: "#A9A9A9", // Default colorconnector: "#B0B0B0", // Connector line colortextPrimary: "#333333",textSecondary: "#FFFFFF",
};const SYMBOL_SIZES = {country: 80,category: 60,item: 45,contributor: 50,
};function getNodeStyle(node: TreeNode) {let color = COLORS.defaultNode;let symbolSize = SYMBOL_SIZES.item;let labelColor = COLORS.textPrimary;let borderColor = "rgba(0,0,0,0)";let borderWidth = 0;switch (node.nodeType) {case "country":color = COLORS.country;symbolSize = SYMBOL_SIZES.country;labelColor = COLORS.textPrimary; // Text color for country nodebreak;case "category":case "regulation": // Assuming regulation, customer, patent are like categoriescase "customer":case "patent":color = COLORS.categoryPurple;symbolSize = SYMBOL_SIZES.category;labelColor = COLORS.textSecondary; // White text for purple nodesbreak;case "brand":color = COLORS.brandRed;symbolSize = SYMBOL_SIZES.item;labelColor = COLORS.textSecondary;break;case "product":color = COLORS.productBlue;symbolSize = SYMBOL_SIZES.item;labelColor = COLORS.textPrimary; // Darker text for light blue nodesbreak;case "contributor":color = COLORS.contributorGreen;symbolSize = SYMBOL_SIZES.contributor;labelColor = COLORS.textSecondary;borderColor = "#FF4500"; // Special border for contributorborderWidth = 2;break;}return {itemStyle: { color, borderColor, borderWidth },symbolSize,label: {show: true,color: labelColor,fontSize:node.nodeType === "country"? 16: node.nodeType === "category"? 14: 12,formatter: (params: any) => {// Basic word wrapping for long namesconst name = params.name;const maxLength = 10; // Max characters per lineif (name.length > maxLength) {let result = "";for (let i = 0; i < name.length; i += maxLength) {result += name.substring(i, i + maxLength) + "\n";}return result.trim();}return name;},// position: ["center", "center"], // 修改此处为数组形式,避免类型冲突overflow: "break", // Allow breaking lineswidth: symbolSize * 0.8, // Label width related to symbol size},emphasis: {focus: "descendant", // Highlight node and its descendantsscale: 1.1,itemStyle: {borderColor: "#FFF",borderWidth: 2,},label: {fontSize:node.nodeType === "country"? 18: node.nodeType === "category"? 16: 14,},},};
}function processDataForEcharts(node: TreeNode): TreeNode {const style = getNodeStyle(node);const processedNode: TreeNode = {...node,...style, // Apply generated stylessymbol: "circle", // All nodes are circles as per UI};if (node.children && node.children.length > 0) {processedNode.children = node.children.map((child) =>processDataForEcharts(child));}return processedNode;
}const initChart = () => {if (chartRef.value) {chartInstance = echarts.init(chartRef.value);setChartOptions();}
};const setChartOptions = () => {if (!chartInstance || !props.chartData) return;const processedData: any = processDataForEcharts(props.chartData);const option: ECOption = {backgroundColor: props.backgroundColor || "transparent", // Or a light blue like in the UItooltip: {trigger: "item",triggerOn: "mousemove",formatter: (params: any) => {// params.data should be the original TreeNode data before ECharts processing if neededreturn `${params.data.nodeType}: ${params.name}`;},},series: [{type: "tree",data: [processedData],layout: "radial", // Key for radial layoutsymbol: "circle", // Default symbol// symbolSize: (value: any, params: any) => {//   // Dynamic symbol size based on nodeType or value if needed//   // This is now handled by getNodeStyle and processDataForEcharts//   return params.data.symbolSize || 50;// },roam: true, // Allows zoom and panlabel: {position: "inside" as any, // Default label positionverticalAlign: "middle",align: "center",fontSize: 12,},// symbolOffset: [50, "-50%"],edgeSymbol: ["none", "arrow"],edgeSymbolSize: [4, 10],lineStyle: {color: COLORS.connector,width: 1.5,curveness: 0, // Slight curve for aesthetics// 配置箭头:起点无符号,终点用箭头symbol: ["none", "arrow"], // 起始点和结束点的符号symbolSize: [10, 15], // 符号大小(箭头尺寸)},initialTreeDepth: 2, // Expand to 2 levels initially (Country -> Category)expandAndCollapse: true,animationDurationUpdate: 750,emphasis: {// Default emphasis for all nodes, can be overridden per nodefocus: "descendant",scale: true,},// Adjust radial layout parametersradial: {// These might need tweaking to match the UI's spread// radius: ['5%', '80%'], // Inner and outer radius// nodeGap: 30, // Gap between nodes in the same level// layerGap: 150, // Gap between layers},top: "5%",left: "5%",bottom: "5%",right: "5%",},],};chartInstance.setOption(option, true); // true to not merge with previous options
};onMounted(() => {nextTick(() => {initChart();});window.addEventListener("resize", resizeChart);
});onBeforeUnmount(() => {window.removeEventListener("resize", resizeChart);chartInstance?.dispose();
});watch(() => props.chartData,() => {setChartOptions();},{ deep: true }
);watch(() => props.backgroundColor,() => {setChartOptions();}
);const resizeChart = () => {chartInstance?.resize();
};// Expose a resize method if needed by parent
defineExpose({resizeChart,
});
</script><style scoped>
.radial-tree-view-container {width: 100%;height: 100%;min-height: 500px; /* Ensure a minimum height for the chart */
}.chart-container {width: 100%;height: 100%;
}
</style>

mock数据

export interface TreeNode {name: string;id: string;value?: number;itemStyle?: {color?: string;borderColor?: string;borderWidth?: number;};label?: {show?: boolean;position?: string;formatter?: string | ((params: any) => string);color?: string;fontSize?: number;overflow?: "truncate" | "break" | "breakAll";width?: number;ellipsis?: string;};lineStyle?: {color?: string;width?: number;curveness?: number;};children?: TreeNode[];nodeType:| "country"| "category"| "brand"| "product"| "regulation"| "customer"| "patent"| "contributor";isCollapsed?: boolean;symbol?: string;symbolSize?: number | number[];emphasis?: {focus?: "ancestor" | "descendant" | "self" | "none";scale?: boolean | number;itemStyle?: {color?: string;borderColor?: string;borderWidth?: number;};label?: {color?: string;fontSize?: number;};lineStyle?: {color?: string;width?: number;};};
}export const mockRadialData: TreeNode = {name: "美国",id: "usa",nodeType: "country",children: [{name: "客户",id: "customer_category",nodeType: "category",children: [{ name: "10000 客户", id: "customer_1", nodeType: "customer" }, // Simplified, assuming this is a leaf{name: "消费者画像",id: "consumer_profile",nodeType: "category", // Sub-categorychildren: [{ name: "年龄段", id: "age_group", nodeType: "product" }, // Using 'product' type for generic items for now{ name: "学历层次", id: "education_level", nodeType: "product" },{name: "贡献者: 刘先生",id: "contrib_liu",nodeType: "contributor",},],},],},{name: "法规",id: "regulation_category",nodeType: "category",children: [{ name: "10000 法规", id: "regulation_1", nodeType: "regulation" },],},{name: "品牌",id: "brand_main_category",nodeType: "category",children: [{ name: "10000 品牌", id: "brand_count_node", nodeType: "brand" }, // A summary node{name: "品牌A",id: "brand_a",nodeType: "brand",children: [{ name: "品牌形象", id: "brand_a_image", nodeType: "product" },{ name: "品牌故事", id: "brand_a_story", nodeType: "product" },{name: "贡献者: 刘先生",id: "contrib_brand_a_liu",nodeType: "contributor",},],},{name: "品牌B",id: "brand_b",nodeType: "brand",children: [{ name: "品牌定位", id: "brand_b_position", nodeType: "product" },{ name: "目标群体", id: "brand_b_target", nodeType: "product" },],},],},{name: "专利",id: "patent_category",nodeType: "category",children: [{ name: "10000 专利", id: "patent_1", nodeType: "patent" }],},{name: "产品",id: "product_main_category",nodeType: "category",children: [{ name: "10000 产品", id: "product_count_node", nodeType: "product" },{name: "产品1 (USB-C)",id: "product_1",nodeType: "product",children: [{ name: "设计风格", id: "product_1_design", nodeType: "product" },{ name: "充电方式", id: "product_1_charge", nodeType: "product" },{ name: "外观尺寸", id: "product_1_size", nodeType: "product" },{name: "贡献者: 李小姐",id: "contrib_prod_1_li",nodeType: "contributor",},],},{name: "产品2",id: "product_2",nodeType: "product",children: [{ name: "材料", id: "product_2_material", nodeType: "product" },{ name: "重量", id: "product_2_weight", nodeType: "product" },],},],},{name: "价格", // In UI, this looks like a categoryid: "price_category",nodeType: "category",children: [{ name: "10000 价格区间", id: "price_range_1", nodeType: "product" }, // Using 'product' for generic item],},{name: "消费者", // In UI, this looks like a categoryid: "consumer_category_alt",nodeType: "category",children: [{ name: "10000 消费者", id: "consumer_1_alt", nodeType: "customer" },],},],
};

图例显示

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/web/82763.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C# 日志管理功能代码

一、功能概述 本应用通过 AsyncFileLogger 类提供了灵活的日志控制功能&#xff0c;可在运行时通过 UI 界面启用或禁用日志记录。日志系统具有以下特点&#xff1a; 可控制开关&#xff1a;通过按钮随时启用或禁用日志&#xff0c;无需重启应用异步写入&#xff1a;日志记录采…

CSS 性能优化

目录 CSS 性能优化CSS 提高性能的方法1. 选择器优化1.1 选择器性能原则1.2 选择器优化示例 2. 重排&#xff08;Reflow&#xff09;和重绘&#xff08;Repaint&#xff09;优化2.1 重排和重绘的概念2.2 触发重排的操作2.3 触发重绘的操作2.4 优化重排和重绘的方法 3. 资源优化3…

【JJ斗地主-注册安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 暴力破解密码&#xff0c;造成用户信息泄露短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造成亏损无底洞 …

SON.stringify()和JSON.parse()之间的转换

1.JSON.stringify() 作用&#xff1a;将对象、数组转换成字符串 const obj {code: "500",message: "出错了", }; const jsonString JSON.stringify(obj); console.log(jsonString);//"{"code":"Mark Lee","message"…

MongoDB $type 操作符详解

MongoDB $type 操作符详解 引言 MongoDB 是一款流行的开源文档型数据库,它提供了丰富的查询操作符来满足不同的数据查询需求。在 MongoDB 中,$type 操作符是一个非常有用的查询操作符,它允许用户根据文档中字段的类型来查询文档。本文将详细介绍 MongoDB 的 $type 操作符,…

RagFlow优化代码解析(一)

引子 前文写到RagFlow的环境搭建&推理测试&#xff0c;感兴趣的童鞋可以移步&#xff08;RagFlow环境搭建&推理测试-CSDN博客&#xff09;。前文也写过RagFLow参数配置&测试的文档&#xff0c;详见&#xff08;RagFlow环境搭建&推理测试-CSDN博客&#xff09;…

永磁同步电机控制算法--模糊PI转速控制器

一、原理介绍 在常规的PID控制系统的基础上提出了一种模糊PID以及矢量变换方法相结合的控制系统&#xff0c;经过仿真分析对比证明&#xff1a; 模糊PID控制系统能够有效的提高永磁同步电机的转速响应速度&#xff0c;降低转矩脉动&#xff0c;增强了整体控制系统的抗干扰能力…

MySQL基本操作(续)

第3章&#xff1a;MySQL基本操作&#xff08;续&#xff09; 3.3 表操作 表是关系型数据库中存储数据的基本结构&#xff0c;由行和列组成。在MySQL中&#xff0c;表操作包括创建表、查看表结构、修改表和删除表等。本节将详细介绍这些操作。 3.3.1 创建表 在MySQL中&#…

探索未知惊喜,盲盒抽卡机小程序系统开发新启航

在消费市场不断追求新鲜感与惊喜体验的当下&#xff0c;盲盒抽卡机以其独特的魅力&#xff0c;迅速成为众多消费者热衷的娱乐与消费方式。我们紧跟这一潮流趋势&#xff0c;专注于盲盒抽卡机小程序系统的开发&#xff0c;致力于为商家和用户打造一个充满趣味与惊喜的数字化平台…

89.实现添加收藏的功能的后端实现

实现完查看收藏列表之后&#xff0c;实现的是添加收藏的功能 我的设想是&#xff1a;在对话界面中&#xff0c;如果用户认为AI的回答非常好&#xff0c;可以通过点击该回答对应的气泡中的图标&#xff0c;对该内容进行添加 所以后端实现为&#xff1a; service类中添加&…

OD 算法题 B卷【猴子吃桃】

文章目录 猴子吃桃 猴子吃桃 猴子喜欢吃桃&#xff0c;桃园有N棵桃树&#xff0c;第i棵桃树上有Ni个桃&#xff0c;看守将在H(>N)小时后回来&#xff1b;猴子可以决定吃桃的速度K(个/小时)&#xff0c;每个小时他会选择一棵桃树&#xff0c;从中吃掉K个桃&#xff0c;如果这…

ubuntu 端口复用

需求描述&#xff1a;复用服务器的 80端口&#xff0c;同时处理 ssh 和 http 请求&#xff0c;也就是 ssh 连接和 http 访问服务器的时候都可以指定 80 端口&#xff0c;然后服务器可以正确分发请求给 ssh 或者 http。 此时&#xff0c;ssh 监听的端口为 22&#xff0c;而 htt…

Hive中ORC存储格式的优化方法

优化Hive中的ORC(Optimized Row Columnar)存储格式可显著提升查询性能、降低存储成本。以下是详细的优化方法,涵盖参数配置、数据组织、写入优化及监控调优等维度: 一、ORC核心参数优化 1. 存储与压缩参数 SET orc.block.size=268435456; -- 块大小(默认256MB)…

Vim 设置搜索高亮底色

在 Vim 中&#xff0c;默认搜索命中会高亮显示&#xff0c;方便用户快速定位关键字。但有些用户希望自定义搜索匹配的底色或前景色&#xff0c;以适应不同的配色方案或提高可读性。本文将详细介绍如何修改 Vim 的搜索高亮颜色。 一、Vim 搜索高亮机制 Vim 用内置的高亮组&…

【计算机网络】非阻塞IO——poll实现多路转接

&#x1f525;个人主页&#x1f525;&#xff1a;孤寂大仙V &#x1f308;收录专栏&#x1f308;&#xff1a;计算机网络 &#x1f339;往期回顾&#x1f339;&#xff1a;【计算机网络】非阻塞IO——select实现多路转接 &#x1f516;流水不争&#xff0c;争的是滔滔不息 一、…

vscode使用系列之快速生成html模板

一.欢迎来到我的酒馆 vscode&#xff0c;yyds! 目录 一.欢迎来到我的酒馆二.vscode下载安装1.关于vscode你需要知道2.开始下载安装 三.vscode快速创建html模板 二.vscode下载安装 1.关于vscode你需要知道 Q&#xff1a;为什么使用vscode? A&#xff1a;使用vscode写…

【C/C++】不同防止头文件重复包含的措施

文章目录 #pragma once vs #ifndef 文件宏1 原理层面区别&#xff08;core&#xff09;2 关键区别与优缺点分析3 总结与最佳实践 #pragma once vs #ifndef 文件宏 在 C/C 中&#xff0c;#pragma once 和传统的文件宏守卫 (#ifndef HEADER_H #define HEADER_H ... #endif) 都用…

java-springboot文件上传校验之只允许上传excel文件,且检查不能是脚本或者有害文件或可行性文件

四重验证机制&#xff1a; 文件扩展名检查&#xff08;.xlsx/.xls&#xff09;MIME类型检查文件魔数验证&#xff08;真实文件类型&#xff09;可执行文件特征检测 防御措施&#xff1a; 使用try-with-resources确保流关闭限制文件大小防止DoS攻击使用Apache POI的FileMagic进…

不确定性分析在LEAP能源-环境系统建模中的整合与应用

本内容突出与实例结合&#xff0c;紧密结合国家能源统计制度及《省级温室气体排放编制指南》&#xff0c;深入浅出地介绍针对不同级别研究对象时如何根据数据结构、可获取性、研究目的&#xff0c;构建合适的能源生产、转换、消费、温室气体排放&#xff08;以碳排放为主&#…

高性能分布式消息队列系统(四)

八、客户端模块的实现 客户端实现的总体框架 在 RabbitMQ 中&#xff0c;应用层提供消息服务的核心实体是 信道&#xff08;Channel&#xff09;。 用户想要与消息队列服务器交互时&#xff0c;通常不会直接操作底层的 TCP 连接&#xff0c;而是通过信道来进行各种消息的发布…