Updating SVG map example from Visualizing Data
in
Programming Questions
•
2 years ago
I'm having trouble updating the SVG map example that matches data from a TSV file to color states. Here's what I have. However, it draws the entire map including the map as the same color including filling the lines separating Alaska and Hawaii.
I also opened it in Illustrator and ensured it was not grouped. <path id="state name">
I also referenced this which helped considerably:
Thanks for your help!
- PShape svg;
- Table data;
- float dataMin = -7;
- float dataMax = 11;
- void setup () {
- size(1368, 936);
- svg = loadShape("map.svg");
- data = new Table("random.tsv");
- }
- void draw() {
- background(255);
- noStroke();
- smooth();
- svg.disableStyle();
- int rowCount = data.getRowCount();
- for (int row = 0; row < rowCount; row++) {
- String abbrev = data.getRowName(row);
- PShape state = svg.getChild(abbrev);
- if (state == null) {
- println("no state found for " + abbrev);
- }
- else {
- float value = data.getFloat(row, 1);
- if (value >= 0) {
- float amt = norm(value, 0, dataMax);
- color c = lerpColor(#FFFFFF, #221177, amt);
- fill(c);
- }
- else {
- float amt = norm(value, 0, dataMin);
- color c = lerpColor(#FFFFFF, #992211, amt);
- fill(c);
- }
- shape(svg, 30, 30, width - (width*.10), height - (height*.10));
- }
- }
- }
1