当前位置:   article > 正文

echarts-tooltip自定义内容使用vue数据渲染_echarts tooltip formatter自定义html

echarts tooltip formatter自定义html

目的:echarts饼图扇型点击出现提示框显示table可分页

实现:echarts-tooltip的formatter支持返回HTML字符串或者创建的DOM实例。可以页面创建个DOM,在formatter函数获取并return,但是页面会渲染这个DOM,所以一开始就v-show=false不显示,在echarts点击事件里出发显示

1.在页面创建DOM

  1. <div v-show="isShow" ref="pieTable" class="tooltip">
  2. <el-table
  3. :data="pieTable"
  4. stripe
  5. height="calc(100% - 50px)"
  6. >
  7. <el-table-column
  8. prop="account"
  9. align="center"
  10. label="账户"
  11. />
  12. <el-table-column
  13. prop="name"
  14. align="center"
  15. label="账户名"
  16. />
  17. <el-table-column
  18. prop="money"
  19. align="center"
  20. label="合计金额"
  21. />
  22. <el-table-column
  23. prop="relationship"
  24. align="center"
  25. label="关系"
  26. />
  27. </el-table>
  28. <el-pagination
  29. :current-page="listQuery3.pageNum"
  30. :page-size="listQuery3.pageSize"
  31. layout="total, prev, pager, next, jumper"
  32. :total="total3"
  33. @size-change="handleSizeChange3"
  34. @current-change="handleCurrentChange3"
  35. />
  36. </div>

2.绘制饼图方法里配置tooltip,并监听点击事件

  1. pieChart() {
  2. const chartDom = document.getElementById('pie')
  3. // chartDom.style.width = this.width + 'px'
  4. this.myChart = this.$echarts.init(chartDom)
  5. const arr = [
  6. { name: '温州', value: 6, arr: [{ name: 'a', money: 100, account: '1111', relationship: '亲属' }, { name: 'b', money: 100, account: '1111', relationship: '亲属' }, { name: 'c', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }] },
  7. { name: '杭州', value: 8, arr: [{ name: 'a', money: 100, account: '1111', relationship: '亲属' }, { name: 'b', money: 100, account: '1111', relationship: '亲属' }, { name: 'c', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }] },
  8. { name: '衢州', value: 4, arr: [{ name: 'a', money: 100, account: '1111', relationship: '亲属' }, { name: 'b', money: 100, account: '1111', relationship: '亲属' }, { name: 'c', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }] },
  9. { name: '湖州', value: 10, arr: [{ name: 'a', money: 100, account: '1111', relationship: '亲属' }, { name: 'b', money: 100, account: '1111', relationship: '亲属' }, { name: 'c', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }, { name: 'd', money: 100, account: '1111', relationship: '亲属' }] }
  10. ]
  11. const option = {
  12. tooltip: {
  13. triggerOn: 'click',
  14. enterable: true, //鼠标可以移入提示框
  15. confine: true, //提示框不超出可视区域显示
  16. formatter: (params) => {
  17. const DomHtml = this.$refs.pieTable
  18. return DomHtml
  19. }
  20. },
  21. series: [
  22. {
  23. type: 'pie',
  24. radius: [0, '80%'],
  25. center: ['50%', '50%'],
  26. showEmptyCircle: false,
  27. selectedMode: 'single',
  28. roseType: true,
  29. data: arr,
  30. label: {
  31. show: true,
  32. color: '#fff',
  33. fontSize: 15,
  34. fontWeight: 600,
  35. formatter: '{b}:{c}',
  36. position: 'inside'
  37. }
  38. }
  39. ]
  40. }
  41. this.myChart.off('click')
  42. this.myChart.on('click', (params) => {
  43. this.isShow = true
  44. this.getPieTable()
  45. this.pieTable = params.data.arr
  46. })
  47. option && this.myChart.setOption(option)
  48. }

3.效果图

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