/    Sign up×
Community /Pin to ProfileBookmark

What would be the best way to change colour of provinces or states on a map.

Hello,

I am fairly new to JavaScript/AJAX but I am hoping to find a solution for a project I am working on.

At present I have an image of a map of Canada
(similar to: [url]http://www.cid-bdc.ca/unemployment/images/mapBackground.jpg[/url])

At present I am working on a web application that shows certain statistics about each province when one is clicked on. I have read a bit into SVG and have come up with an image map of sorts using SVG and polygon’s.

[code=html]
<polygon class=”province” points=”20,155 3,351 125,366 98,291 127,215 20,155″ onclick=”setProv(‘British Columbia’);” title=”British Columbia” />
<polygon class=”province” points=”128,215 100,295 126,367 160,375 191,232 128,215″ onclick=”setProv(‘Alberta’);” title=”Alberta” />
<polygon class=”province” points=”190,233 162,374 235,386 241,241 190,233″ onclick=”setProv(‘Saskatchewan’);” title=”Saskatchewan” />
<polygon class=”province” points=”241,240 235,384 287,385 287,335 330,282 290,239 241,240″ onclick=”setProv(‘Manitoba’);” title=”Manitoba” />
<polygon class=”province” points=”331,280 288,337 287,391 340,399 363,386 389,419 415,425 409,471 476,404 430,398 413,342 385,325 382,298 331,280″ onclick=”setProv(‘Ontario’);” title=”Ontario” />
<polygon class=”province” points=”387,194 429,392 437,400 482,397 519,320 570,292 575,260 514,300 485,262 510,254 474,195 469,225 444,200 387,194″ onclick=”setProv(‘Quebec’);” title=”Quebec” />
<polygon class=”province” points=”538,341 517,360 544,380 565,355 538,344″ onclick=”setProv(‘New Brunswick’);” title=”New Brunswick” />
<polygon class=”province” points=”564,355 553,388 568,396 600,339 587,328 583,350 564,356″ onclick=”setProv(‘Nova Scotia’);” title=”Nova Scotia” />
<polygon class=”province” points=”559,344 555,351 575,351 577,342 559,344″ onclick=”setProv(‘Prince Edward Island’);” title=”Prince Edward Island” />
<polygon class=”province” points=”473,187 514,253 490,265 511,288 585,261 590,319 643,289 614,269 589,258 554,230 516,220 473,187″ onclick=”setProv(‘Newfoundland and Labrador’);” title=”Newfoundland and Labrador” />
[/code]

Now, what I would like to do is change the colour of the province based on the statistics which are loaded.

For example: If the user were to want to find unemployment rates by province, I would like to be able to shade the provinces with the colours based on the current unemployment rate for that province.

I am able to load and display these rates in a table, and currently I also have them stored in an array.

At the moment I can think of two ways to do this, of which I have had mixed results.

[B]Method 1:[/B] Using JavaScript to adjust the fill colour and opacity of the polygon’s.

This is the code I have been using for this method:

[CODE]
function colorizeProvs()
{
for(i = 2; i <= 11; i++)
{
var data = provData[i].split(“,”);
var provName = data[0];
var provRate = data[dateVal];
var opacity = provRate * 0.01 * 5;
document.getElementById(provName).style.opacity=opacity;
document.getElementById(provName).style.filter = “alpha(opacity=” + opacity + “);”;
document.getElementById(provName).style.fill = “RED”;

}//end for
}//end of colorizeProvs
[/CODE]

However, as the provinces are quite complex in shape and have quite “messy” lines, this method provides block like shapes which don’t line up with the provinces properly.

[B]Method 2:[/B]Using photoshop to create images of each province in one of 5 colours (for different ranges in data).

For example:
Unemployment Rate: 0.0% – 5.0% gets one colour.
Unemployment Rate: 5.1% – 10.0% gets a slightly darker version of this colour
etc.

The problem with this is that the shading is not as precise, it doesn’t show changes of 1-2% unless they fall over a range break, and the other problem with this has been my implementation.

Because each province is shaded individually, I have not been able to code an adequete solution for overlaying the province images with the shading overtop of the “complete map” background image so that they line up properly AND the polygon’s and other SVG points do not get adjusted or covered by these images.

Can anyone offer and pointers or other methods that may work in a situation like this?

Thanks for all your help!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@007JulienFeb 01.2012 — Just an old page which run only with Internet Explorer... It uses gif images and opacity.

It's always possible to build colors like on this page... But I have never managed to marry both techniques...
Copy linkTweet thisAlerts:
@xelawhoFeb 02.2012 — you might find that using google geocharts takes alot of the back-end work out of what you are trying to do.
Copy linkTweet thisAlerts:
@jalarieFeb 08.2012 — Did you intentionally leave out Northwest Territories, Nunavut, and Yukon?
Copy linkTweet thisAlerts:
@DaveRichauthorFeb 08.2012 — Unfortunately I do not have any data available for the territories/ Nunavut.
Copy linkTweet thisAlerts:
@jalarieFeb 08.2012 — Unfortunately I do not have any data available for the territories/ Nunavut.[/QUOTE]

What data do you need? Perhaps I can help you get it.
Copy linkTweet thisAlerts:
@jalarieFeb 08.2012 — Are these of any help?
<i>
</i>&lt;polygon class="province" points="92,50 20,155 105,200 102,70 92,50" onclick="setProv('Yukon');" title="Yukon" /&gt;
&lt;polygon class="province" points="102,70 105,200 241,241 250,184 163,116 102,70" onclick="setProv('Northwest Territories');" title="Northwest Territories" /&gt;
&lt;polygon class="province" points="176,90 163,116 250,184 241,241 286,241 330,202 474,177 341,0 252,0 220,100 176,90" onclick="setProv('Nunavut');" title="Nunavut" /&gt;


By the way, where did you get that rather nice map? My I use it?
Copy linkTweet thisAlerts:
@DaveRichauthorFeb 09.2012 — Thanks, but I need the unemployment data for the Yukon, NWT and Nunavut, I have the CSV for the other provinces here: http://www.cid-bdc.ca/unemployment/data/provinces_ur.csv

Feel free to use any of it as you see fit!
×

Success!

Help @DaveRich spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...