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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { find, merge } from 'lodash-es'
import { useI18n } from '@/hooks/web/useI18n'
import { getLocale } from '@/utils/utils'
import { parseJson } from '@/views/chart/components/js/util'
 
const { t } = useI18n()
 
export const isEnLocal = !['zh', 'zh-cn', 'zh-CN', 'tw'].includes(getLocale())
 
export const formatterItem = {
  type: 'auto', // auto,value,percent
  unitLanguage: isEnLocal ? 'en' : 'ch',
  unit: 1, // 换算单位
  suffix: '', // 单位后缀
  decimalCount: 2, // 小数位数
  thousandSeparator: true // 千分符
}
 
// 单位list
export const unitType = [
  { name: t('chart.unit_none'), value: 1 },
  { name: t('chart.unit_thousand'), value: 1000 },
  { name: t('chart.unit_ten_thousand'), value: 10000 },
  { name: t('chart.unit_million'), value: 1000000 },
  { name: t('chart.unit_hundred_million'), value: 100000000 }
]
export const unitEnType = [
  { name: 'None', value: 1 },
  { name: 'Thousand (K)', value: 1000 },
  { name: 'Million (M)', value: 1000000 },
  { name: 'Billion (B)', value: 1000000000 }
]
 
export function getUnitTypeList(lang) {
  if (isEnLocal) {
    return unitEnType
  }
  if (lang === 'ch') {
    return unitType
  }
  return unitEnType
}
 
export function getUnitTypeValue(lang, value) {
  const list = getUnitTypeList(lang)
  const item = find(list, l => l.value === value)
  if (item) {
    return value
  }
  return 1
}
 
export function initFormatCfgUnit(cfg) {
  if (cfg && cfg.unitLanguage === undefined) {
    cfg.unitLanguage = 'ch'
  }
  if (cfg && isEnLocal) {
    cfg.unitLanguage = 'en'
  }
  onChangeFormatCfgUnitLanguage(cfg, cfg.unitLanguage)
}
 
export function onChangeFormatCfgUnitLanguage(cfg, lang) {
  cfg.unit = getUnitTypeValue(lang, cfg.unit)
}
 
// 格式化方式
export const formatterType = [
  { name: 'value_formatter_auto', value: 'auto' },
  { name: 'value_formatter_value', value: 'value' },
  { name: 'value_formatter_percent', value: 'percent' }
]
 
export function valueFormatter(value, formatter) {
  if (value === null || value === undefined) {
    return null
  }
  // 1.unit 2.decimal 3.thousand separator and suffix
  let result
  if (formatter.type === 'auto') {
    result = transSeparatorAndSuffix(transUnit(value, formatter), formatter)
  } else if (formatter.type === 'value') {
    result = transSeparatorAndSuffix(
      transDecimal(transUnit(value, formatter), formatter),
      formatter
    )
  } else if (formatter.type === 'percent') {
    value = value * 100
    result = transSeparatorAndSuffix(transDecimal(value, formatter), formatter)
  } else {
    result = value
  }
  return result
}
 
function transUnit(value, formatter) {
  initFormatCfgUnit(formatter)
  return value / formatter.unit
}
 
function transDecimal(value, formatter) {
  const resultV = retain(value, formatter.decimalCount) as string
  if (Object.is(parseFloat(resultV), -0)) {
    return resultV.slice(1)
  }
  return resultV
}
 
function retain(value, n) {
  if (!n) return Math.round(value)
  const tran = Math.round(value * Math.pow(10, n)) / Math.pow(10, n)
  let tranV = tran.toString()
  const newVal = tranV.indexOf('.')
  if (newVal < 0) {
    tranV += '.'
  }
  for (let i = tranV.length - tranV.indexOf('.'); i <= n; i++) {
    tranV += '0'
  }
  return tranV
}
 
function transSeparatorAndSuffix(value, formatter) {
  let str = value + ''
  if (str.match(/^(\d)(\.\d)?e-(\d)/)) {
    str = value.toFixed(18).replace(/\.?0+$/, '')
  }
  if (formatter.thousandSeparator) {
    const thousandsReg = /(\d)(?=(\d{3})+$)/g
    const numArr = str.split('.')
    numArr[0] = numArr[0].replace(thousandsReg, '$1,')
    str = numArr.join('.')
  }
  if (formatter.type === 'percent') {
    str += '%'
    //百分比没有后缀,直接返回
    return str
  } else {
    const unit = formatter.unit
 
    if (formatter.unitLanguage === 'ch') {
      if (unit === 1000) {
        str += t('chart.unit_thousand')
      } else if (unit === 10000) {
        str += t('chart.unit_ten_thousand')
      } else if (unit === 1000000) {
        str += t('chart.unit_million')
      } else if (unit === 100000000) {
        str += t('chart.unit_hundred_million')
      }
    } else {
      if (unit === 1000) {
        str += 'K'
      } else if (unit === 1000000) {
        str += 'M'
      } else if (unit === 1000000000) {
        str += 'B'
      }
    }
  }
  return str + formatter.suffix.replace(/(^\s*)|(\s*$)/g, '')
}
 
/**
 * 根据最小值、最大值max和刻度数量tickCount
 * 计算一个nice最小刻度值
 * @param min
 * @param max
 * @param tickCount
 */
function niceMin(min, max, tickCount = 5) {
  // 数据的总跨度
  const range = max - min
  // 将范围均分为 tickCount-1 份, 得到每份的粗略步长
  const roughStep = range / (tickCount - 1)
  // 确定步长的数量级
  // 取步长的 10 为底的对数,向下取整,得到步长的数量级
  const exponent = Math.floor(Math.log10(roughStep))
  // 将步长取整到nice的倍数(如 1, 2, 5, 10, 20, 50...)
  const power = Math.pow(10, exponent)
  // 从中找出第一个大于等于粗略步长的nice步长
  const niceStep = [1, 2, 5, 10].map(mult => mult * power).find(step => step >= roughStep)
  // 将 min 向下取整到 niceStep 的整数倍,得到一个更整齐的起始值
  return Math.floor(min / niceStep) * niceStep
}
 
/**
 * 监听图例、缩略轴事件,计算y轴的nice最小刻度值
 * @param chart
 * @param newChart
 */
export const listenYAxisNiceMinEvents = (chart: Chart, newChart) => {
  const yAxis = parseJson(chart.customStyle).yAxis
  if (yAxis.axisValue?.auto) {
    newChart.on('legend-item-group:click', e => {
      if (e.view?.options?.scales) {
        const values = e.view.filteredData
          .map(d => d.value)
          ?.filter(v => v !== null && v !== undefined)
        const min = Math.min(...values)
        const max = Math.max(...values)
        e.view.options.scales.value.min = niceMin(min, max)
        e.view.render(true)
      }
    })
    newChart.on('slider:valuechanged', ev => {
      const values = ev.view.filteredData
        .map(d => d.value)
        ?.filter(v => v !== null && v !== undefined)
      const min = Math.min(...values)
      const max = Math.max(...values)
      if (max !== min) {
        ev.view.options.scales.value.min = niceMin(min, max)
      }
    })
  }
}
 
/**
 * 计算数据min值的nice值
 * @param chart
 * @param options
 * @param tmpOptions
 */
export const calcNiceMinValue = (chart, options, tmpOptions) => {
  let filteredData
  let cfg
  const senior = parseJson(chart.senior)
  if (senior.functionCfg && senior.functionCfg.sliderShow) {
    cfg = {
      start: senior.functionCfg.sliderRange[0] / 100,
      end: senior.functionCfg.sliderRange[1] / 100
    }
  }
  const data = options.data || []
  filteredData = data
  // 如果有缩略轴,则取缩略轴范围
  if (cfg && cfg.start !== undefined && cfg.end !== undefined) {
    const startIndex = Math.floor(cfg.start * data.length)
    const endIndex = Math.ceil(cfg.end * data.length)
    filteredData = data.slice(startIndex, endIndex)
  }
  const values = filteredData.map(d => d.value)?.filter(v => v !== null && v !== undefined)
  const min = Math.min(...values)
  const max = Math.max(...values)
  const niceMinValue = niceMin(min, max)
  if (isNaN(niceMinValue)) {
    return tmpOptions
  }
  const axis = {
    yAxis: {
      ...tmpOptions.yAxis,
      min: niceMinValue
    }
  }
  return { ...tmpOptions, ...axis }
}
 
const unShowTooltipsFormatter = [
  'table-info',
  'table-normal',
  'table-pivot',
  'stock-line',
  'bullet-graph',
  'percentage-bar-stack-horizontal'
]
/**
 * 适配图表数字格式化属性
 * @param viewInfo
 * @param value
 */
export const formatterViewInfo = (viewInfo, value) => {
  viewInfo.xAxis.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo.xAxisExt.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo.yAxis.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo.extStack.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo.extBubble.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo.extLabel.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo.extTooltip.forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  //customAttr
  viewInfo['customAttr']['label']['labelFormatter'] = merge(
    viewInfo['customAttr']['label']['labelFormatter'],
    value
  )
  viewInfo['customAttr']['label']['quotaLabelFormatter'] = merge(
    viewInfo['customAttr']['label']['quotaLabelFormatter'],
    value
  )
  viewInfo['customAttr']['label']['seriesLabelFormatter'].forEach(function (item) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  })
  viewInfo['customAttr']['label']['totalFormatter'] = merge(
    viewInfo['customAttr']['label']['totalFormatter'],
    value
  )
  if (!unShowTooltipsFormatter.includes(viewInfo.type)) {
    viewInfo['customAttr']['tooltip']['tooltipFormatter'] = merge(
      viewInfo['customAttr']['tooltip']['tooltipFormatter'],
      value
    )
    viewInfo['customAttr']['tooltip']['seriesTooltipFormatter'].forEach(function (item) {
      item['formatterCfg'] = merge(item['formatterCfg'], value)
    })
  }
 
  //customStyle
  viewInfo['customStyle']['xAxis']['axisLabelFormatter'] = merge(
    viewInfo['customStyle']['xAxis']['axisLabelFormatter'],
    value
  )
  viewInfo['customStyle']['yAxis']['axisLabelFormatter'] = merge(
    viewInfo['customStyle']['yAxis']['axisLabelFormatter'],
    value
  )
  viewInfo['customStyle']['yAxisExt']['axisLabelFormatter'] = merge(
    viewInfo['customStyle']['yAxisExt']['axisLabelFormatter'],
    value
  )
}
 
export const mergeTooltipFormat = (item, type, value) => {
  if (!unShowTooltipsFormatter.includes(type)) {
    item['formatterCfg'] = merge(item['formatterCfg'], value)
  }
}