2

DB から取得したデータをプロットしようとしていますが、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>

基本的に、キーの ISO コードに基づいてデータをプロットしたいと考えていccodeます。たとえば、マップ上をポイントすると、マーカーに からのデータが表示され、nameフィールドも表示されます。ありがとう。percentagecname

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 に答える