将php数据绘制到Highmap:HighCharts中

问题描述 投票:0回答:1

您好我在使用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
php mysql json highcharts
1个回答
1
投票

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

这是一个简单的javascript代码,可以将您的JSON数据转换为可由highcharts提取的数据:

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

这使得join-by键成为:'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/

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

© www.soinside.com 2019 - 2024. All rights reserved.