我正在尝试使用amcharts创建图表。在那里,我将类别作为链接,我想在新的选项卡/窗口中打开。我添加了以下代码,它在新的选项卡/窗口中打开链接,但同时也在同一选项卡中打开链接。请告诉我我做错了什么。我不想使用Jquery,我是javascript的新手。
这是我在新标签页面中添加到javascript中用于打开类别链接的代码 -
window.open(event.serialDataItem.dataContext.url, '_blank');
这是最初的AmCharts代码集 - https://codepen.io/team/amcharts/pen/50c8b6a103e7126fac91ce40000192be
这是片段 -
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"addClassNames": true,
"titles": [{
"text": "Open this demo in a separate window for links to work"
}],
"dataProvider": [{
"country": "USA",
"visits": 2025,
"url": "https://codepen.io"
}, {
"country": "China",
"visits": 1882,
"url": "https://codepen.io"
}, {
"country": "Japan",
"visits": 1809,
"url": "https://codepen.io"
}, {
"country": "Germany",
"visits": 1322,
"url": "https://codepen.io"
}, {
"country": "UK",
"visits": 1122,
"url": "https://codepen.io"
}, {
"country": "France",
"visits": 1114,
"url": "https://codepen.io"
}, {
"country": "India",
"visits": 984,
"url": "https://codepen.io"
}, {
"country": "Spain",
"visits": 711,
"url": "https://codepen.io"
}, {
"country": "Netherlands",
"visits": 665,
"url": "https://codepen.io"
}, {
"country": "Russia",
"visits": 580,
"url": "https://codepen.io"
}, {
"country": "South Korea",
"visits": 443,
"url": "https://codepen.io"
}, {
"country": "Canada",
"visits": 441,
"url": "https://codepen.io"
}, {
"country": "Brazil",
"visits": 395,
"url": "https://codepen.io"
}],
"valueAxes": [{
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
}],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "visits"
}],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "country",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20,
"listeners": [{
"event": "clickItem",
"method": function(event) {
window.location.href = event.serialDataItem.dataContext.url;
window.open(event.serialDataItem.dataContext.url, '_blank');
}
}]
}
});
#chartdiv {
width: 100%;
height: 400px;
}
.amcharts-axis-label {
cursor: pointer;
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
答案 0 :(得分:1)
你应该删除该行..
window.location.href = event.serialDataItem.dataContext.url;
..来自 clickItem 监听器。
此行指示浏览器替换当前页面位置。基本上这一行会阻止下一行执行。
这应该有效:
"listeners": [{
"event": "clickItem",
"method": function(event) {
window.open(event.serialDataItem.dataContext.url, '_blank');
}
}]
它可能在Codepen环境中不起作用,但它应该在别处工作。