我正在学习如何使用电子图表并尝试不同类型。我剪切并粘贴了两种 eChart 条形类型,一种是向下钻取 (id = main2),一种是具有负值的水平条形 (id = main1);布置在单个表格行中。
两者都正确渲染,并且向下钻取工作正常;后退按钮按预期工作并显示正确的初始条形图;但返回后它仍然在图表的顶层可见。 刷新页面即可将其删除。
<head>
<meta charset='utf-8' />
</head>
<body>
<table class="container-drilldown" >
<thead>
</thead>
<tbody>
<tr>
<td class='drilldown' class ='center' id='main2' style='width: 30vw; height:45vh' >
<script type='text/javascript'>
var chartDom = document.getElementById('main2');
var myChart = echarts.init(chartDom);
var option = {
xAxis: {
data: ['Animals', 'Fruits', 'Cars']
},
yAxis: {},
dataGroupId: '',
animationDurationUpdate: 500,
series: {
type: 'bar',
id: 'sales',
data: [
{
value: 5,
groupId: 'animals'
},
{
value: 2,
groupId: 'fruits'
},
{
value: 4,
groupId: 'cars'
}
],
universalTransition: {
enabled: true,
divideShape: 'clone'
}
}
};
const drilldownData = [
{
dataGroupId: 'animals',
data: [
['Cats', 4],
['Dogs', 2],
['Cows', 1],
['Sheep', 2],
['Pigs', 1]
]
},
{
dataGroupId: 'fruits',
data: [
['Apples', 4],
['Oranges', 2]
]
},
{
dataGroupId: 'cars',
data: [
['Toyota', 4],
['Opel', 2],
['Volkswagen', 2]
]
}
];
myChart.on('click', function (event) {
if (event.data) {
var subData = drilldownData.find(function (data) {
return data.dataGroupId === event.data.groupId;
});
if (!subData) {
return;
}
myChart.setOption({
xAxis: {
data: subData.data.map(function (item) {
return item[0];
})
},
series: {
type: 'bar',
id: 'sales',
dataGroupId: subData.dataGroupId,
data: subData.data.map(function (item) {
return item[1];
}),
universalTransition: {
enabled: true,
divideShape: 'clone'
}
},
graphic: [
{
type: 'text',
left: 50,
top: 20,
style: {
text: 'Back',
fontSize: 18
},
onclick: function () {
myChart.setOption(option);
}
}
]
});
}
});
option && myChart.setOption(option);
</script>
</td>
<td class='drilldown' class ='center' id='main1' style='width: 30vw; height:45vh' >
<script type='text/javascript'>
var chartDom1 = document.getElementById('main1');
var myChart1 = echarts.init(chartDom1);
var option1 = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
legend: {
data: ['Profit', 'Expenses', 'Income']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
type: 'value'
}
],
yAxis: [
{
type: 'category',
axisTick: {
show: false
},
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
}
],
series: [
{
name: 'Profit',
type: 'bar',
label: {
show: true,
position: 'inside'
},
emphasis: {
focus: 'series'
},
data: [200, 170, 240, 244, 200, 220, 210]
},
{
name: 'Income',
type: 'bar',
stack: 'Total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [320, 302, 341, 374, 390, 450, 420]
},
{
name: 'Expenses',
type: 'bar',
stack: 'Total',
label: {
show: true,
position: 'left'
},
emphasis: {
focus: 'series'
},
data: [-120, -132, -101, -134, -190, -230, -210]
}
]
};
option && myChart1.setOption(option1);
</script>
</td>
</tr>
echarts#setOption
的默认行为
是将现有选项与新选项合并;这就是为什么 graphic
来自
通话后保留第二个选项myChart.setOption(option)
。如果您想拥有原始选项
完全替换第二个选项,您必须禁用选项合并:
myChart.setOption(option, {notMerge: true});
这是您的代码片段,没有不相关的第二个图表,也没有丢失的 css 类:
const chartDom = document.getElementById('main2');
const myChart = echarts.init(chartDom);
const option = {
xAxis: {
data: ['Animals', 'Fruits', 'Cars']
},
yAxis: {},
dataGroupId: '',
animationDurationUpdate: 500,
series: {
type: 'bar',
id: 'sales',
data: [
{
value: 5,
groupId: 'animals'
},
{
value: 2,
groupId: 'fruits'
},
{
value: 4,
groupId: 'cars'
}
],
universalTransition: {
enabled: true,
divideShape: 'clone'
}
}
};
const drilldownData = [
{
dataGroupId: 'animals',
data: [
['Cats', 4],
['Dogs', 2],
['Cows', 1],
['Sheep', 2],
['Pigs', 1]
]
},
{
dataGroupId: 'fruits',
data: [
['Apples', 4],
['Oranges', 2]
]
},
{
dataGroupId: 'cars',
data: [
['Toyota', 4],
['Opel', 2],
['Volkswagen', 2]
]
}
];
myChart.on('click', function (event) {
if (event.data) {
var subData = drilldownData.find(function (data) {
return data.dataGroupId === event.data.groupId;
});
if (!subData) {
return;
}
myChart.setOption({
xAxis: {
data: subData.data.map(function (item) {
return item[0];
})
},
series: {
type: 'bar',
id: 'sales',
dataGroupId: subData.dataGroupId,
data: subData.data.map(function (item) {
return item[1];
}),
universalTransition: {
enabled: true,
divideShape: 'clone'
}
},
graphic: [
{
type: 'text',
left: 50,
top: 20,
style: {
text: 'Back',
fontSize: 18
},
onclick: function () {
myChart.setOption(option, {notMerge: true});
}
}
]
});
}
});
myChart.setOption(option);
<table class="" >
<thead>
</thead>
<tbody>
<tr>
<td class='' id='main2' style='/*width: 30vw; height:45vh;*/ width: 500px; height: 250px' >
</td>
</tr>
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>