我有each
这样的声明:
$.each(data, function(i, value) {
sublayers.push({
sql: "SELECT " + firstSel2 + ", cartodb_id, the_geom_webmercator FROM full_data_for_testing_deid_2 where " + firstSel2 + "='" + value.attri_1 + "'",
cartocss: "#full_data_for_testing_deid_2 {marker-fill:"+color_here+";marker-width:5;marker-line-width: 0.2;marker-line-color:#fff;marker-allow-overlap: true;}"
});
});
在cartocss部分中,有一个变量color_here
。这就是我需要替换的东西。这需要是来自像这样的对象的十六进制值:
var myColors = ["#364C57",
"#666DD6",
"#867EAD",
"#1A3D76",
"#7F787F",
"#35304C",
"#1D5772",
"#15446B",
"#7382C0",
"#484A48",
"#454252",
"#333C6F"];
我需要的是每个语句为它循环的每个value
提取其中一种颜色(从顶部开始)而不重复。假设在我上面的对象中永远不会有比我们的颜色更多的循环。有没有办法做到这一点,以确保颜色不会重复给定的每个语句?我可以提出从列表中随机选择的内容,但这会导致重复的可能性。
答案 0 :(得分:1)
似乎您只需根据当前索引从数组中选择颜色:
var color_here = myColors[i];
不是吗?将它放在sublayers.push()
之前。
答案 1 :(得分:0)
也许这就是你想要的:
var color_curr;
var myColors = ["#364C57",
"#666DD6",
"#867EAD",
"#1A3D76",
"#7F787F",
"#35304C",
"#1D5772",
"#15446B",
"#7382C0",
"#484A48",
"#454252",
"#333C6F"];
$.each(data, function(i, value) {
var color = myColors[i];
if(color!=color_curr){
sublayers.push({
sql: "SELECT " + firstSel2 + ", cartodb_id, the_geom_webmercator FROM full_data_for_testing_deid_2 where " + firstSel2 + "='" + value.attri_1 + "'",
cartocss: "#full_data_for_testing_deid_2 {marker-fill:"+color_here+";marker-width:5;marker-line-width: 0.2;marker-line-color:#fff;marker-allow-overlap: true;}"
});
}
color_curr=color;
});