我有一个包含一系列数据的echarts条形图。单击某个栏时,会对其应用强调。有没有一种方法可以以编程方式选择条形图中的系列之一以在图表加载时强调?
我的条形图选项代码:
options = {
markPoint: { data: ['Q1'] },
color: ['#3398DB'],
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
type: 'category',
data: ['Q1', 'Q2', 'Q3', 'Q4'],
axisTick: {
alignWithLabel: true,
}
}
],
yAxis: [{
type: 'value'
}],
series: [{
name: 'Counters',
type: 'bar',
barWidth: '50%',
data: [3,8,12,5],
itemStyle: {
//highlight
emphasis: {
barBorderColor: 'red',
barBorderWidth: 2
}
}
}]
};
Run Code Online (Sandbox Code Playgroud)
谢谢。
是的,这很容易。Echarts API 有两种作用相反的方法:
\naction.highlight\xe2\x80\x94 将强调样式设置为 dataPoint。action.downplay\xe2\x80\x94 取消突出显示。一个小例子:
\n var myChart = echarts.init(document.getElementById(\'main\'));\n var chartData = [5, 20, 36, 10, 10, 20];\n\n var option = {\n tooltip: {},\n legend: {\n data: [\'Label\']\n },\n xAxis: {\n data: ["Category1", "Category2", "Category3", "Category4", "Category5", "Category6"]\n },\n yAxis: {},\n series: [{\n name: \'Series\',\n type: \'bar\',\n data: chartData,\n emphasis: {\n itemStyle: {\n color: \'blue\'\n }\n },\n }]\n };\n\n myChart.setOption(option);\n \n // Current selected dataPoint\n var selectedDataPoint = null;\n \n // Each eteration set another type of \n setInterval(() => {\n var randomDataPoint = Math.floor(Math.random() * Math.floor(chartData.length));\n myChart.dispatchAction({ type: \'highlight\', dataIndex: randomDataPoint })\n }, 800)\n Run Code Online (Sandbox Code Playgroud)\r\n<script src="https://cdn.jsdelivr.net/npm/echarts@4.8.0/dist/echarts.min.js"></script>\n<div id="main" style="width: 600px;height:400px;"></div>Run Code Online (Sandbox Code Playgroud)\r\n| 归档时间: |
|
| 查看次数: |
8439 次 |
| 最近记录: |