Re: SVG Won't Color?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2010-01-06 at 11:27 -0500, Alice Wei wrote:

> Hi, 
>   
>   I have the code as in the following, and I am trying to colorize the map. The SVG File is located here: http://upload.wikimedia.org/wikipedia/commons/5/5f/USA_Counties_with_FIPS_and_names.svg. Looks like when I tried to modify 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 there something else I have missed here? 
> 
> <?php
> 
> //We are outputting an SVG
> header("Content-type: image/svg+xml");
> 
> $array= array();
> $array2= array();
> $array3= array();
> $array4= array();
> 
> #Map Colors
> $colors_array= array("#F1EEF6","#D4B9DA","#C994C7","#DF65B0","#DD1C77","#980043");
> 
>  $file = file("unemployment09.csv");
>  foreach ($file as $line) {
>     $chars = preg_split("/,/", $line);
>     $unemployment_rate = $chars[12];
>     array_push($array,$unemployment_rate);   
>   }
> 
> //Calculate the number of elements in array
> $total_num = count($array);
> 
> #Load the Map
> $ourFileName= "USA_Counties_with_FIPS_and_names.svg";
> $fh = fopen($ourFileName, "r") or die("Can't open file");
> $contents = fread($fh,filesize($ourFileName));
> $lines2= file($ourFileName);
> 
> #Color the counties based on unemployment rate
> for ($i=0;$i<$total_num;$i++){
> 
>   switch($array[$i]){
>    
>    case ($array[$i] > 10):
>    $color = 5;
>    break;   
> 
>    case ($array[$i] > 8):
>    $color = 4;
>    break;   
>  
>    case ($array[$i] > 6):
>    $color = 3;
>    break;   
> 
>    case ($array[$i] > 4):
>    $color = 2;
>    break;   
> 
>    case ($array[$i] > 2):
>    $color = 1;
>    break;   
> 
>    default:
>    $color= 0;
>    break; 
> 
>   }
>   $color_class= $colors_array[$color];
>   array_push($array4,$color_class);
> }
>    
> foreach ($lines2 as $line_num => $line2) {
> 
>   $line_add_one = $line_num + 1;
>   if(preg_match("/<path/",$line2)) {
>    
>    $rest = substr($lines2[$line_add_one],0,-3);
>    $colors_style = ";color:" . $array4[$line_add_one];
>    $rest = $rest . $colors_style . "\""; 
>   }
>   array_push($array3,$lines2[$line_add_one]);   
> }
> 
> echo $contents;
> fclose($fh); 
> 
> ?>
> 
> Thanks for your help.
> 
> Alice
>  		 	   		  
> _________________________________________________________________
> Hotmail: Trusted email with Microsoft’s 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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux