赞
踩
tooltip
内容<template> <div> <div class="echarts-demo" id="echarts-demo"></div> </div> </template> <script> export default { name: '', props: {}, components: {}, data () { return {} }, computed: {}, watch: {}, created () { }, mounted () { this.init() }, methods: { init () { let myChart = this.$echarts.init(document.querySelector('#echarts-demo')); myChart.setOption({ tooltip: { position: `top`, // padding: 0,, enterable: true, formatter (res) { const dom = '<div>'+ `数量:${res.data}` + '</div>' return dom } }, xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [ { data: [150, 230, 224, 218, 135, 147, 260], type: 'line' } ] }) }, }, } </script> <style scoped lang="less"> .echarts-demo { width: 100%; height: 500px; margin-top: 300px; } </style>
tooltip
渲染Vue
组件的内容tooltip
虽然也是自定义内容,但随着内容的复杂度提升,想完美展示出来还是存在难度的,并且如果展示内容过于复杂,还需要写很多代码才能实现,那么将其写成Vue
的一个组件就可以完美解决以上的所有问题,并且还可以无限套娃,支持更丰富的展示与交互<template> <!-- echartsTooltip.vue --> <!-- id 不能跟父组件一致 --> <div class="echarts-demo" id="echarts-demo-">{{tooltipData}}</div> </template> <script> export default { name: 'echartsTooltip', props: { tooltipData: {}, }, components: {}, data () { return {} }, computed: {}, watch: {}, created () { }, mounted () { // 必须等 DOM 更新之后,不然会找不到 Dom 元素 this.$nextTick(() => { this.init() }) }, methods: { init () { let myChart = this.$echarts.init(document.querySelector('#echarts-demo-')); myChart.setOption({ tooltip:{}, xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [ { data: [150, 230, 224, 218, 135, 147, 260], type: 'line' } ] }) }, }, } </script> <style scoped lang="less"> .echarts-demo { width: 300px; height: 300px; } </style>
tooltip
展示子组件内容<template> <div> <div class="echarts-demo" id="echarts-demo"></div> </div> </template> <script> import Vue from 'vue'; import router from '@/router'; import echartsTooltip from './echartsTooltip.vue'; export default { name: '', props: {}, components: {}, data () { return {} }, computed: {}, watch: {}, created () { }, mounted () { this.init() }, methods: { init () { let myChart = this.$echarts.init(document.querySelector('#echarts-demo')); myChart.setOption({ tooltip: { position: `top`, // 展示位置 padding: 0, enterable: true, // 鼠标是否可移入tooltip formatter (res) { // const dom = '<div>'+ `${res.data}` + '</div>' // return dom const div = document.createElement('div'); let mapTooltip = new Vue({ router, render: (h) => h(echartsTooltip, { props: { info: res.data } }) }); mapTooltip.$mount(div); return mapTooltip.$el; } }, xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [ { data: [150, 230, 224, 218, 135, 147, 260], type: 'line' } ] }) }, }, } </script> <style scoped lang="less"> .echarts-demo { width: 100%; height: 500px; margin-top: 300px; } </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。