|
|
@@ -0,0 +1,252 @@
|
|
|
+<template>
|
|
|
+ <div class="ng-network-chart">
|
|
|
+ <ul class="ng-network-keyboard" aria-label="Loaded network members">
|
|
|
+ <li v-for="(member, index) in members" :key="member.USER_ID">
|
|
|
+ <button v-if="!member.leaf" type="button" @click="activateNode(member, index + 1)">
|
|
|
+ Expand or collapse {{ member.USER_NAME }} · {{ member.REAL_NAME }}
|
|
|
+ </button>
|
|
|
+ <span v-else>{{ member.USER_NAME }} · {{ member.REAL_NAME }}</span>
|
|
|
+ </li>
|
|
|
+ </ul>
|
|
|
+ <div ref="viewport" class="ng-network-chart-scroll" tabindex="0" role="region" aria-label="Sponsor Network Ex. Drag to pan, scroll to zoom.">
|
|
|
+ <div ref="canvas" :style="{ width: chartWidth + 'px', height: chartHeight + 'px' }" />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import * as echarts from 'echarts/core'
|
|
|
+import { TreeChart } from 'echarts/charts'
|
|
|
+import { TooltipComponent } from 'echarts/components'
|
|
|
+import { CanvasRenderer } from 'echarts/renderers'
|
|
|
+
|
|
|
+echarts.use([TreeChart, TooltipComponent, CanvasRenderer])
|
|
|
+
|
|
|
+// 仅调整新图的横向间距,避免 ECharts 默认把不同分支的间距加倍。
|
|
|
+echarts.registerLayout(2000, model => {
|
|
|
+ model.eachSeriesByType('tree', series => {
|
|
|
+ if (series.id !== 'ng-sponsor-network-ex') return
|
|
|
+ const root = series.getData().tree.root.children[0]
|
|
|
+ let column = 0
|
|
|
+ const place = node => {
|
|
|
+ const children = node.isExpand ? node.children : []
|
|
|
+ children.forEach(place)
|
|
|
+ const x = children.length
|
|
|
+ ? (children[0].getLayout().x + children[children.length - 1].getLayout().x) / 2
|
|
|
+ : column++ * 216
|
|
|
+ node.setLayout({ x }, true)
|
|
|
+ }
|
|
|
+ place(root)
|
|
|
+ const offset = (series.layoutInfo.width - (column - 1) * 216) / 2
|
|
|
+ root.eachNode(node => {
|
|
|
+ if (node.getLayout()) node.setLayout({ x: node.getLayout().x + offset }, true)
|
|
|
+ })
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+export default {
|
|
|
+ name: 'SponsorNetworkChart',
|
|
|
+ props: {
|
|
|
+ treeData: { type: Object, required: true }
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return { viewportWidth: 0 }
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ layout() {
|
|
|
+ const members = []
|
|
|
+ let leaves = 0
|
|
|
+ let depth = 1
|
|
|
+ const visit = (node, level) => {
|
|
|
+ members.push(node)
|
|
|
+ depth = Math.max(depth, level)
|
|
|
+ if (node.children && node.children.length) node.children.forEach(child => visit(child, level + 1))
|
|
|
+ else leaves++
|
|
|
+ }
|
|
|
+ visit(this.treeData, 1)
|
|
|
+ return { members, leaves, depth }
|
|
|
+ },
|
|
|
+ members() {
|
|
|
+ return this.layout.members
|
|
|
+ },
|
|
|
+ chartWidth() {
|
|
|
+ return Math.max(this.viewportWidth, (this.layout.leaves - 1) * 216 + 224)
|
|
|
+ },
|
|
|
+ chartHeight() {
|
|
|
+ return (this.layout.depth - 1) * 172 + 140
|
|
|
+ }
|
|
|
+ },
|
|
|
+ watch: {
|
|
|
+ treeData: { deep: true, handler: 'draw' }
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.chart = echarts.init(this.$refs.canvas)
|
|
|
+ this.chart.on('click', this.handleClick)
|
|
|
+ this.draw()
|
|
|
+ if (window.ResizeObserver) {
|
|
|
+ this.resizeObserver = new window.ResizeObserver(this.resizeChart)
|
|
|
+ this.resizeObserver.observe(this.$refs.viewport)
|
|
|
+ }
|
|
|
+ window.addEventListener('resize', this.resizeChart)
|
|
|
+ },
|
|
|
+ beforeDestroy() {
|
|
|
+ if (this.resizeObserver) this.resizeObserver.disconnect()
|
|
|
+ window.removeEventListener('resize', this.resizeChart)
|
|
|
+ this.chart.dispose()
|
|
|
+ this.chart = null
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ resizeChart() {
|
|
|
+ this.viewportWidth = this.$refs.viewport.clientWidth
|
|
|
+ this.$nextTick(() => {
|
|
|
+ if (this.chart) {
|
|
|
+ this.chart.resize()
|
|
|
+ this.centerRoot()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ draw() {
|
|
|
+ this.viewportWidth = this.$refs.viewport.clientWidth
|
|
|
+ this.$nextTick(() => {
|
|
|
+ if (!this.chart) return
|
|
|
+ this.chart.resize()
|
|
|
+ const rich = {
|
|
|
+ code: { color: '#1683a3', fontSize: 11, fontWeight: 700, width: 164, lineHeight: 22, align: 'left' },
|
|
|
+ member: { fontSize: 13, fontWeight: 700, width: 164, lineHeight: 24, align: 'left' },
|
|
|
+ rank: { color: '#202259', backgroundColor: '#edf1f5', borderRadius: 8, padding: [2, 5], fontSize: 10, align: 'left' },
|
|
|
+ period: { color: '#606266', backgroundColor: '#edf1f5', borderRadius: 8, padding: [2, 5], fontSize: 10, align: 'left' },
|
|
|
+ gap: { width: 5, align: 'left' },
|
|
|
+ // 右对齐定位整个标记,自动字宽和对称内边距让符号保持居中。
|
|
|
+ toggle: { color: '#fff', backgroundColor: '#1683a3', borderRadius: 9, height: 18, padding: [0, 5], align: 'right', fontSize: 13 },
|
|
|
+ leaf: { color: '#606266', backgroundColor: '#e4e9ee', borderRadius: 9, height: 18, padding: [0, 7], align: 'right' }
|
|
|
+ }
|
|
|
+ this.chart.setOption({
|
|
|
+ textStyle: { fontFamily: window.getComputedStyle(this.$el).fontFamily },
|
|
|
+ tooltip: {
|
|
|
+ trigger: 'item',
|
|
|
+ confine: true,
|
|
|
+ formatter: this.tooltip,
|
|
|
+ borderColor: '#d9e1e9',
|
|
|
+ textStyle: { color: '#25292c', fontSize: 12 },
|
|
|
+ extraCssText: 'line-height:1.8;max-width:280px;overflow-wrap:anywhere'
|
|
|
+ },
|
|
|
+ series: [{
|
|
|
+ id: 'ng-sponsor-network-ex',
|
|
|
+ type: 'tree',
|
|
|
+ orient: 'TB',
|
|
|
+ layout: 'orthogonal',
|
|
|
+ edgeShape: 'polyline',
|
|
|
+ edgeForkPosition: '50%',
|
|
|
+ top: 70,
|
|
|
+ bottom: 70,
|
|
|
+ left: 112,
|
|
|
+ right: 112,
|
|
|
+ roam: true,
|
|
|
+ scaleLimit: { min: 0.5, max: 3 },
|
|
|
+ initialTreeDepth: -1,
|
|
|
+ expandAndCollapse: true,
|
|
|
+ symbol: 'rect',
|
|
|
+ symbolSize: [188, 90],
|
|
|
+ itemStyle: { color: 'transparent', borderWidth: 0 },
|
|
|
+ lineStyle: { color: '#d0dae5', width: 1.2 },
|
|
|
+ label: {
|
|
|
+ position: 'inside',
|
|
|
+ align: 'center',
|
|
|
+ verticalAlign: 'middle',
|
|
|
+ width: 164,
|
|
|
+ height: 70,
|
|
|
+ padding: [10, 12],
|
|
|
+ borderRadius: 12,
|
|
|
+ backgroundColor: '#fff',
|
|
|
+ borderColor: '#d9e1e9',
|
|
|
+ borderWidth: 1,
|
|
|
+ color: '#25292c',
|
|
|
+ overflow: 'truncate',
|
|
|
+ formatter: this.label,
|
|
|
+ rich
|
|
|
+ },
|
|
|
+ data: [{
|
|
|
+ ...this.treeData,
|
|
|
+ label: {
|
|
|
+ color: '#fff', backgroundColor: '#202259', borderColor: '#202259',
|
|
|
+ rich: { ...rich, code: { ...rich.code, color: '#82d9ee' }}
|
|
|
+ }
|
|
|
+ }],
|
|
|
+ emphasis: { disabled: true },
|
|
|
+ animationDuration: 0,
|
|
|
+ animationDurationUpdate: 200
|
|
|
+ }]
|
|
|
+ }, true)
|
|
|
+ this.centerRoot()
|
|
|
+ })
|
|
|
+ },
|
|
|
+ centerRoot() {
|
|
|
+ const series = this.chart.getModel().getSeriesByIndex(0)
|
|
|
+ if (!series) return
|
|
|
+ const root = series.getData().getItemGraphicEl(1)
|
|
|
+ if (root) this.$refs.viewport.scrollLeft = Math.max(0, root.transformCoordToGlobal(0, 0)[0] - this.viewportWidth / 2)
|
|
|
+ },
|
|
|
+ label({ data, collapsed }) {
|
|
|
+ // 接口文本不能被当成 ECharts 富文本标记;详情仍显示完整原文。
|
|
|
+ const text = value => String(value == null ? '—' : value).replace(/\{/g, '{').replace(/\}/g, '}').replace(/[\r\n]/g, ' ')
|
|
|
+ const hasChildren = data.children && data.children.length > 0
|
|
|
+ const canExpand = hasChildren || (!data.leaf && data.children == null)
|
|
|
+ const toggle = canExpand ? '{toggle|' + (hasChildren && !collapsed ? '−' : '+') + '}' : '{leaf|·}'
|
|
|
+ return '{code|' + text(data.USER_NAME) + '}\n{member|' + text(data.REAL_NAME || 'Member ' + data.USER_NAME) + '}\n' +
|
|
|
+ '{rank|' + text(data.DEC_LV_NAME) + '}{gap| }{period|Period: ' + text(data.PERIOD_AT) + '}' + toggle
|
|
|
+ },
|
|
|
+ tooltip({ data }) {
|
|
|
+ const content = document.createElement('div')
|
|
|
+ const fields = { 'Member ID': data.USER_NAME, Name: data.REAL_NAME, Level: data.DEC_LV_NAME, Highest: data.EMP_LV_NAME, Period: data.PERIOD_AT }
|
|
|
+ Object.keys(fields).forEach(label => {
|
|
|
+ const row = document.createElement('div')
|
|
|
+ row.textContent = label + ': ' + (fields[label] == null || fields[label] === '' ? '—' : fields[label])
|
|
|
+ content.appendChild(row)
|
|
|
+ })
|
|
|
+ return content
|
|
|
+ },
|
|
|
+ handleClick(event) {
|
|
|
+ if (event.componentType === 'series') this.$emit('click-node', event)
|
|
|
+ },
|
|
|
+ activateNode(data, dataIndex) {
|
|
|
+ this.chart.dispatchAction({ type: 'treeExpandAndCollapse', seriesIndex: 0, dataIndex })
|
|
|
+ this.$emit('click-node', { data, name: data.USER_NAME, collapsed: false })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+.ng-network-chart {
|
|
|
+ position: relative;
|
|
|
+}
|
|
|
+
|
|
|
+.ng-network-chart-scroll {
|
|
|
+ overflow: auto;
|
|
|
+}
|
|
|
+
|
|
|
+.ng-network-chart-scroll:focus-visible {
|
|
|
+ outline: 2px solid #1683a3;
|
|
|
+ outline-offset: 2px;
|
|
|
+}
|
|
|
+
|
|
|
+.ng-network-keyboard {
|
|
|
+ position: absolute;
|
|
|
+ width: 1px;
|
|
|
+ height: 1px;
|
|
|
+ overflow: hidden;
|
|
|
+ clip-path: inset(50%);
|
|
|
+}
|
|
|
+
|
|
|
+.ng-network-keyboard:focus-within {
|
|
|
+ z-index: 2;
|
|
|
+ width: 100%;
|
|
|
+ height: auto;
|
|
|
+ max-height: 200px;
|
|
|
+ margin: 0;
|
|
|
+ padding: 12px 24px;
|
|
|
+ overflow: auto;
|
|
|
+ background: #fff;
|
|
|
+ clip-path: none;
|
|
|
+}
|
|
|
+</style>
|