我正在尝试制作一个 amcharts 图表,其中的列链接到自定义 URL,并希望 URL 在新选项卡/窗口中打开。我尝试将此代码添加到图形对象中,但它不起作用,它正在同一选项卡/窗口中打开链接 -
"listeners": [{
"event": "clickItem",
"method": function(event) {
window.open(event.serialDataItem.dataContext.url, '_blank');
}
}],
请告诉我我做错了什么。我不想使用 Jquery,而且我是 javascript 新手。
这是我的片段 -
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"dataProvider": [{
"country": "USA",
"visits": 2025,
"url": "https://en.wikipedia.org/wiki/United_States"
}, {
"country": "China",
"visits": 1882,
"url": "https://en.wikipedia.org/wiki/China"
}, {
"country": "Japan",
"visits": 1809,
"url": "https://en.wikipedia.org/wiki/Japan"
}, {
"country": "Germany",
"visits": 1322,
"url": "https://en.wikipedia.org/wiki/Germany"
}, {
"country": "France",
"visits": 1114,
"url": "https://en.wikipedia.org/wiki/France"
}, {
"country": "India",
"visits": 984,
"url": "https://en.wikipedia.org/wiki/India"
}, {
"country": "Spain",
"visits": 711,
"url": "https://en.wikipedia.org/wiki/Spain"
}],
"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",
"listeners": [{
"event": "clickItem",
"method": function(event) {
window.open(event.serialDataItem.dataContext.url, '_blank');
}
}],
"urlField": "url"
}],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "country",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
}
});
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
最佳答案
您可以使用urlTarget如:
var chart = AmCharts.makeChart("chartdiv", {
"graphs": [{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "visits",
"urlField": "url",
"urlTarget": "_blank"
}],
...
};
关于javascript - Amcharts 图表 - 图表列到自定义 URL 的超链接以在新选项卡/窗口中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898995/