在echarts中自定义提示框内容
一、用到的是 tooltip 下的 formatter 配置
API地址:option.html#tooltip.formatter
二、单个数据项的提示框配置
tooltip: { trigger: 'item' },
三、多个数据项的提示框配置
tooltip: { trigger: 'axis' },
1、默认提示
2、显示当前单个数据项的提示框
3、显示当前横坐标下多个数据项的自定义提示框
代码(复制可用)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>在echarts中自定义提示框内容</title>
<style>
.tips{
margin-left:20px;
color:red
}
</style>
<!-- 引入 echarts.js -->
<script src="http://echarts.baidu.com/examples/vendors/echarts/echarts.min.js?_v_=1526486305040"></script>
</head>
<body>
<a href="https://www.echartsjs.com/zh/option.html#tooltip.formatter" target="_blank">option.html#tooltip.formatter API地址</a>
<p>
<b>单个数据项的提示框配置<br>
<span class="tips">
tooltip: {
trigger: 'item'
},
</span></b>
</p>
<p>
<b>
多个数据项的提示框配置<br>
<span class="tips">
tooltip: {
trigger: 'axis'
},
</span>
</b>
</p>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<p>显示当前单个数据项的提示框</p>
<div id="main1" style="width: 600px;height:400px;"></div>
<p>显示当前横坐标下多个数据项的自定义提示框</p>
<div id="main2" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
let consoleObj = {};
/**
* @todo 渲染 echarts
* @param id echarts需要挂载的对象
* @param paramData paramData 需要展示的相关数据
*/
consoleObj.createBar = function (id, paramData) {
let barChart = echarts.init(document.getElementById( id ));
const barOptions = {
tooltip : {
trigger: 'axis',
axisPointer : {
type : 'shadow'
}
},
//color: ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83', '#ca8622', '#bda29a','#6e7074', '#546570', '#c4ccd3'],
color: ['#c23531','#d48265', '#6e7074','#749f83'],
xAxis : [ // X轴
{
show: true,
type : 'category',
axisTick: {
alignWithLabel: true
},
data : paramData.xAxis //X轴的数据
}
],
yAxis : [ // Y轴
{
type : 'value'
}
],
series: [
{
name: '订单数量',
type: 'bar',
stack: 'countStack',
itemStyle: {
barBorderRadius: [5, 5, 0, 0]
},
barCategoryGap: '50%', // 柱形间距
data: paramData.orderCount
},
{
name: '订单金额',
type: 'bar',
stack: 'priceStack',
itemStyle: {
barBorderRadius: [5, 5, 0, 0]
},
barCategoryGap: '50%', // 柱形间距
label: {
formatter: '¥:{c}',
show: true,
position: 'top' //在X轴上方
},
data: paramData.orderPrice
},
{
name: '退款数量',
type: 'bar',
stack: 'countStack',
itemStyle: {
barBorderRadius: [5, 5, 0, 0]
},
barCategoryGap: '50%', // 柱形间距
data: paramData.refundCount
},
{
name: '退款金额',
type: 'bar',
stack: 'priceStack',
itemStyle: {
barBorderRadius: [0, 0, 5, 5]
},
label: {
show: true,
formatter: '出账:{c}',
color: '#999',
position: 'bottom'
},
data: paramData.refundPrice
}
]
}
barChart.setOption(barOptions);
};
/**
* @todo 显示当前横坐标下单个数据项的自定义提示框
* @param id echarts需要挂载的对象
* @param paramData paramData 需要展示的相关数据
*/
consoleObj.createBarOne = function (id, paramData) {
let barChart = echarts.init(document.getElementById( id ));
const barOptions = {
tooltip : {
trigger: 'item',
axisPointer : {
type : 'shadow'
},
formatter: function(params){
var color = params.color;//图例颜色
var htmlStr ='<div>';
htmlStr += params.name + '<br/>';//x轴的名称
//组装订单金额,退款金额,退款数量 数据
let dateIndex = params.dataIndex; //当前stack的索引(数据在传入的 data 数组中的 index)
let string = '';
string += ';订单金额:'+ paramData.orderPrice[dateIndex];
string += '<br/>退款数量:'+ paramData.refundCount[dateIndex];
string += ';退款金额:'+ paramData.refundPrice[dateIndex];
//为了保证和原来的效果一样,在数据左边加上一个(圆圈、点)
htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:'+color+';"></span>';
//拼接提示框内容
htmlStr += params.seriesName + ':'+params.value + string;
htmlStr += '</div>';
return htmlStr;
}
},
//color: ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83', '#ca8622', '#bda29a','#6e7074', '#546570', '#c4ccd3'],
color: ['#c23531','#d48265', '#6e7074','#749f83'],
xAxis : [ // X轴
{
show: true,
type : 'category',
axisTick: {
alignWithLabel: true
},
data : paramData.xAxis //X轴的数据
}
],
yAxis : [ // Y轴
{
type : 'value'
}
],
series: [
{
name: '订单数量',
type: 'bar',
stack: 'countStack',
itemStyle: {
barBorderRadius: [5, 5, 0, 0]
},
barCategoryGap: '50%', // 柱形间距
data: paramData.orderCount
}
]
}
barChart.setOption(barOptions);
};
/**
* @todo 显示当前横坐标下多个数据项的自定义提示框
* @param id echarts需要挂载的对象
* @param paramData paramData 需要展示的相关数据
*/
consoleObj.createBarMore = function (id, paramData) {
let barChart = echarts.init(document.getElementById( id ));
const barOptions = {
tooltip : {
trigger: 'axis',
axisPointer : {
type : 'shadow'
},
formatter: function(params){
let htmlStr = '';
let money = 0;
for(let i=0;i<params.length;i++){ //循环当前 stack
let param = params[i];
let xName = param.name;//x轴的名称
let seriesName = param.seriesName;//图例名称
let value = param.value;//y轴值
let color = param.color;//图例颜色
let dateIndex = param.dataIndex; //当前stack的索引(数据在传入的 data 数组中的 index)
if(seriesName == '订单数量' ){
money = ';订单金额:'+ paramData.orderPrice[dateIndex];
}else{
money = ';退款金额:'+ paramData.refundPrice[dateIndex];
}
if(i===0){
htmlStr += xName + '<br/>';//x轴的名称
}
htmlStr +='<div>';
//为了保证和原来的效果一样,在数据左边加上一个(圆圈、点)
htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:'+color+';"></span>';
//圆点后面显示的文本
htmlStr += seriesName + ':' + value + money;
htmlStr += '</div>';
}
return htmlStr;
}
},
//color: ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83', '#ca8622', '#bda29a','#6e7074', '#546570', '#c4ccd3'],
color: ['#c23531','#d48265', '#6e7074','#749f83'],
xAxis : [ // X轴
{
show: true,
type : 'category',
axisTick: {
alignWithLabel: true
},
data : paramData.xAxis //X轴的数据
}
],
yAxis : [ // Y轴
{
type : 'value'
}
],
series: [
{
name: '订单数量',
type: 'bar',
stack: 'countStack',
itemStyle: {
barBorderRadius: [5, 5, 0, 0]
},
barCategoryGap: '50%', // 柱形间距
data: paramData.orderCount
},
{
name: '退款数量',
type: 'bar',
stack: 'countStack',
itemStyle: {
barBorderRadius: [5, 5, 0, 0]
},
barCategoryGap: '50%', // 柱形间距
data: paramData.refundCount
}
]
}
barChart.setOption(barOptions);
};
//定义数据
let paramData = {
xAxis: ['周一', '周二', '周三', '周四', '周五', '周六', '周天'], // x轴数据
orderPrice: [100, 200, 300, 400, 500, 440, 350], //订单金额
orderCount: [10, 20, 30, 40, 50, 44, 35], //订单数量
refundPrice: [-200,-400,-500,-100,-200,-440,-350], //退款金额
refundCount: [-2, -5, -15, -16, -19, -11, -22], //退款数量
};
//调用方法
consoleObj.createBar('main', paramData);
consoleObj.createBarOne('main1', paramData);
consoleObj.createBarMore('main2', paramData);
</script>
</body>
</html>