antv g6决策树

antv g6决策树
在这里插入图片描述

<template>
  <div id="container"></div>
</template>

<script>
import G6 from "@antv/g6";

export default {
  name: "index",
  mounted() {
    // console.log(777777778889);
//  this.treedata=    JSON.parse(JSON.stringify(this.treedata).replace(/"name"/g,'"label"'))
//   this.treedata=   JSON.parse(JSON.stringify(this.treedata).replace(/"sub_folder"/g,'"children"'))

    let backgroundColor = 'rgba(255,230,230,0.88)'
    let fontColor = 'rgb(0,0,0)'
    let trueincreaseColor = 'rgb(111,255,115)'
    let increaseFontColor = 'rgb(255,255,255)'
    let falseincreaseColor = 'rgb(255,0,0)'
    let circleColor = 'rgba(17,28,236,0.85)'
    let lineColor = 'rgba(83,124,248,0.85)'
    this.init(backgroundColor, fontColor, trueincreaseColor, falseincreaseColor, circleColor, lineColor, increaseFontColor);
  },
  // props:["treedata"],

  methods: {
    init(color, fontColor, trueincreaseColor, falseincreaseColor, circleColor, lineColor, increaseFontColor) {

// root node
      G6.registerNode('root', {
        draw: (cfg, group) => {
          const size = [80, 30];
          const keyShape = group.addShape('rect', {
            attrs: {
              width: size[0],
              height: size[1],
              x: -size[0] / 2,
              y: -size[1] / 2,
              fill: color,
              radius: 5
            },
            draggable: true,
            name: 'root-keyshape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.ratio}%`,
              fill: fontColor,
              fontSize: 6,
              x: 10 - size[0] / 2,
              y: 3,
            },
            draggable: true,
            name: 'ratio-shape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.label}`,
              fill: fontColor,
              fontSize: 9,
              x: -6,
              y: 0,
            },
            draggable: true,
            name: 'label-shape'
          });
          group.addShape('line', {
            attrs: {
              x1: -6,
              x2: 35,
              y1: 2,
              y2: 2,
              stroke: 'rgba(255, 255, 255, 0.85)',
              lineWidth: 0.5
            },
            draggable: true,
            name: 'divider-shape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.subLabel}`,
              fill: fontColor,
              fontSize: 6,
              x: -6,
              y: 10,
            },
            draggable: true,
            name: 'sublabel-shape'
          });
          return keyShape;
        }
      });

// level1 node
      G6.registerNode('level1node', {
        draw: (cfg, group) => {
          const size = [60, 40]
          const keyShape = group.addShape('rect', {
            attrs: {
              width: size[0],
              height: size[1],
              x: -size[0] / 2,
              y: -size[1] / 2,
              fill: color,
              radius: 5
            },
            draggable: true,
            name: 'level1node-keyshape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.label}`,
              fill: fontColor,
              fontSize: 6,
              x: 0,
              y: -6,
              textAlign: 'center'
            },
            draggable: true,
            name: 'label-shape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.subLabel}`,
              fill: fontColor,
              fontSize: 8,
              x: 0,
              y: 6,
              fontWeight: 800,
              textAlign: 'center'
            },
            draggable: true,
            name: 'sublabel-shape'
          });
          group.addShape('rect', {
            attrs: {
              x: -12,
              y: 8,
              width: 25,
              height: 8,
              radius: 4,
              fill: cfg.increase ? trueincreaseColor : falseincreaseColor
            },
            draggable: true,
            name: 'ratio-box',
          })
          group.addShape('text', {
            attrs: {
              text: `${cfg.ratio}%`,
              fill: increaseFontColor,
              fontSize: 6,
              x: 0,
              y: 9,
              textAlign: 'center',
              textBaseline: 'top'
            },
            draggable: true,
            name: 'ratio-shape'
          });
          // edge end
          group.addShape('line', {
            attrs: {
              x1: -size[0] / 2,
              x2: -size[0] / 2 + 6,
              y1: 0,
              y2: 0,
              lineWidth: 1,
              stroke: lineColor,
            }
          });
          group.addShape('circle', {
            attrs: {
              r: 2,
              x: -size[0] / 2 + 6,
              y: 0,
              fill: circleColor,
            }
          })
          return keyShape;
        },
        update: undefined,
      }, 'rect')

// other node
      G6.registerNode('othernode', {
        draw: (cfg, group) => {
          const size = [100, 30];
          const keyShape = group.addShape('rect', {
            attrs: {
              width: size[0],
              height: size[1],
              x: -size[0] / 2,
              y: -size[1] / 2,
              fill: color,
              radius: 5
            },
            draggable: true,
            name: 'level1node-keyshape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.label}`,
              fill: fontColor,
              fontSize: 6,
              x: 10 - size[0] / 2,
              y: -2,
              textAlign: 'left'
            },
            draggable: true,
            name: 'label-shape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.subLabel}`,
              fill: fontColor,
              fontSize: 8,
              fontWeight: 800,
              x: 10 - size[0] / 2,
              y: 8,
              textAlign: 'left'
            },
            draggable: true,
            name: 'sublabel-shape'
          });
          group.addShape('rect', {
            attrs: {
              x: 12,
              y: -4,
              width: 25,
              height: 8,
              radius: 4,
              fill: cfg.increase ? trueincreaseColor : falseincreaseColor
            },
            draggable: true,
            name: 'ratio-box'
          })
          group.addShape('text', {
            attrs: {
              text: `${cfg.ratio}%`,
              fill: increaseFontColor,
              fontSize: 6,
              x: 18,
              y: -3,
              textAlign: 'left',
              textBaseline: 'top'
            },
            draggable: true,
            name: 'ratio-shape'
          });

          // edge end
          group.addShape('line', {
            attrs: {
              x1: -size[0] / 2,
              x2: -size[0] / 2 + 6,
              y1: 0,
              y2: 0,
              lineWidth: 1,
              stroke: lineColor,
            }
          });
          group.addShape('circle', {
            attrs: {
              r: 2,
              x: -size[0] / 2 + 6,
              y: 0,
              fill: circleColor,
            }
          })
          return keyShape;
        },
        update: undefined
      }, 'rect')

// edge
      G6.registerEdge('round-poly', {
        getControlPoints: (cfg) => {
          const {startPoint, endPoint} = cfg;
          return [
            startPoint,
            {
              x: startPoint.x,
              y: endPoint.y
            },
            endPoint
          ];
        }
      }, 'polyline')

      const data = {
        id: 'root',
        label: '利息收入',
        subLabel: '3,283.456',
        ratio: 3,
        children: [
          {
            id: 'child-a',
            label: '平均利息',
            subLabel: '9%',
            ratio: 1,
            increase: true,
            children: [
              {
                id: 'child-a-1',
                label: '平均利息1',
                subLabel: '9%',
                ratio: 1,
                increase: true,
                children: [
                  {
                    id: 'child-a-1-1',
                    label: '平均利息1-1',
                    subLabel: '9%',
                    ratio: 1,
                    increase: true,
                  },
                  {
                    id: 'child-a-1-2',
                    label: '平均利息1-2',
                    subLabel: '9%',
                    ratio: 1,
                    increase: true,
                  }]
              },
              {
                id: 'child-a-2',
                label: '平均利息2',
                subLabel: '9%',
                ratio: 1,
                increase: true,
              }]
          }, {
            id: 'child-b',
            label: '贷款余额',
            subLabel: '1,789,567',
            ratio: 23,
            increase: true,
            children: [{
              id: 'child-b-a',
              label: '投放金额',
              subLabel: '2,385,124',
              ratio: 17,
              increase: true,
              operator: '-',
            }, {
              id: 'child-b-b',
              label: '还款金额',
              subLabel: '595,557',
              ratio: 12,
              increase: true,
            }
            ]
          }, {
            id: 'child-c',
            label: '还款期限',
            subLabel: '7',
            ratio: 23,
            increase: true,
          }
        ]
      };

      G6.Util.traverseTree(data, subtree => {
        if (subtree.level === undefined) subtree.level = 0;
        // subtree.children?.forEach(child => child.level = subtree.level + 1);
        if (subtree.children && subtree.children.forEach) {
          subtree.children.forEach(child => {
            if (child) {
              child.level = subtree.level + 1;
            }
          });
        }

        switch (subtree.level) {
          case 0:
            subtree.type = 'root';
            break;
          case 1:
            subtree.type = 'level1node';
            break;
          default:
            subtree.type = 'othernode';
        }
      });

      const width = container.scrollWidth;
      const height = (container.scrollHeight || 500) - 30;
      const graph = new G6.TreeGraph({
        container: 'container',
        width,
        height,
        fitView: true,
        layout: {
          type: 'compactBox',
          direction: 'LR',
          getHGap: function getVGap() {
            return 5;
          },
        },
        defaultEdge: {
          type: 'round-poly',
          sourceAnchor: 0,
          targetAnchor: 1,
          style: {
            radius: 8,
            stroke: lineColor
          }
        },
        defaultNode: {
          anchorPoints: [
            [0.9, 0.5],
            [0, 0.5]
          ]
        },
        nodeStateStyles: {
          hover: {
            fill: '#fff',
            shadowBlur: 30,
            shadowColor: '#ddd',
          },
          operatorhover: {
            'operator-box': {
              opacity: 1
            }
          }
        },
        modes: {
          default: ['zoom-canvas', 'drag-canvas', 'collapse-expand']
        }
      });

      graph.on('node:mouseenter', e => {
        // if (e.target.get('name')?.includes('operator')) {
        if (e.target && e.target.get && e.target.get('name') && e.target.get('name').includes('operator')) {

          graph.setItemState(e.item, 'operatorhover', true);
        } else {
          graph.setItemState(e.item, 'hover', true);
        }
      })
      graph.on('node:mouseleave', e => {
        graph.setItemState(e.item, 'operatorhover', false);
        graph.setItemState(e.item, 'hover', false);
      });

      graph.data(data);
      graph.render();

    },
  },
};
</script>


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ANTV G6是一款智能电视机顶盒,它不仅可以通过网络观看在线视频,还提供了离线文档的功能。 ANTV G6的离线文档功能允许用户在没有网络连接的情况下访问和浏览本地存储的文档文件。用户可以通过USB接口或者其他存储设备将文档文件导入到G6中,然后在离线状态下查看和编辑这些文档。 离线文档功能使得用户可以在没有网络的情况下继续处理文档工作,提高了工作和学习的效率。无论是在出差、旅行或者没有网络覆盖的地方,用户都可以依靠ANTV G6来查看和编辑文档,保证工作的顺利进行。 ANTV G6不仅支持常见的文档格式,如Word、Excel和PowerPoint,还支持PDF、TXT等多种格式。用户可以通过文件管理系统在G6中浏览并打开各种格式的文件,进行阅读、编辑和保存。 此外,ANTV G6的离线文档功能还提供了一些便捷的操作选项,如快速搜索、书签、阅读模式等。用户可以根据自己的需求和喜好进行设置,提高使用的便捷性和个性化。 总之,ANTV G6的离线文档功能为用户提供了更加便利和灵活的文档处理方式,无论是在有网络的情况下,还是在没有网络的环境中,用户都可以通过G6进行文档工作,提升工作和学习的效率。 ### 回答2: ANTV G6是一款智能电视盒子,它可以通过连接到网络收看各种在线视频内容。但是,ANTV G6也支持离线播放,即使在没有网络连接的情况下,用户也可以通过存储在盒子内部的离线文档来观看内容。 离线文档功能使得用户可以在没有网络时仍然享受丰富的内容。用户可以下载电影、电视剧、纪录片等视频文件,并将其保存在ANTV G6的存储空间内。用户还可以下载各种格式的音乐文件,创建自己的音乐库。此外,用户还可以下载电子书、漫画等文档文件,并通过ANTV G6的离线文档功能进行阅读。 ANTV G6的离线文档功能具有很高的灵活性和可定制性。用户可以根据自己的口味和需求,选择并下载自己喜欢的内容。而且,用户可以通过文件管理器轻松管理和排序这些离线文档,让自己的媒体库更加整洁有序。 总的来说,ANTV G6的离线文档功能为用户提供了便捷的娱乐方式。无论是在没有网络的地方、网络出现故障或者想要隐私地享受内容,用户都可以通过离线文档功能满足自己的需求。ANTV G6的离线文档功能成为用户对于这款智能电视盒子的一个重要选择因素。 ### 回答3: ANTV G6是一款智能电视盒子,它可以连接到电视并提供多种功能。ANTV G6支持离线文档功能,这意味着用户可以在没有网络连接的情况下访问和查阅文档。 离线文档功能非常实用,特别是在没有网络连接或网络不稳定的情况下。用户可以事先下载需要的文档,然后在没有网络时随时打开并查看。这样,即使用户没有互联网,也可以方便地查阅重要资料或文件。 ANTV G6的离线文档功能支持多种文档格式,如PDF、DOC、PPT等常见格式。用户可以通过安装相应的文档阅读器应用程序来打开和阅读这些文档。除了查看文档外,用户还可以进行一些基本的操作,如搜索、添加书签、标注等。 另外,ANTV G6的离线文档功能还提供了快速跳转和目录导航等便捷功能,使得用户能够更方便地浏览和定位到所需的内容。此外,ANTV G6还支持对文档进行分享,用户可以将文档分享给其他设备或其他用户。 总的来说,ANTV G6的离线文档功能为用户提供了便捷的文档访问和查阅体验。无论用户身处何地,只要有ANTV G6,就能够随时打开和浏览各种文件。这对于那些需要频繁查阅文档并且经常处于没有网络连接的环境中的用户来说,无疑是一项非常有用且实用的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值