2

我正在尝试绘制从数据库中获取的数据,但我没有弄清楚如何处理 json 数组并将其传递给绘图插件(jvectorMap)

这是我的json数组的结构

{
"countries":[
{
"cname":"Albania",
"ccode":"AL",
"name":"John",
"percent":"20"
},
{
"cname":"Austria",
"ccode":"AT",
"name":"Doe",
"percent":"30"
}
]
}

HTML 中的 JavaScript

<script>
        var dataC = "<?php echo $mapData?>";
          $(function(){
            $('#world-map').vectorMap({
              map: 'world_mill_en',
              series: {
                regions: [{
                  values: dataC[ccode],
                  scale: ['#C8EEFF', '#0071A4'],
                  normalizeFunction: 'polynomial'
                }]
              },
              onLabelShow: function(e, el, code){
                el.html(el.html()+dataC[ccode]+dataC[name]+dataC[percent]+dataC[cname]);
              }
            });
          });
        </script>

本质上,我希望根据ccode密钥中的 ISO 代码绘制我的数据。例如,当我指向地图时,我希望在标记中看到来自name和字段percentage的数据。cname谢谢。

4

1 回答 1

3
var dataC = {
    "countries": [
        {
        "cname": "Albania",
        "ccode": "AL",
        "name": "John",
        "percent": "20"},
    {
        "cname": "Austria",
        "ccode": "AT",
        "name": "Doe",
        "percent": "30"}
    ]
};

//---------------------------------------------------------------------------
// The data for the jVectorMap is expected to be an object with 
// member variables for each country code and an associated value. 
// You need to convert your `dataC` data into that format.
//---------------------------------------------------------------------------
var countryData = []; 
//for each country, set the code and value
$.each(dataC.countries, function() {
    countryData[this.ccode] = this.percent;
});

$(function() {
    $('#world-map').vectorMap({
        map: 'world_mill_en',
        series: {
            regions: [{
                values: countryData, //load the data
                scale: ['#C8EEFF', '#0071A4'],
                normalizeFunction: 'polynomial'}]
        },
        //-----------------------------------------------
        // changed to onRegionLabelShow from onLabelShow
        //-----------------------------------------------
        onRegionLabelShow: function(e, el, code) {
            //search through dataC to find the selected country by it's code
            var country = $.grep(dataC.countries, function(obj, index) {
                return obj.ccode == code;
            })[0]; //snag the first one
            //only if selected country was found in dataC
            if (country != undefined) { 
                el.html(el.html() + 
                        "<br/><b>Code: </b>" +country.ccode + 
                        "<br/><b>Name: </b>" + country.name + 
                        "<br/><b>Percent: </b>" + country.percent + 
                        "<br/><b>Country Name: </b>"+ country.cname);
            }
        }
    });
});​
于 2012-11-19T00:37:07.807 回答