1

I see that I can use the CartoDB wizard to color polygons I have based on a column value. This works great except that it only colors 6 or so polygons and the rest are lumped as “others”.

I see that I can edit the CSS and add the other polygons one at a time. My map has about 250 of these, so doing it one at a time isn’t practical. Is there a more automated way to, say, randomly assign a color from the palette to all the polygons based on a value in the table? It’s fine if there are duplicate colors.

2 Answers 2

1

I don't know if there is an elegant way to do that but there is a workaround for it using SQL and CartoCSS:

1) apply a query like

select *, cartodb_id%PALETTE_COLOR_NUMER as pal from your_table

2) apply a cartocss like this:

#layer { ... 
[pal = 0] { polygon-fill: color1; } 
[pal = 1] { polygon-fill: color2; } 
[pal = 2] { polygon-fill: color3; } 
[pal = 3] { polygon-fill: color4; }
[pal = 4] { polygon-fill: color5; }
 ...
}
2
  • This still leaves me writing this out 700 (not the 250 I initially thought) for each polygon. And where are the colors defined? Do I need to define what color1, etc. actually are with this?
    – jonmrich
    Commented Feb 2, 2015 at 12:31
  • I don't understand, why 700? you need to write this as many times as color in the palette you have. color1, color2 and so on are the colors in your palette, you need to replace them Commented Feb 3, 2015 at 10:59
0

I wrote a little script for this that solves the problem by generating all the CSS properties that you can then cut and paste into CartDB:

for (rep = 1; rep <= 800; rep++) {
var color = Math.floor(Math.random()*16777215).toString(16);
if (rep < 10)  {
document.write('#mydiv[territory="T00' + rep + '"] {<br> polygon-fill:#' +color+';<br>}<br>');
}
  if (rep >=10 && rep <100)
 {
document.write('#mydiv[territory="T0' + rep + '"] {<br> polygon-fill:#' +color+';<br>}<br>');
}
if (rep >= 100)  {
document.write('#mydiv[territory="T' + rep + '"] {<br> polygon-fill:#' +color+';<br>}<br>');
}
}

The extra if statements were necessary in my case because I had territories formatted like this "T001" and "T099".

4
  • in this way two things can happen: reach cartocss limit for maps api and that the visualization does not make any sense for the user Commented Feb 6, 2015 at 15:06
  • Actually, this worked perfectly after I put the resulting CSS properties into the map editor. The visualization makes perfect sense, as it colors a specific geographic area (a sales territory) with a specific color to separate it visually from other territories.
    – jonmrich
    Commented Feb 6, 2015 at 15:24
  • yes, the software actually works but I'm not so sure about the visualization, could you share a fragment? Commented Feb 6, 2015 at 18:47
  • also if you want to separate visually you can use lines, it will work in the same way with better ratio information/pixel. If you want still use polygon-fill you should use a set of colors with the same lightness (colorbrewer2.org) so the viz does make sense. Commented Feb 6, 2015 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.