gpt4 book ai didi

javascript - 如何在点击功能之外获取值(value)

转载 作者:行者123 更新时间:2023-12-01 03:27:54 25 4
gpt4 key购买 nike

我的网页上有一个 map 的 div 和一个饼图的 div。 map 上有几个图钉,我想根据不同的点击刷新图表div。

下面是leaflet.js的点击函数:

var country = "";

map.on('click', function(e) {
country = "Worldwide";
alert("Set map to worldwide");
});

markerUS.on('click', function(e) {
country = "U.S.";
alert('U.S');
});

chart.js(饼图)中:

AmCharts.makeChart("chartdiv1", {
"type": "pie",
"angle": 12,
"balloonText": "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>",
"depth3D": 15,
"titleField": "category",
"valueField": "column-1",
"allLabels": [],
"balloon": {},
"legend": {
"enabled": true,
"align": "center",
"markerType": "circle"
},
"titles": [{
"id": "Title-1",
"size": 15,
"text": "Number of Projects distribution of " + country
}],
"dataProvider": [{
"category": "a",
"column-1": 8
},
{
"category": "b",
"column-1": 6
},
{
"category": "c",
"column-1": 2
},
{
"category": "d",
"column-1": "3"
},
{
"category": "e",
"column-1": "4"
},
{
"category": "f",
"column-1": "2"
}
]
});

在“标题”内,我根据我单击的引脚将国家/地区设置为变量。

但是我发现国家/地区未定义,并且 leaflet.js 中的值国家/地区似乎没有传递给图表。为什么?如何纠正这个问题并实现该功能?

最佳答案

您获得了未定义的国家/地区,因为它的值仅在用户单击 map 或制造商时才会初始化。您可以尝试像这样更新您的代码

map.on('click', function(e) {
var country = "Worldwide";
makeChart(country);
});

markerUS.on('click', function(e) {
var country = "U.S.";
makeChart(country);
});

function makeChart(country) {
AmCharts.makeChart("chartdiv1", {
// your chart option ....
});
}

关于javascript - 如何在点击功能之外获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44716330/

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