3

我正在使用 jVectorMap 通过着色显示国家/地区值(即,国家/地区根据其值具有不同的颜色)。一切正常,除非某些国家/地区包含在数据集中。那么这些国家就不会像应该的那样被着色(事实上,它们根本不会被着色)。

我已将巴林 (BH) 和新加坡 (SG) 确定为破坏地图的两个国家/城邦。看起来这些国家似乎不包括在地图上。他们不在那里,我并不感到惊讶。但是,我希望地图不会失败。

JavaScript 错误'this.elements[...].element' is null or not an object在于以下内容(请参阅下面代码中有关失败的注释)。

jvm.DataSeries.prototype={
    //...
    setValues:function(e){
        var t=Number.MIN_VALUE,n=Number.MAX_VALUE,r,i,s={};
        if(!this.params.min||!this.params.max){
            for(i in e)
                r=parseFloat(e[i]),r>t&&(t=e[i]),r<n&&(n=r);
            this.params.min||this.scale.setMin(n),this.params.max||this.scale.setMax(t),this.params.min=n,this.params.max=t
        }
        for(i in e)
            //FAILS ON THE FOLLOWING LINE
            r=parseFloat(e[i]),r?s[i]=this.scale.getValue(r):s[i]=this.elements[i].element.style.initial[this.params.attribute];
        this.setAttributes(s),this.values=e
    },
    //...
},

有没有办法解决这个问题?我宁愿更改 jVectorMap 代码,或者必须在我的 Java 代码中执行以下操作:

if (!countryCode.equals("BH") && !countryCode.equals("SG")) {
    countryValues.put(countryCode, countryValue);
}
4

1 回答 1

3

如果您想确保您的数据仅包含地图中的国家/地区代码,您可以在为地图设置数据之前使用地图国家/地区代码作为过滤条件过滤数据。

您可以定义一种方法,在该方法中传递数据,遍历每个项目并删除地图路径中不存在国家代码的条目:

function filterDataForMap(data, mapName){
    var filteredData = {};
    $.each(data,function(key, value){
       //Only add when the key (country code) exists in the map
       if(jvm.WorldMap.maps[mapName].paths[key]!==undefined) {
          filteredData[key] = value;        
       }                
    });
    return filteredData;
}

应用于地图的创建:

$('#map').vectorMap({
        map: 'world_mill_en',
        series: {
          regions: [
            {
              scale: colorScale,
              attribute: 'fill',
              normalizeFunction: 'polynomial',
              values: filterDataForMap(mapData, 'world_mill_en') //filter data
            }]
        }
 });
于 2012-11-17T02:20:59.273 回答