6

I have searched high and low for a way to colorize parts of a map based on a csv or xml file. For instance to illustrate results of a election. Doing it by hand is both tedious

I imagine being able to attach a path to an variable and being able to change the background-image or the swatch based on a numeric value.

Has any one got a idea on how to approach this?

  • doing this is not really hard but a bit uncomfortably close to development. Thing is a colored map is very different form a pie chart and that in turn is different from a spiral timeline and that.... So we can easily asnwer a specific case but not do all of them. DOing this is not more than a 20 minute job. – joojaa Oct 25 '16 at 8:53
  • "Not more than 20 minute job" - is my famous last words :) – Silly-V Oct 25 '16 at 13:55
  • @Silly-V yes but in this case true. I timed it, im just trying to figure out how to decouple my properitary stuff, that i am not comfortable with sharing, from the loop. – joojaa Oct 25 '16 at 14:12
  • So did he send you the materials and you're working on it? I wouldn't be able to say a time estimate myself until I see what there's to work with. I'm curious as to how this map is made myself. – Silly-V Oct 25 '16 at 14:29
  • @Silly-V I can make a script thats file agnostic. Its not so hard to search for a name and pair up. – joojaa Oct 26 '16 at 4:09
4

Doing this is pretty simple. All you need is:

  • A data file in a CSV form with names and values.
  • Each region shape to be filled annotated by the name.

Then all you do is a loop over each column, although you may want 2 loops if you want to normalize data ranges. Here is the final result of a tutorial that I prepared for students in my university:

#target illustrator


(function () { // protect namespace

var doc = app.activeDocument;
var input = read_data_CSV();

var data = input[0];
var max = input[1];
var min = input[2];

for ( i = 0; i < data.length; i++ ) {  
    var name = data[i][0];
    var value = (data[i][1] - min)/(max-min)*100;

    try{
        var pathitem = doc.pageItems.getByName(name);

        var col = new CMYKColor();
        col.black = 0;
        col.cyan = 100 - value;
        col.magenta = value;
        col.yellow = 0;

        pathitem.fillColor = col;
        if (pathitem.typename === "CompoundPathItem")
            pathitem.pathItems[0].fillColor = col; 

    } catch(err) {
       alert(name+ " errors!");  
    }  
}


/**
 * Prompt user for a CSV file, two columns 
 * name and value. The CSV file is assumed
 * to be in form:
 *
 * name;10.5
 *
 * Where the column separator is ";" and 
 * the decimal separator is ".".
 *
 * @returns {array} containing the data, max
 *                  and min.
 */
function read_data_CSV(){
    var file = File.openDialog('data', 'center:*.csv');
    file.open( 'r' );  

    var max = Number.MIN_VALUE;
    var min = Number.MAX_VALUE;
    var data = [];

    while( !file.eof ) {  
        var input = file.readln().split( ';' );
        var numeric = parseFloat(input[1]);

        if (numeric > max) max = numeric;
        if (numeric < min) min = numeric;

        data.push([input[0], numeric]);
    }

    return [data, max, min];
}

})(); //run on load

Map

Image 1: Example map mapped with values form csv file

To test this on some real map data Ive prepared following files:

  • map.ai, a map containing the map of municipalities in Southern Finland. The map is based on "NLS Yleiskarttarasteri 1:1 000 000, 1.4.2013" you may share or mix this data as long as you attribute the source and list version where it was taken from.
  • population_31_12_2016_logarithmic.csv, containing the population data for the region in December 31 2016.
  • percentage_of_unemployment_2015.csv, containing unemployment data in year 2015.

Be sure to have the file to modify active. This is a example only I would need to be much more robust to share it as something else.

2

From a purely level-of-effort perspective, consider trying Tableau. I'm pretty sure you can pick it up faster than a script can be written. You can also export your charts/maps to PDF for fine tuning in Illustrator.

Even the free Public version provides some pretty robust mapping functionality. As long as your dataset is named something meaningful, it can recognize geographic territories pretty well out of the box.

Here's a tutorial to give you sense of what's possible: https://www.interworks.com/blog/ccapitula/2015/02/25/tableau-essentials-formatting-tips-maps

NB: All Tableau Public charts/maps are public by default. Also, I did not create that tutorial.

0

You may be able to use a self-made, custom script in conjunction with some spreadsheet data to do this. Either you can write yourself, or one will come as an answer in this thread, or you can ask the people on the Adobe Illustrator Scripting forum to write one for you.

When creating the custom script, your basic goal will be to attach the data to the art shape by means of some 'key' such as the path's name in the layers panel, or using the attribute notes or even using the variables feature and attaching a different variable to every path. In the spreadsheet you'll have to include this reference key in one column and the corresponding color data such as a swatch name or color values in another column. Your custom script will need to parse the CSV data and colorize the paths.

If you can provide a link to your files, or use screenshots to explain the structure of your spreadsheet and document, it would be the next step.

  • 2
    I dont know if i like this answer as it relies way too much on external links without actually telling why i would like to use the link and what is behind the link, just some wague references that it might help or not. Still thinking if this needs to be downvoted or not. I mean you come off as saying ask somewhere else. – joojaa Oct 25 '16 at 5:02
  • Perhaps I can re-word this answer to clearly state which paragraph goes with which and what is precisely behind the links. However, as the question is "how to approach this", I believe this answers how to approach it - and asking "somewhere else" can be a legitimate answer since it doesn't literally say ask anywhere else but points to a specific place. – Silly-V Oct 25 '16 at 6:57
  • 1
    Well no, OP asked here lets solve it here if possible. – joojaa Oct 25 '16 at 7:55
  • 1
    Would you not say that it would involve at least some custom scripting? And if so - there's hardly enough information about what the specifics are regarding the work document, which in turn begs the question of those specifics as they have to be elaborated on - we simply have little to work with here. – Silly-V Oct 25 '16 at 8:14
  • @silly I'll be glad to submit more details. In regards to how many people who may have a need for such a solution I am actually surprised that there aren't any tutorials or threads out there. There must be thousands of graphic designers who painstakingly go through updating maps and graphics for newspaper and magazines by manual typing and coloring maps. But this method is so prone to error and time consuming. With Google Fusion and other web tools available you would think it would lead to the old graphic world to follow suit with new tools. – Jakob Thuemoes Oct 25 '16 at 8:25
0

I'm not familiar with Illustrator's scripting / variable abilities, but I did recently learn about the ability to create simple charts in Illustrator. It may be simpler to create the map and color the values manually. Tedious? Yes. But it gets the results you want.

Other alternatives: Tableau as suggested, or the new features in Excel using the Map tool from the Excel store will do the job quickly.

I just started a job using Cognos 10 and I'll be working on maps in the coming weeks, so if you have Cognos Report Studio you could go that route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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