当前位置:   article > 正文

关于Echarts图标的tooltip配置中的formatter_echarts line tooltip formatter

echarts line tooltip formatter

使用tooltip的formatter进行格式化,格式化前效果如上图,一个是数字,一个是xxx万,不符合设计,需要统一,期望效果是这样

需要先了解formatter

格式化的类型有两种,一是字符串模板,二是自定义模板


字符串模板:

模板变量有 {a}, {b},{c},{d},{e},分别表示系列名,数据名,数据值等。 在 trigger 为 ‘axis’ 的时候,会有多个系列的数据,此时可以通过 {a0}, {a1}, {a2} 这种后面加索引的方式表示系列的索引。 不同图表类型下的 {a},{b},{c},{d} 含义不一样。 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:

  • 折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)
  • 散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)
  • 地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)
  • 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)


示例

formatter: '{b0}: {c0}'

自定义函数(回调函数):

回调函数格式:

(params: Object|Array, ticket: string, callback: (ticket: string, html: string)) => string


这里字符串模板不太方便实现,所以使用自定义模板


自定义函数的第一个参数params是formatter需要的数据集
包含以下内容

  1. {
  2. componentType: 'series',
  3. // 系列类型
  4. seriesType: string,
  5. // 系列在传入的 option.series 中的 index
  6. seriesIndex: number,
  7. // 系列名称
  8. seriesName: string,
  9. // 数据名,类目名
  10. name: string,
  11. // 数据在传入的 data 数组中的 index
  12. dataIndex: number,
  13. // 传入的原始数据项
  14. data: Object,
  15. // 传入的数据值
  16. value: number|Array,
  17. // 数据图形的颜色
  18. color: string,
  19. // 饼图的百分比
  20. percent: number,
  21. }

在 trigger 为 ‘axis’ 的时候,或者 tooltip 被 axisPointer 触发的时候,params 是多个系列的数据数组

  1. {
  2. componentType: 'series',
  3. // 系列类型
  4. seriesType: string,
  5. // 系列在传入的 option.series 中的 index
  6. seriesIndex: number,
  7. // 系列名称
  8. seriesName: string,
  9. // 数据名,类目名
  10. name: string,
  11. // 数据在传入的 data 数组中的 index
  12. dataIndex: number,
  13. // 传入的原始数据项
  14. data: Object,
  15. // 传入的数据值。在多数系列下它和 data 相同。在一些系列下是 data 中的分量(如 map、radar 中)
  16. value: number|Array|Object,
  17. // 坐标轴 encode 映射信息,
  18. // key 为坐标轴(如 'x' 'y' 'radius' 'angle' 等)
  19. // value 必然为数组,不会为 null/undefied,表示 dimension index 。
  20. // 其内容如:
  21. // {
  22. // x: [2] // dimension index 为 2 的数据映射到 x 轴
  23. // y: [0] // dimension index 为 0 的数据映射到 y 轴
  24. // }
  25. encode: Object,
  26. // 维度名列表
  27. dimensionNames: Array<String>,
  28. // 数据的维度 index,如 0 或 1 或 2 ...
  29. // 仅在雷达图中使用。
  30. dimensionIndex: number,
  31. // 数据图形的颜色
  32. color: string,
  33. }

第二个参数 ticket 是异步回调标识,配合第三个参数 callback 使用。

第三个参数 callback 是异步回调.

在提示框浮层内容是异步获取的时候,可以通过 callback 传入上述的 ticket 和 html 更新提示框浮层内容。

示例

  1. formatter: function (params, ticket, callback) {
  2. $.get('detail?name=' + params.name, function (content) {
  3. callback(ticket, toHTML(content));
  4. });
  5. return 'Loading';
  6. }




最后是实现代码

formatNumber是自己写的通用格式化函数

  1. option = {
  2. title: {
  3. text: '',
  4. },
  5. color: '',
  6. tooltip: {
  7. trigger: 'axis',//触发类型 [item,axis,none] item"数据项图形触发,主要在散点图,饼图等无类目轴的图表。'axis'坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表
  8. axisPointer: {
  9. type: 'cross',//line 直线指示器 shadow 阴影指示器 none 无指示器 cross 十字准星指示器
  10. },
  11. formatter: function (params: any) {
  12. let result = params[0].name + '<br>' // 获取横轴对应的数据作为提示信息的标题
  13. params.forEach(function (item: any) {
  14. if (item.seriesType === 'line') {
  15. result += item.marker + ' ' + item.seriesName + ': ' + formatNumber(item.value) + '<br>' // 对折线图数据进行格式化
  16. } else if (item.seriesType === 'bar') {
  17. result += item.marker + ' ' + item.seriesName + ': ' + formatNumber(item.value) + ' <br>' // 对柱状图数据进行格式化
  18. }
  19. })
  20. return result
  21. },
  22. },
  23. grid: {},
  24. legend: {data: [a,b]},
  25. xAxis: [],
  26. yAxis: [],
  27. series: [],
  28. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/308675
推荐阅读
相关标签
  

闽ICP备14008679号