Amchart 4:如何在条形图中单击 bar/column 突出显示 bar/column
Amchart 4: How to highlight bar/column on click of bar/column in bar plots
我试图在 Amchart 4 中点击 bar/column 时突出显示 bar/column。使用下面的代码,我得到了当前点击的 bar/column 的值,但是使用 column.isActive
属性,bar/column 没有突出显示。
我找到了这个 Amchart official Documentation link,但它是关于点击轴标签的。我正在尝试实现相同但点击 bar/column,而不是轴标签。
这是代码实现:
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"country": "USA",
"visits": 2025
}, {
"country": "China",
"visits": 1882
}, {
"country": "Japan",
"visits": 1809
}, {
"country": "Germany",
"visits": 1322
}, {
"country": "UK",
"visits": 1122
}];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.labels.template.adapter.add("dy", function(dy, target) {
if (target.dataItem && target.dataItem.index & 2 == 2) {
return dy + 25;
}
return dy;
});
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "visits";
series.dataFields.categoryX = "country";
series.name = "Visits";
series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/]";
series.columns.template.fillOpacity = .8;
var columnTemplate = series.columns.template;
columnTemplate.strokeWidth = 2;
columnTemplate.strokeOpacity = 1;
// Make a panning cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "panXY";
chart.cursor.snapToSeries = series;
chart.events.on("hit", function(ev){
chart.series.each(function(series) {
if (series instanceof am4charts.ColumnSeries) {
series.columns.each(function(column) {
if (column.dataItem.categories.categoryX == series.tooltipDataItem.dataContext.country) {
console.log(column.dataItem.categories.categoryX, series.tooltipDataItem.dataContext.country);
column.isActive = true;
} else {
column.isActive = false;
}
})
}
})
})
}); // end am4core.ready()
现在,我也发现了这个 post link,但这与 Amchart 版本 3 库有关,我使用的是 Amchart 4。
请检查此 jfiddle 实施。
我不确定 amchart 是否有 api,但因为它基于 svg,我们可以轻松地为选定的列设置样式,例如 this
column.element.node.parentNode.setAttribute('fill', highLightColor);
column.element.node.parentNode.setAttribute('stroke', highLightStrokeColor);
对于每个定义,您不需要 hit
监听器,只有当您想要在激活被点击的列的同时停用其他列时。您只需创建列 togglable
并在 active 状态下定义实际属性。
您必须使列可切换。
series.columns.template.togglable = true;
这会将活动状态从 false
切换到 true
。 togglable
状态激活时的属性
var tActiveState = series.columns.template.states.create("active");
tActiveState.properties.strokeWidth = 0;
tActiveState.properties.fill = "crimson";
您必须设置活动状态的属性。 states
命中时停用其他列
series.columns.template.events.on("hit", function(event){
series.columns.each(function(column){
if(column !== event.target){
column.setState("default");
column.isActive = false
}
})
});
为此,我们将再次使用 hit
侦听器。 Event Listeners
基于您的代码的示例
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"country": "USA",
"visits": 2025
}, {
"country": "China",
"visits": 1882
}, {
"country": "Japan",
"visits": 1809
}, {
"country": "Germany",
"visits": 1322
}, {
"country": "UK",
"visits": 1122
}];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.labels.template.adapter.add("dy", function(dy, target) {
if (target.dataItem && target.dataItem.index & 2 == 2) {
return dy + 25;
}
return dy;
});
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "visits";
series.dataFields.categoryX = "country";
series.name = "Visits";
series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/]";
series.columns.template.fillOpacity = .8;
//REM: Making the columns togglable.
series.columns.template.togglable = true;
//REM: Setting the properties of the "hover" state
var tHoverState = series.columns.template.states.create("hover");
tHoverState.properties.strokeWidth = 1;
tHoverState.properties.fill = "yellow";
//REM: Setting the properties of the active state "isActive=true"
var tActiveState = series.columns.template.states.create("active");
tActiveState.properties.strokeWidth = 0;
tActiveState.properties.fill = "crimson";
var columnTemplate = series.columns.template;
columnTemplate.strokeWidth = 2;
columnTemplate.strokeOpacity = 1;
// Make a panning cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "panXY";
chart.cursor.snapToSeries = series;
//REM: To deactivate other columns on click
series.columns.template.events.on("hit", function(event){
series.columns.each(function(column){
if(column !== event.target){
column.setState("default");
column.isActive = false
}
})
});
}); // end am4core.ready()
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id = 'chartdiv'></div>
或者你可以跳过 togglable 并且只使用 hit
series.columns.template.events.on("hit", function(event){
series.columns.each(function(column){
//REM: Deactivate all non clicked columns
if(column !== event.target){
column.setState("default");
column.isActive = false
}
//REM: Toggle the clicked column
else{
column.isActive = !column.isActive
}
})
});
我试图在 Amchart 4 中点击 bar/column 时突出显示 bar/column。使用下面的代码,我得到了当前点击的 bar/column 的值,但是使用 column.isActive
属性,bar/column 没有突出显示。
我找到了这个 Amchart official Documentation link,但它是关于点击轴标签的。我正在尝试实现相同但点击 bar/column,而不是轴标签。
这是代码实现:
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"country": "USA",
"visits": 2025
}, {
"country": "China",
"visits": 1882
}, {
"country": "Japan",
"visits": 1809
}, {
"country": "Germany",
"visits": 1322
}, {
"country": "UK",
"visits": 1122
}];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.labels.template.adapter.add("dy", function(dy, target) {
if (target.dataItem && target.dataItem.index & 2 == 2) {
return dy + 25;
}
return dy;
});
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "visits";
series.dataFields.categoryX = "country";
series.name = "Visits";
series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/]";
series.columns.template.fillOpacity = .8;
var columnTemplate = series.columns.template;
columnTemplate.strokeWidth = 2;
columnTemplate.strokeOpacity = 1;
// Make a panning cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "panXY";
chart.cursor.snapToSeries = series;
chart.events.on("hit", function(ev){
chart.series.each(function(series) {
if (series instanceof am4charts.ColumnSeries) {
series.columns.each(function(column) {
if (column.dataItem.categories.categoryX == series.tooltipDataItem.dataContext.country) {
console.log(column.dataItem.categories.categoryX, series.tooltipDataItem.dataContext.country);
column.isActive = true;
} else {
column.isActive = false;
}
})
}
})
})
}); // end am4core.ready()
现在,我也发现了这个
请检查此 jfiddle 实施。
我不确定 amchart 是否有 api,但因为它基于 svg,我们可以轻松地为选定的列设置样式,例如 this
column.element.node.parentNode.setAttribute('fill', highLightColor);
column.element.node.parentNode.setAttribute('stroke', highLightStrokeColor);
对于每个定义,您不需要 hit
监听器,只有当您想要在激活被点击的列的同时停用其他列时。您只需创建列 togglable
并在 active 状态下定义实际属性。
您必须使列可切换。
series.columns.template.togglable = true;
这会将活动状态从 false
切换到 true
。 togglable
状态激活时的属性
var tActiveState = series.columns.template.states.create("active");
tActiveState.properties.strokeWidth = 0;
tActiveState.properties.fill = "crimson";
您必须设置活动状态的属性。 states
命中时停用其他列
series.columns.template.events.on("hit", function(event){
series.columns.each(function(column){
if(column !== event.target){
column.setState("default");
column.isActive = false
}
})
});
为此,我们将再次使用 hit
侦听器。 Event Listeners
基于您的代码的示例
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"country": "USA",
"visits": 2025
}, {
"country": "China",
"visits": 1882
}, {
"country": "Japan",
"visits": 1809
}, {
"country": "Germany",
"visits": 1322
}, {
"country": "UK",
"visits": 1122
}];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.labels.template.adapter.add("dy", function(dy, target) {
if (target.dataItem && target.dataItem.index & 2 == 2) {
return dy + 25;
}
return dy;
});
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "visits";
series.dataFields.categoryX = "country";
series.name = "Visits";
series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/]";
series.columns.template.fillOpacity = .8;
//REM: Making the columns togglable.
series.columns.template.togglable = true;
//REM: Setting the properties of the "hover" state
var tHoverState = series.columns.template.states.create("hover");
tHoverState.properties.strokeWidth = 1;
tHoverState.properties.fill = "yellow";
//REM: Setting the properties of the active state "isActive=true"
var tActiveState = series.columns.template.states.create("active");
tActiveState.properties.strokeWidth = 0;
tActiveState.properties.fill = "crimson";
var columnTemplate = series.columns.template;
columnTemplate.strokeWidth = 2;
columnTemplate.strokeOpacity = 1;
// Make a panning cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "panXY";
chart.cursor.snapToSeries = series;
//REM: To deactivate other columns on click
series.columns.template.events.on("hit", function(event){
series.columns.each(function(column){
if(column !== event.target){
column.setState("default");
column.isActive = false
}
})
});
}); // end am4core.ready()
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id = 'chartdiv'></div>
或者你可以跳过 togglable 并且只使用 hit
series.columns.template.events.on("hit", function(event){
series.columns.each(function(column){
//REM: Deactivate all non clicked columns
if(column !== event.target){
column.setState("default");
column.isActive = false
}
//REM: Toggle the clicked column
else{
column.isActive = !column.isActive
}
})
});