SVG Won"t Color?
am 06.01.2010 17:27:49 von Alice Wei
--_cc6d607d-ec52-4e08-9a8c-3782ee4d9ce4_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Hi,
=20
I have the code as in the following=2C and I am trying to colorize the ma=
p. The SVG File is located here: http://upload.wikimedia.org/wikipedia/comm=
ons/5/5f/USA_Counties_with_FIPS_and_names.svg. Looks like when I tried to m=
odify the contents of the line=2C it does not seem to take into affect. Thu=
s=2C my map does not get colorized. Do I have to save the file here? Or=2C =
is there something else I have missed here?=20
//We are outputting an SVG
header("Content-type: image/svg+xml")=3B
$array=3D array()=3B
$array2=3D array()=3B
$array3=3D array()=3B
$array4=3D array()=3B
#Map Colors
$colors_array=3D array("#F1EEF6"=2C"#D4B9DA"=2C"#C994C7"=2C"#DF65B0"=2C"#DD=
1C77"=2C"#980043")=3B
$file =3D file("unemployment09.csv")=3B
foreach ($file as $line) {
$chars =3D preg_split("/=2C/"=2C $line)=3B
$unemployment_rate =3D $chars[12]=3B
array_push($array=2C$unemployment_rate)=3B =20
}
//Calculate the number of elements in array
$total_num =3D count($array)=3B
#Load the Map
$ourFileName=3D "USA_Counties_with_FIPS_and_names.svg"=3B
$fh =3D fopen($ourFileName=2C "r") or die("Can't open file")=3B
$contents =3D fread($fh=2Cfilesize($ourFileName))=3B
$lines2=3D file($ourFileName)=3B
#Color the counties based on unemployment rate
for ($i=3D0=3B$i<$total_num=3B$i++){
switch($array[$i]){
=20
case ($array[$i] > 10):
$color =3D 5=3B
break=3B =20
case ($array[$i] > 8):
$color =3D 4=3B
break=3B =20
=20
case ($array[$i] > 6):
$color =3D 3=3B
break=3B =20
case ($array[$i] > 4):
$color =3D 2=3B
break=3B =20
case ($array[$i] > 2):
$color =3D 1=3B
break=3B =20
default:
$color=3D 0=3B
break;
}
$color_class=3D $colors_array[$color]=3B
array_push($array4=2C$color_class)=3B
}
=20
foreach ($lines2 as $line_num =3D> $line2) {
$line_add_one =3D $line_num + 1=3B
if(preg_match("/
=20
$rest =3D substr($lines2[$line_add_one]=2C0=2C-3)=3B
$colors_style =3D "=3Bcolor:" . $array4[$line_add_one]=3B
$rest =3D $rest . $colors_style . "\"";
}
array_push($array3=2C$lines2[$line_add_one])=3B =20
}
echo $contents=3B
fclose($fh);
?>
Thanks for your help.
Alice
=20
____________________________________________________________ _____
Hotmail: Trusted email with Microsoft=92s powerful SPAM protection.
http://clk.atdmt.com/GBL/go/196390706/direct/01/=
--_cc6d607d-ec52-4e08-9a8c-3782ee4d9ce4_--
Re: SVG Won"t Color?
am 06.01.2010 17:31:52 von Ashley Sheridan
--=-+FIDHtgP4lYIeeXyVb9U
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
On Wed, 2010-01-06 at 11:27 -0500, Alice Wei wrote:
> Hi,=20
> =20
> I have the code as in the following, and I am trying to colorize the ma=
p. The SVG File is located here: http://upload.wikimedia.org/wikipedia/comm=
ons/5/5f/USA_Counties_with_FIPS_and_names.svg. Looks like when I tried to m=
odify the contents of the line, it does not seem to take into affect. Thus,=
my map does not get colorized. Do I have to save the file here? Or, is the=
re something else I have missed here?=20
>=20
>
>=20
> //We are outputting an SVG
> header("Content-type: image/svg+xml");
>=20
> $array=3D array();
> $array2=3D array();
> $array3=3D array();
> $array4=3D array();
>=20
> #Map Colors
> $colors_array=3D array("#F1EEF6","#D4B9DA","#C994C7","#DF65B0","#DD1C77",=
"#980043");
>=20
> $file =3D file("unemployment09.csv");
> foreach ($file as $line) {
> $chars =3D preg_split("/,/", $line);
> $unemployment_rate =3D $chars[12];
> array_push($array,$unemployment_rate); =20
> }
>=20
> //Calculate the number of elements in array
> $total_num =3D count($array);
>=20
> #Load the Map
> $ourFileName=3D "USA_Counties_with_FIPS_and_names.svg";
> $fh =3D fopen($ourFileName, "r") or die("Can't open file");
> $contents =3D fread($fh,filesize($ourFileName));
> $lines2=3D file($ourFileName);
>=20
> #Color the counties based on unemployment rate
> for ($i=3D0;$i<$total_num;$i++){
>=20
> switch($array[$i]){
> =20
> case ($array[$i] > 10):
> $color =3D 5;
> break; =20
>=20
> case ($array[$i] > 8):
> $color =3D 4;
> break; =20
> =20
> case ($array[$i] > 6):
> $color =3D 3;
> break; =20
>=20
> case ($array[$i] > 4):
> $color =3D 2;
> break; =20
>=20
> case ($array[$i] > 2):
> $color =3D 1;
> break; =20
>=20
> default:
> $color=3D 0;
> break;=20
>=20
> }
> $color_class=3D $colors_array[$color];
> array_push($array4,$color_class);
> }
> =20
> foreach ($lines2 as $line_num =3D> $line2) {
>=20
> $line_add_one =3D $line_num + 1;
> if(preg_match("/
> =20
> $rest =3D substr($lines2[$line_add_one],0,-3);
> $colors_style =3D ";color:" . $array4[$line_add_one];
> $rest =3D $rest . $colors_style . "\"";=20
> }
> array_push($array3,$lines2[$line_add_one]); =20
> }
>=20
> echo $contents;
> fclose($fh);=20
>=20
> ?>
>=20
> Thanks for your help.
>=20
> Alice
> =20
> ____________________________________________________________ _____
> Hotmail: Trusted email with Microsoftâ€=99s powerful SPAM protection.
> http://clk.atdmt.com/GBL/go/196390706/direct/01/
You're reading the SVG contents into $contents, and also into $lines2 as
an array. You seem to be making changes to the array version on a line
by line basis, but then you output $contents, which is the original
contents of the SVG file! You need to output the elements of $lines2,
either one-by-one, or concatenate the array into a string and output
that.
Also, as a bit of advice, I'd try to make your variable names a bit more
descriptive than $lines2, $line2, etc. Trying to read through the code
and remember what variable does what could be a massive headache later
on!
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-+FIDHtgP4lYIeeXyVb9U--