开发公司uniapp App端 echarts 设置tooltip的formatter不生效问题及解决办法

一、开发需求

在App端实现,开发公司图表的提示框中展示数值的单位。如下图:

 

二、开发公司遇到的问题

1、开发公司首先想到的是对tooltip开发公司进行相关的设置,开发公司然后试了两种方式,开发公司都没有效果。

(1)设置tooltip的valueFormatter

valueFormatte:tooltip 开发公司中数值显示部分的格式化回调函数。(从 v5.3.0 开始支持)

回调函数格式:

(value: number | string) => string

相关代码:

  1. tooltip: {
  2. show: true,
  3. trigger: 'axis',
  4. // App端不生效
  5. valueFormatter: (value) => {
  6. if (value && value !== '-') return Number(value).toFixed(2) + '元'
  7. else return '-'
  8. }
  9. }

设置完后,h5能正常看到单位,但是App端不生效。(当前使用版本是V5.3.3,不是版本问题)

(2)设置tooltip的formatter

formatter:提示框浮层内容格式器,支持字符串模板和回调函数两种形式。

使用回调函数形式:

  1. tooltip: {
  2. show: true,
  3. trigger: 'axis',
  4. // App端也不生效
  5. formatter: (params) => {
  6. let str = params[0].name
  7. params.forEach(item => {
  8. let valueStr = item.value&&item.value!=='-' ? (item.value + '元') : '-'
  9. str = str + '' + item.marker + item.seriesName + ' ' + valueStr
  10. })
  11. return str
  12. }
  13. }

设置完后,h5能正常看到单位,但是App端不生效。(当前使用版本是V5.3.3,不是版本问题)

三、发现原因及解决办法

经过网上查询和代码审查,发现是App端不生效是因为:

在App端,回调函数无法从renderjs外传递,而我上面的两种设置都使用了回调函数,因此App端不生效。

但是需求功能要使用回调函数才能实现。

查看Echarts组件的代码,发现里面有这样一段代码:

 

所以,只需要在函数update(option) {}里面自定义设置相关回调函数即可。

下面是我最终的实现代码:

Echarts/index.vue关键代码:

  1. /**
  2. * 监测数据更新
  3. * @param {Object} option
  4. */
  5. update(option) {
  6. if (this.chart) {
  7. // 因App端,回调函数无法从renderjs外传递,故在此自定义设置相关回调函数
  8. if (option) {
  9. // console.log(91, option)
  10. // tooltip
  11. if (option.tooltip) {
  12. // 判断是否格式化tooltip
  13. if (option.tooltip.formatterStatus) {
  14. option.tooltip.formatter = this.tooltipFormatter(option.tooltip.formatterUnit, option.tooltip.formatFloat2, option.tooltip.formatThousands)
  15. }
  16. // 设置tooltip浮层层级
  17. option.tooltip.extraCssText = 'z-index: 1000;'
  18. }
  19. }
  20. // 设置新的option
  21. this.chart.setOption(option, option.notMerge)
  22. }
  23. },
  24. /**
  25. * tooltip格式化 - 适用于折线图、柱状图等多系列图表
  26. * @param {Object} unit 数值后的单位
  27. * @param {Object} formatFloat2 是否保留两位小数
  28. * @param {Object} formatThousands 是否添加千分位
  29. */
  30. tooltipFormatter(unit, formatFloat2=true, formatThousands) {
  31. return params => {
  32. console.log(152, params)
  33. let result = ''
  34. for (let i in params) {
  35. if (i == 0) {
  36. result += params[i].axisValueLabel
  37. }
  38. let value = '-'
  39. if (params[i].data !== null && params[i].data !== '' && params[i].data !== '-') {
  40. value = params[i].data
  41. // 保留两位小数
  42. if (formatFloat2) {
  43. value = this.formatFloat2(value)
  44. }
  45. // 添加千分位
  46. if (formatThousands) {
  47. value = this.formatThousands(value)
  48. }
  49. if (unit) value = value + ' ' + unit
  50. }
  51. // #ifdef H5
  52. result += '' + params[i].marker + params[i].seriesName + ' ' + value
  53. // #endif
  54. // #ifdef APP-PLUS
  55. result += '<br/>' + params[i].marker + params[i].seriesName + '&nbsp;&nbsp;' + value
  56. // #endif
  57. }
  58. return result
  59. }
  60. },
  61. /**
  62. * 保留两位小数
  63. * @param {Object} value
  64. */
  65. formatFloat2(value) {
  66. let temp = Math.round(parseFloat(value) * 100) / 100
  67. let xsd = temp.toString().split('.')
  68. if (xsd.length === 1) {
  69. temp = (isNaN(temp) ? '0' : temp.toString()) + '.00'
  70. return temp
  71. }
  72. if (xsd.length > 1) {
  73. if (xsd[1].length < 2) {
  74. temp = temp.toString() + '0'
  75. }
  76. return temp
  77. }
  78. },
  79. /**
  80. * 添加千分位
  81. * @param {Object} value
  82. */
  83. formatThousands(value) {
  84. if (value === undefined || value === null) {
  85. value = ''
  86. }
  87. if (!isNaN(value)) {
  88. value = value + ''
  89. }
  90. let re = /\d{1,3}(?=(\d{3})+$)/g
  91. let n1 = value.replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) {
  92. return s1.replace(re, '$&,') + s2
  93. })
  94. return n1
  95. }

在需要使用的图表进行相关配置:

  1. tooltip: {
  2. show: true,
  3. trigger: 'axis',
  4. formatterStatus: true,
  5. formatterUnit: '元'
  6. }

三、最终实现效果

 

举一反三,(App端)需要使用回调函数配置echarts图表相关功能的,都可以用这种方式实现。

记录于2022-09-21.

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发