3

使用 jquery 和 jqvmap,我在美国地图中设置了多个州的颜色。例如,要将所有以“A”开头的状态都涂成红色,这样可以:

jQuery('#vmap').vectorMap('set', 'colors', {al: 'red'});
jQuery('#vmap').vectorMap('set', 'colors', {ak: 'red'});
jQuery('#vmap').vectorMap('set', 'colors', {az: 'red'});
jQuery('#vmap').vectorMap('set', 'colors', {ar: 'red'});

有没有办法让它更短?我想这样做:

var astates = ["al", "ak", "az", "ar"];
for (var i = 0; i < astates.length; ++i) {
    jQuery('#vmap').vectorMap('set', 'colors', { 'astates[i]' : 'red'}); 
}

但这似乎不起作用。谢谢

4

3 回答 3

4
var fc = 'red'; // you'll need this if you'll want to change the whole group color someday
jQuery('#vmap').vectorMap('set', 'colors', {al: fc, ak: fc, az: fc, ar:fc});
于 2013-11-27T09:11:56.390 回答
4
colorsST = {};
var astates = ["al", "ak", "az", "ar"];
for (var i = 0; i < astates.length; ++i) {
    colorST[i] = 'red';
    jQuery('#vmap').vectorMap('set', 'colors', colorST); 
}
于 2013-10-28T02:12:10.620 回答
1

我正在使用此功能如下:

let colors =  < any > {};

let colors = {
    "zz": "#006491",
    "us": "#1a769f",
    "nl": "#bde6f9",
    "at": "#c0e8fa",
    "tr": "#c0e8fa",
    "ie": "#c0e9fb"
}

jQuery('#vmap').vectorMap('set', 'colors', colors);

并且可能你需要不同的颜色(我需要:))例如根据一些值你可以计算一些起始颜色并用最小值和最大值计算

let myValuesMap: Map<string, number> = new Map(); //{"zz" => 3011, "us" => 2669, "at" => 260, "nl" => 172, "tr" => 148, "ie"=>101}
let max = 0, min = Number.MAX_VALUE, startColor = [200, 238, 255], endColor = [0, 100, 145], colors = <any>{}, hex;

myValuesMap.forEach((value: number, key: string) => {
  if (value > max) { max = value }
  if (value < min) { min = value }
});

myValuesMap.forEach((value: number, key: string) => {
  if (value > 0) {
    colors[key] = '#';
    for (var i = 0; i < 3; i++) {
      hex = Math.round(startColor[i] + (endColor[i] - startColor[i]) * (value == max ? 1 : (value / (max - min)))).toString(16);
      if (hex.length == 1) { hex = '0' + hex; }
      colors[key] += (hex.length == 1 ? '0' : '') + hex;
    }
  }
});

jQuery('#vmap').vectorMap('set', 'colors', colors);
于 2019-11-22T11:09:13.760 回答