gpt4 book ai didi

php - 将 php 数据绘制到 Highmap : HighCharts

转载 作者:行者123 更新时间:2023-11-29 16:08:43 25 4
gpt4 key购买 nike

您好,我正在使用 Highmaps JS,我想做的是将数据库中的数据绘制到 highMaps 中。这是我尝试过的。

PHP 代码:

$json_data=array(); 

foreach($result as $rec)
{
$json_array['Country']=$rec['user_country'];
$json_array['persons']=$rec['usercountry'];

$json_array['code']=$keyvalue[$rec['user_country']];

array_push($json_data,$json_array);
} $data = json_encode($json_data) ;

这是我正在使用的脚本:

<script type="text/javascript">

var data = <?php echo $data ; ?>
Highcharts.mapChart('world-map', {
chart: {
borderWidth: 1,
map: 'custom/world'
},

title: {
text: 'World population 2013 by country'
},

subtitle: {
text: 'Demo of Highcharts map with bubbles'
},

legend: {
enabled: false
},
mapNavigation: {
enabled: true,
buttonOptions: {
verticalAlign: 'bottom'
}
},
series: [{
name: 'Countries',
color: '#E0E0E0',
enableMouseTracking: false
}, {
type: 'mapbubble',
name: 'Population 2016',
joinBy: ['iso-a3', 'code3'],
data: data,
minSize: 4,
maxSize: '12%',
tooltip: {
pointFormat: '{point.properties.hc-a2}: {point.z} thousands'
}
}]
});
});
</script>

所以我想要的是用数据显示国家气泡。但页面没有显示任何内容。全是空白。

我得到的 JSON 值是:

[{"Country":"Australia","persons":"5","CountryCode":"AU"}, 
{"Country":"India","persons":"8","CountryCode":"IN"},
{"Country":"Mexico","persons":"4","CountryCode":"MX"},
{"Country":"Spain","persons":"2","CountryCode":"ES"},
{"Country":"United States","persons":"4","CountryCode":"US"}]

我在控制台中看到的错误是:

Uncaught SyntaxError: Unexpected identifier

最佳答案

Highcharts 期望每种类型的系列数据都有特定的格式。对于mapbubble,它需要{name, z}。您正在尝试提供{Country, people}。此外,您的加入方式不正确。

这是一个简单的 JavaScript 代码,它将您的 JSON 数据转换为可由 highcharts 摄取的数据:

data = data.map(function(el){
return {name: el.Country, z: parseInt(el.persons), 'iso-a2': el.CountryCode}
})

这使得连接键为:“iso-a2”。

这使得一切看起来像这样:

var data = [{"Country":"Australia","persons":"5","CountryCode":"AU"}, 
{"Country":"India","persons":"8","CountryCode":"IN"},
{"Country":"Mexico","persons":"4","CountryCode":"MX"},
{"Country":"Spain","persons":"2","CountryCode":"ES"},
{"Country":"United States","persons":"4","CountryCode":"US"}]
data = data.map(function(el){
return {name: el.Country, z: parseInt(el.persons), 'iso-a2': el.CountryCode}
})

Highcharts.mapChart('world-map', {
chart: {
borderWidth: 1,
map: 'custom/world'
},

title: {
text: 'World population 2013 by country'
},

subtitle: {
text: 'Demo of Highcharts map with bubbles'
},

legend: {
enabled: false
},
mapNavigation: {
enabled: true,
buttonOptions: {
verticalAlign: 'bottom'
}
},
series: [{
name: 'Countries',
color: '#E0E0E0',
enableMouseTracking: false
}, {
type: 'mapbubble',
name: 'Population 2016',
joinBy: 'iso-a2',//'iso-a3', 'code3'],
data: data,
minSize: 4,
maxSize: '12%',
tooltip: {
pointFormat: '{point.properties.name}: {point.z} thousands'
}
}]
});
<script src="https://code.highcharts.com/maps/highmaps.js"></script>
<script src="https://code.highcharts.com/mapdata/custom/world.js"></script>
<div id='world-map'>

</div>

工作 JSFiddle 示例: https://jsfiddle.net/ewolden/yqm78dtp/19/

ma​​pbubble.data 上的 API: https://api.highcharts.com/highmaps/series.mapbubble.data

关于php - 将 php 数据绘制到 Highmap : HighCharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55491502/

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