echarts实现树图-tree

一、效果图

二、实现

1、数据

2、数据处理

3、样式处理

var colors = ['#00ADD0', '#FFA12F', '#B62AFF', '#604BFF', '#95F300'];

// 样式
function handleStyles(data: any, index: any, color = 'rgb(46, 135, 239)') {
  //index标识第几层
  return data.map((item: any, index2: any) => {
    //计算出第1级颜色
    if (index == 0) {
      color = 'rgb(46, 135, 239)';
    }
    if (index == 1) {
      // 5个二级  自己下级 相同颜色
      color = colors.find((item, eq) => eq == index2 % 5);
    }
    // 第三级继承父级第二级颜色  所以参数color  也可以在自定义
    // if (index == 2) { }

    // 设置节点大小
    if (index === 0 || index === 1) {
      item.label = {
        position: 'inside',
      };
    } else {
      // 三级
      item.label = {
        position: 'bottom', // 1 inside  outside
        //2 以像素为单位,第一个参数为x轴方向,
        // 第二个参数为y轴方向,左上角为起点,向右向下为正数,向上向左为负数。
        // position: [10, 60],

        // 3 以百分比为单位,注意百分比需要用引号包裹,第一个参数为x轴方向,第二个参数为y轴方向,左上角为起点,向右向下为正数,向上向左为负数。
        // position:['2%', '5%'],

        color: '#000',

        // rotate: 0,
        //   borderRadius: "50%",
      };
    }
    // 设置节点大小 size
    switch (index) {
      case 0:
        item.symbolSize = 150;
        break;
      case 1:
        item.symbolSize = 90;
        break;
      case 2:
        item.symbolSize = 50;
        break;
      default:
        item.symbolSize = 40;
        break;
    }

    // 设置线条颜色
    item.lineStyle = { color: color };

    if (item.children) {
      //存在子节点
      item.itemStyle = {
        borderColor: color,
        color: color,
      };
      item.children = handleStyles(item.children, index + 1, color);
    } else {
      //不存在
      item.itemStyle = {
        // color: 'transparent',
        color: color,
        borderColor: color,
      };
    }
    return item;
  });
}

4、echarts的option配置

{
    type: 'tree', // tree
    // backgroundColor: '#000',// 整个树图谱背景色  默认白色
    tooltip: {
      //提示框  // 触发方式 mousemove, click, none, mousemove|click
      triggerOn: 'mousemove',
      // item 图形触发, axis 坐标轴触发, none 不触发 // triggerOn: 'click',
      trigger: 'item',

      formatter: (params: any) => {
        return params.name; // 可以<div></div>
      },
    },
    series: [
      {
        type: 'tree',
        hoverAnimation: true, //hover样式  true
        data: treeData.value, //
        // 整个图谱位置比例
        top: 30, //0
        bottom: 60, // 0
        left: 0,
        right: 0,
        // 布局
        layout: 'radial',
        symbol: 'circle',
        symbolSize: 10, // 节点大小
        //
        // nodePadding的数组值为[10, 20],表示第一层节点与第二层节点之间的水平间距为10,垂直间距为20。
        nodePadding: 20, // 20
        animationDurationUpdate: 550, // 动画过渡时间  毫秒
        expandAndCollapse: false, //子树折叠和展开的交互,默认打开
        initialTreeDepth: 2, // 设置树状图的初始展示层数
        roam: true, //是否开启鼠标缩放和平移漫游。scale/move/true
        focusNodeAdjacency: true, //
        //节点样式
        itemStyle: {
          borderWidth: 1,
          // borderColor
          // color
        },
        // 节点内文字标签
        label: {
          // show: false, // 控制展示

          //标签样式
          // color: '#000',
          color: '#fff',

          fontSize: 14,

          position: 'inside', // outside
          rotate: 0, // 倾斜

          // 默认展示 不写也是params.name
          formatter: (params: any) => {
            //   不可用div
            return params.name;
          },
        },
        // 线样式
        lineStyle: {
          width: 1,
          curveness: 0.5,
        },
      },
    ],
  };

三、参考

1、关系图

echarts图表集

2、位置配置

ECharts 中设置position配置项所在位置(外部,内部,自定义位置)_echarts position_咸鱼!的博客-优快云博客

### uni-app 中使用 ECharts 创建折线树图 在 `uni-app` 中集成并使用 ECharts 实现折线树图主要涉及几个方面的工作:安装依赖库、初始化图表实例以及配置数据源。 #### 安装 echarts-for-weixin 组件 为了能够在小程序环境中顺利运行 ECharts 图表,推荐使用专门适配微信小程序环境的版本——echarts-for-weixin (efw)[^1]。通过 npm 或者手动下载的方式引入该组件到项目当中。 #### 初始化页面布局结构 定义好 HTML 结构,在合适的位置预留出用于展示图表的空间容器,并设置固定的高度宽度以便后续渲染图形时能够正常显示[^2]。 ```html <template> <view class="charts-box"> <canvas canvas-id="treeChart" id="treeChart"></canvas> </view> </template> <style scoped> .charts-box { width: 750rpx; height: 600rpx; } </style> ``` #### 加载 efw 库文件 & 配置 chart option 确保已经成功加载了必要的 JavaScript 文件之后,就可以按照官方文档说明来编写相应的选项参数对象 treeOption 来描述想要绘制的具体样式效果了[^3]。 ```javascript import * as wx from 'weapp-adapter'; import * as echarts from '/components/ec-canvas/echarts'; export default { data() { return { ec: {} }; }, onReady() { this.initTreeChart(); }, methods: { initTreeChart() { const query = uni.createSelectorQuery().in(this); query.select('#treeChart').fields({ node: true, size: true }).exec((res) => { let canvas = res[0].node; let ctx = canvas.getContext('2d'); // Initialize the instance of ECharts with given DOM element. var myChart = echarts.init(canvas, null, { renderer: 'canvas' }); // Specify the configuration items and data for the chart to be displayed here... var treeOption = { title: { text: 'A Tree Chart Example' }, series : [ { type: "lines", coordinateSystem: 'cartesian2d', lineStyle: { color: '#5470C6', opacity: 0.9, width: 2 } } ] }; // Set options after initialization is complete. myChart.setOption(treeOption); // Keep a reference so that it can be updated later if needed. this.ec.chart = myChart; }); } } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值