Skip to content
SyntaxLogoFree edited this page Jul 7, 2014 · 4 revisions

In choropleth maps, each map region will be colored according to some data value. Again, the data object should be a dictionary of region keys to whatever you want. Kartograph will call the function for every region of the specified layer. If you leave out the layer parameter, the last added layer will be used.

data = { 'USA': 262373, 'CAN': 84833, .. }

map.choropleth({
   layer: 'countries',
   data: data,
   colors: function(d) {
      return '#f94'; // return color based on data value/object
   }
});

In cases where the data is not given in a single dictionary but a list of arrays or dictionaries, you can tell Kartograph how to map the data to the polygon ids by setting the key property.

data = [ ['USA', 262373], ['CAN', 84833], .. }

map.choropleth({
   data: data,
   key: 0,
   colors: function(d) { }
});

For more advanced color features like interpolation and color scales I recommend to use Chroma.js, which is kind of a side project of Kartograph.

data = { 'USA': 262373, 'CAN': 84833, .. }

colscale = new chroma.ColorScale({
    colors: ['#F7E1C5', '#6A000B'],
    limits: chroma.limits(data, 'quant', 5)
});

map.choropleth({
    layer: 'countries',
    data: data,
    colors: function(d) {
        return colscale.getColor(d);
    }
});
Clone this wiki locally