gpt4 book ai didi

javascript - Amcharts 4 : How to highlight bar/column on click of bar/column in bar plots

转载 作者:行者123 更新时间:2023-12-03 07:05:01 28 4
gpt4 key购买 nike

我试图在 Amchart 4 中点击条形图/列时突出显示条形图/列。使用下面的代码,我正在获取当前点击的条形图/列的值,但使用的是 column.isActive 属性,条形/列未突出显示。

我找到了这个 Amchart official Documentation链接,但它是关于点击轴标签。我正在尝试实现相同但点击条形/列,而不是轴标签。

这是代码实现:

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()

现在,我也找到了这个stackoverflow发布链接,但这与 Amchart 版本 3 库有关,我使用的是 Amchart 4。

请查看此jfiddle实现。

最佳答案

对于每个定义,您不需要 hit 监听器,只有当您想要在激活被点击的列时停用其他列时才需要。您只需要使列可切换 并定义处于事件 状态的实际属性。

您必须使列可切换。

series.columns.template.togglable = true;

这会将事件状态从 false 切换到 truetogglable

状态激活时的属性

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
}
})
});

关于javascript - Amcharts 4 : How to highlight bar/column on click of bar/column in bar plots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62850948/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com