echarts 的series有多个对象,如何实现tooltip的轮播
时间: 2023-05-30 11:01:57 浏览: 223
echarts-tooltip-carousel.js
要实现tooltip的轮播,可以通过以下步骤:
1. 在tooltip的formatter函数中,获取当前数据的数量和索引,并将其保存到变量中。
```javascript
formatter: function(params) {
var dataIndex = params.dataIndex;
var dataCount = params.seriesData.length;
...
}
```
2. 在tooltip的axisPointer的showContent属性中,设置轮播的定时器,并在定时器中更新tooltip的内容,并调用轮播函数。
```javascript
axisPointer: {
type: 'shadow',
show: true,
handle: {
show: true,
margin: 30
},
showContent: true,
triggerOn: 'mousemove',
formatter: function(params) {
var dataIndex = params.dataIndex;
var dataCount = params.seriesData.length;
var content = '';
var current = dataIndex;
var timer = setInterval(function() {
current = (current + 1) % dataCount;
content = '内容' + current;
chart.dispatchAction({
type: 'showTip',
seriesIndex: params.seriesIndex,
dataIndex: current
});
chart.dispatchAction({
type: 'highlight',
seriesIndex: params.seriesIndex,
dataIndex: current
});
setTimeout(function() {
chart.dispatchAction({
type: 'hideTip'
});
chart.dispatchAction({
type: 'downplay',
seriesIndex: params.seriesIndex,
dataIndex: current
});
}, 2000);
}, 3000);
return content;
}
}
```
3. 在轮播函数中,先通过dispatchAction方法显示当前数据的tooltip,并高亮当前数据。然后等待2秒钟后,再通过dispatchAction方法隐藏tooltip,并取消当前数据的高亮状态。
```javascript
function carousel(params, chart) {
var dataIndex = params.dataIndex;
var dataCount = params.seriesData.length;
var current = dataIndex;
setInterval(function() {
current = (current + 1) % dataCount;
chart.dispatchAction({
type: 'showTip',
seriesIndex: params.seriesIndex,
dataIndex: current
});
chart.dispatchAction({
type: 'highlight',
seriesIndex: params.seriesIndex,
dataIndex: current
});
setTimeout(function() {
chart.dispatchAction({
type: 'hideTip'
});
chart.dispatchAction({
type: 'downplay',
seriesIndex: params.seriesIndex,
dataIndex: current
});
}, 2000);
}, 3000);
}
```
4. 在图表初始化后,调用轮播函数。
```javascript
var chart = echarts.init(document.getElementById('main'));
...
chart.on('showTip', function(params) {
clearInterval(carouselTimer);
});
chart.on('hideTip', function(params) {
carouselTimer = setInterval(function() {
carousel(params, chart);
}, 3000);
});
...
var carouselTimer = setInterval(function() {
carousel(params, chart);
}, 3000);
```
完整代码示例:
```javascript
var option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow',
show: true,
handle: {
show: true,
margin: 30
},
showContent: true,
triggerOn: 'mousemove',
formatter: function(params) {
var dataIndex = params.dataIndex;
var dataCount = params.seriesData.length;
var content = '';
var current = dataIndex;
var timer = setInterval(function() {
current = (current + 1) % dataCount;
content = '内容' + current;
chart.dispatchAction({
type: 'showTip',
seriesIndex: params.seriesIndex,
dataIndex: current
});
chart.dispatchAction({
type: 'highlight',
seriesIndex: params.seriesIndex,
dataIndex: current
});
setTimeout(function() {
chart.dispatchAction({
type: 'hideTip'
});
chart.dispatchAction({
type: 'downplay',
seriesIndex: params.seriesIndex,
dataIndex: current
});
}, 2000);
}, 3000);
return content;
}
}
},
series: [{
type: 'bar',
data: [10, 20, 30, 40, 50]
}]
};
var chart = echarts.init(document.getElementById('main'));
chart.setOption(option);
function carousel(params, chart) {
var dataIndex = params.dataIndex;
var dataCount = params.seriesData.length;
var current = dataIndex;
setInterval(function() {
current = (current + 1) % dataCount;
chart.dispatchAction({
type: 'showTip',
seriesIndex: params.seriesIndex,
dataIndex: current
});
chart.dispatchAction({
type: 'highlight',
seriesIndex: params.seriesIndex,
dataIndex: current
});
setTimeout(function() {
chart.dispatchAction({
type: 'hideTip'
});
chart.dispatchAction({
type: 'downplay',
seriesIndex: params.seriesIndex,
dataIndex: current
});
}, 2000);
}, 3000);
}
chart.on('showTip', function(params) {
clearInterval(carouselTimer);
});
chart.on('hideTip', function(params) {
carouselTimer = setInterval(function() {
carousel(params, chart);
}, 3000);
});
var carouselTimer = setInterval(function() {
carousel(params, chart);
}, 3000);
```
阅读全文