wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import {
  G2PlotChartView,
  G2PlotDrawOptions
} from '@/views/chart/components/js/panel/types/impl/g2plot'
import type {
  CirclePacking as G2CirclePacking,
  CirclePackingOptions
} from '@antv/g2plot/esm/plots/circle-packing'
import { flow, parseJson } from '@/views/chart/components/js/util'
import { getPadding } from '@/views/chart/components/js/panel/common/common_antv'
import { useI18n } from '@/hooks/web/useI18n'
import type { Datum } from '@antv/g2plot/esm/types/common'
import { valueFormatter } from '@/views/chart/components/js/formatter'
import { cloneDeep } from 'lodash-es'
 
const { t } = useI18n()
const DEFAULT_DATA = []
/**
 * 圆形填充图
 */
export class CirclePacking extends G2PlotChartView<CirclePackingOptions, G2CirclePacking> {
  properties: EditorProperty[] = [
    'basic-style-selector',
    'background-overall-component',
    'border-style',
    'label-selector',
    'legend-selector',
    'title-selector',
    'tooltip-selector',
    'jump-set',
    'linkage'
  ]
  propertyInner: EditorPropertyInner = {
    'background-overall-component': ['all'],
    'border-style': ['all'],
    'basic-style-selector': ['colors', 'alpha', 'circleBorderStyle'],
    'title-selector': [
      'title',
      'fontSize',
      'color',
      'hPosition',
      'isItalic',
      'isBolder',
      'remarkShow',
      'fontFamily',
      'letterSpace',
      'fontShadow'
    ],
    'function-cfg': ['emptyDataStrategy'],
    'label-selector': ['color', 'fontSize', 'showDimension', 'showQuota', 'showProportion'],
    'legend-selector': ['icon', 'orient', 'fontSize', 'color', 'hPosition', 'vPosition'],
    'tooltip-selector': ['color', 'fontSize', 'backgroundColor', 'tooltipFormatter', 'show']
  }
  axis: AxisType[] = ['xAxis', 'yAxis', 'filter', 'drill']
  axisConfig: AxisConfig = {
    xAxis: {
      name: `${t('chart.circle_packing_name')} / ${t('chart.dimension')}`,
      type: 'd',
      limit: 1
    },
    yAxis: {
      name: `${t('chart.circle_packing_value')} / ${t('chart.quota')}`,
      type: 'q',
      limit: 1
    }
  }
  async drawChart(drawOptions: G2PlotDrawOptions<G2CirclePacking>): Promise<G2CirclePacking> {
    const { chart, container, action } = drawOptions
    if (chart?.data?.data?.length) {
      // data
      const data = chart.data.data
      const { yAxis } = chart
      const ySort = yAxis[0]?.sort ?? 'none'
      const sort = {
        sort: (a, b) =>
          ySort === 'asc' ? a.value - b.value : ySort === 'desc' ? b.value - a.value : 0
      }
      // 将数据转为圆形填充图数据格式
      const getCirclePackingData = () => {
        const result = [{ name: t('commons.all'), children: [] }]
        const addNode = (nodes, item) => {
          const node = { ...item, name: item.name, children: [] }
          nodes.push(node)
        }
        data.forEach(item => addNode(result[0].children, item))
        return result[0]
      }
      // options
      const initOptions: CirclePackingOptions = {
        data: getCirclePackingData(),
        appendPadding: getPadding(chart),
        hierarchyConfig: {
          ...(ySort === 'none' ? {} : sort)
        },
        interactions: [
          {
            type: 'legend-active',
            cfg: {
              start: [{ trigger: 'legend-item:mouseenter', action: ['element-active:reset'] }],
              end: [{ trigger: 'legend-item:mouseleave', action: ['element-active:reset'] }]
            }
          },
          {
            type: 'legend-filter',
            cfg: {
              start: [
                {
                  trigger: 'legend-item:click',
                  action: [
                    'list-unchecked:toggle',
                    'data-filter:filter',
                    'element-active:reset',
                    'element-highlight:reset'
                  ]
                }
              ]
            }
          }
        ]
      }
      const options = this.setupOptions(chart, initOptions)
      const { CirclePacking: G2CirclePacking } = await import(
        '@antv/g2plot/esm/plots/circle-packing'
      )
      const newChart = new G2CirclePacking(container, options)
      newChart.on('element:click', param => {
        const pointData = param?.data?.data
        if (pointData?.name === t('commons.all')) {
          return
        }
        const actionParams = {
          x: param.x,
          y: param.y,
          data: {
            data: {
              ...pointData
            }
          }
        }
        action(actionParams)
      })
      return newChart
    }
  }
 
  protected configBasicStyle(chart: Chart, options: CirclePackingOptions): CirclePackingOptions {
    // size
    const customAttr: DeepPartial<ChartAttr> = parseJson(chart.customAttr)
    const s = JSON.parse(JSON.stringify(customAttr.basicStyle))
    // 圆形边框样式
    const pointStyle = {
      stroke: s.circleBorderColor,
      lineWidth: s.circleBorderWidth ?? 0
    }
    const padding = s.circlePadding
    return {
      ...options,
      hierarchyConfig: {
        ...options.hierarchyConfig,
        padding: padding / 100 ?? 0
      },
      pointStyle
    }
  }
 
  protected configLabel(chart: Chart, options: CirclePackingOptions): CirclePackingOptions {
    const tmpOptions = super.configLabel(chart, options)
    if (!tmpOptions.label) {
      return {
        ...tmpOptions,
        label: false
      }
    }
    const { label: labelAttr } = parseJson(chart.customAttr)
    const label = {
      ...tmpOptions.label,
      textAlign: 'center',
      offsetY: 5,
      layout: labelAttr.fullDisplay ? [] : [{ type: 'limit-in-shape' }],
      formatter: (d: Datum) => {
        let showLabel = labelAttr.showDimension ? d.name : ''
        if (labelAttr.showQuota) {
          const quota = valueFormatter(d.value, labelAttr.quotaLabelFormatter)
          showLabel += showLabel ? `\n${quota}` : quota
        }
        if (labelAttr.showProportion && d.depth > 0) {
          const proportion = ((d.value || 1) / (d.parent?.value || 1)) * 100
          const percent = `${proportion.toFixed(labelAttr.reserveDecimalCount ?? 2)}%`
          showLabel += showLabel ? `\n${percent}` : percent
        }
        return d.children.length === 0 ? showLabel : ''
      }
    }
    return {
      ...tmpOptions,
      label
    }
  }
 
  protected configTooltip(chart: Chart, options: CirclePackingOptions): CirclePackingOptions {
    const temOptions = super.configTooltip(chart, options)
    if (!temOptions.tooltip) {
      return temOptions
    }
    const tooltipAttr = parseJson(chart.customAttr).tooltip
    return {
      ...temOptions,
      tooltip: {
        ...temOptions,
        fields: ['name', 'value'],
        formatter: d => {
          let value = d.value
          if (tooltipAttr.tooltipFormatter) {
            value = valueFormatter(value, tooltipAttr.tooltipFormatter)
          }
          return { name: d.name, value }
        }
      }
    }
  }
  configEmptyDataStrategy(chart: Chart, options: CirclePackingOptions): CirclePackingOptions {
    const { functionCfg } = parseJson(chart.senior)
    const emptyDataStrategy = functionCfg.emptyDataStrategy
    const setChildren = children => {
      if (emptyDataStrategy === 'ignoreData') {
        for (let i = children.length - 1; i >= 0; i--) {
          let isNotNullChildren = []
          if (children[i].children?.length) {
            isNotNullChildren = children[i].children.filter(item => item.value !== null)
          }
          if (children[i].children?.length && isNotNullChildren.length) {
            setChildren(children[i].children)
          }
          if (children[i]?.hasOwnProperty('value') && children[i].value === null) {
            children.splice(i, 1)
          }
          if (!children[i]?.hasOwnProperty('value') && isNotNullChildren.length === 0) {
            children.splice(i, 1)
          }
        }
      } else {
        for (let i = children.length - 1; i >= 0; i--) {
          let isNotNullChildren = []
          if (children[i].children?.length) {
            isNotNullChildren = children[i].children.filter(item => item.value !== null)
            if (!isNotNullChildren.length) {
              children[i].children = []
              continue
            }
          }
          setChildren(children[i].children)
        }
      }
    }
    const data = cloneDeep(options.data.children)
    setChildren(data)
    options.data.children = data
    return options
  }
  setupDefaultOptions(chart: ChartObj): ChartObj {
    const { customAttr, customStyle, senior } = chart
    const { label, basicStyle } = customAttr
    const { legend } = customStyle
    senior.functionCfg.emptyDataStrategy = 'ignoreData'
    customAttr.label = {
      ...label,
      show: true
    }
    legend.show = false
    basicStyle.circleBorderWidth = 0
    basicStyle.circleBorderColor = '#fff'
    basicStyle.circlePadding = 0
    return chart
  }
  protected setupOptions(chart: Chart, options: CirclePackingOptions): CirclePackingOptions {
    return flow(
      this.configTheme,
      this.configEmptyDataStrategy,
      this.configBasicStyle,
      this.configLabel,
      this.configTooltip,
      this.configLegend
    )(chart, options)
  }
 
  constructor() {
    super('circle-packing', DEFAULT_DATA)
  }
}