vue3使用echarts的tree,自己写事件进行分页
先到npmjs 官网查看当前使用最多的版本
https://www.npmjs.com/package/echarts
看了下5.5.0用的最多
npm i [email protected]
以下的demo(“@/flare”是后面的flare.json数据 )
<template> <div id="chart-container"> </div> </template> <script> import { defineComponent,setup,reactive,onMounted } from 'vue' import * as echarts from 'echarts' import * as flarejson from '@/flare' export default defineComponent({ setup(props,ctx){ let myChart; let echartsoption=reactive([]) const getChartData = ()=>{ let dom = document.getElementById('chart-container'); myChart = echarts.init(dom, null, { renderer: 'canvas', useDirtyRect: false }); } myChart.showLoading(); //隐藏单数的节点 flarejson.children.forEach(function (datum, index) { index % 2 === 0 && (datum.collapsed = true); }); //调用 echartsoption={ tooltip: { trigger: 'item', triggerOn: 'mousemove' }, series: [ { type: 'tree', data: [flarejson], top: '1%', left: '7%', bottom: '1%', right: '10%', symbolSize: 20, //圆的大小 label: { //主节点默认默认配置,也可以在data里面自定义赋值 position: 'left', verticalAlign: 'middle', align: 'right', fontSize: 16 }, leaves: { //子节点相关默认配置 label: { position: 'right', verticalAlign: 'middle', align: 'left' } }, emphasis: { focus: 'descendant' }, expandAndCollapse: true, animationDuration: 550, animationDurationUpdate: 750, tooltip:{ formatter:function(params,ticket,callback){ // $.get('detail?name=' + params.name, function (content) { // callback(ticket, toHTML(content)); // }); // setTimeout(() => { // callback(ticket, 'test setTimeout formatter'); // }, 2000); return 'Loading formatter'; //优先加载 }, valueFormatter:function(params,dataIndex){ //数值显示部分的格式化回调函数 // $.get('detail?name=' + params.name, function (content) { // callback(ticket, toHTML(content)); // }); // setTimeout(() => { // callback(ticket, 'test setTimeout formatter'); // }, 2000); return '$' + value; //优先加载 }, }, } ] } if (echartsoption && typeof echartsoption === 'object') { myChart.setOption(echartsoption); } window.addEventListener('resize', myChart.resize); myChart.on("click", handleNodeClick);//点击事件 } const handleNodeClick = (node)=>{ console.warn({ node }); if(node.data.valuetype){ //下一页 if(node.data.valuetype =='nextpage'){ let parentNode= node.treeAncestors(node.treeAncestors.length-2) //treeAncestors 存储的当前点击的链路节点,我们用来获取父节点 const curData = myChart.getOption().series[0].data; //模拟已经取到下一页的数据 let childrenData=[ {"name": "上一页","valuetype": "lastpage", "value": "lastpage"}, {"name": "11", "value": 11}, ] matchNodeData(curData[0],parentNode,childrenData) echartsoption.series[0].data = curData; renderEcharts() }else if(node.data.valuetype =='lastpage'){ //上一页 //模拟已经取到下一页的数据 let childrenData=[ {"name": "1", "value": 1}, {"name": "2", "value": 2}, {"name": "3", "value": 3}, {"name": "4", "value": 4}, {"name": "5", "value": 5}, {"name": "下一页","valuetype": "nextpage", "value": "nextpage"}, ] matchNodeData(curData[0],parentNode,childrenData) echartsoption.series[0].data = curData; renderEcharts() } } else if (node.data.children && node.data.children.length) { // 已有下级 //node.collapsed = node.collapsed; } else { myChart.showLoading(); setTimeout(() => { myChart.hideLoading(); //下面数据为自己想要加入的数据 const newNode = [{ name: "哪吒三太子1", value: node.dataIndex+1, },{ name: "哪吒三太子2", value: node.dataIndex+2, }] if (!node.data.children) { node.data.children =newNode; } else if (!node.data.children.length) { node.data.children.push(...newNode); } node.data.collapsed=false //展开 //节点的数据赋值给echartsoption const curData = myChart.getOption().series[0].data; echartsoption.series[0].data = curData; renderEcharts(); }, 300); } } //刷新节点 const renderEcharts =()= { myChart.setOption(echartsoption); myChart.resize(); } const matchNodeData =(node,parentNode,childrenData)= { //只能取到name,value这些自带的字段,所以如果有分页,那这个的父节点一定要设置value if(node.value==parentNode.value){ node.children=childrenData return true } if (node.data.children && node.data.children.length){ for(let count=0;count<node.children.length;count++){ if(matchNodeData(node.children[count],parentNode,childrenData)){ //递推继续找子节点 return true } } } } onMounted(()=>{ getChartData() }) return { myChart, echartsoption, getChartData, handleNodeClick, renderEcharts, matchNodeData, } }); </script>
测试的flare.json 数据
{ "name": "flare", "value": 1, "children": [ { "name": "analytics", "children": [ { "name": "cluster", "children": [ {"name": "AgglomerativeCluster", "value": 3938}, {"name": "CommunityStructure", "value": 3812}, {"name": "HierarchicalCluster", "value": 6714}, {"name": "MergeEdge", "value": 743} ] }, { "name": "graph", "children": [ {"name": "BetweennessCentrality", "value": 3534}, {"name": "LinkDistance", "value": 5731}, {"name": "MaxFlowMinCut", "value": 7840}, {"name": "ShortestPaths", "value": 5914}, {"name": "SpanningTree", "value": 3416} ] }, { "name": "optimization", "children": [ {"name": "AspectRatioBanker", "value": 7074} ] } ] }, { "name": "data", "value": "11224", "children": [ { "name": "converters", "children": [ {"name": "Converters", "value": 721}, {"name": "DelimitedTextConverter", "value": 4294}, {"name": "GraphMLConverter", "value": 9800}, {"name": "IDataConverter", "value": 1314}, {"name": "JSONConverter", "value": 2220} ] }, {"name": "DataField", "value": 1759}, {"name": "DataSchema", "value": 2165}, {"name": "DataSet", "value": 586}, {"name": "DataSource", "value": 3331}, {"name": "DataTable", "value": 772}, {"name": "DataUtil", "value": 3322} ] }, { "name": "下一页","valuetype": "nextpage", "value": "nextpage" } ] }
标签:node,name,tree,myChart,value,children,vue3,data,echarts From: https://www.cnblogs.com/zhian/p/18158460