|
In this page we will see the code used to achieve the Goal 1. Below is the important part of the code. |
|
Before moving further with the process of plotting data from database, we recommend to go through the dataXML and dataURL method to know the basics about FusionMap generation procedure. |
|
The code discussed here is present in Download Package > Code > PHP > DB_Drill folder. |
Let's discuss the code from the main file : Default.php |
<?php
include("../Includes/DBConn.php");
include("../Includes/FusionMaps.php");
?>
<HTML>
<HEAD>
<TITLE>FusionMaps v3 - Database Example</TITLE>
<?php
?>
<SCRIPT LANGUAGE="Javascript" SRC="../JSClass/FusionMaps.js"></SCRIPT>
</HEAD>
<BODY>
<?php
$strXML ="";
$sumData =0;
$link = connectToDB();
$strXML = "<map showCanvasBorder='0' borderColor='FFFFFF' connectorColor='000000' fillAlpha='80' hoverColor='FFFFFF' showBevel='0' numberSuffix='%25 of total US population' legendBorderColor='F1f1f1' hoverColor='FFFFFF' legendPosition='bottom'>";
$strXML .= "<colorRange>";
$strXML .= "<color minValue='0' maxValue='0.50' displayValue='0%25 to 0.50%25 of total' color='D64646' />";
$strXML .= "<color minValue='0.50' maxValue='1' displayValue='0.50%25 to 1%25 of total' color='F6BD0F' />";
$strXML .= "<color minValue='1' maxValue='3' displayValue='1%25 to 3%25 of total' color='8BBA00' />";
$strXML .= "<color minValue='3' maxValue='10' displayValue='3%25 or above of total' color='AFD8F8' />";
$strXML .= "</colorRange>";
$strXML .= "<data>";
$strQuery = "select sum(data) datap from fcmap_distribution";
$result3 = mysql_query($strQuery) or die(mysql_error());
$rs = mysql_fetch_array($result3);
if(count($rs)!=0)
$sumdata=$rs['datap'];
$strQuery = "select Internal_Id, sum(data) datap from fcmap_distribution group by Internal_Id";
$result = mysql_query($strQuery) or die(mysql_error());
if ($result) {
while($rs1 = mysql_fetch_array($result)) {
$strQuery ="select * from fcmap_master where Internal_Id='" . $rs1['Internal_Id'] . "'";
$result1 = mysql_query($strQuery) or die(mysql_error());
$rs2 = mysql_fetch_array($result1);
$strXML .= "<entity id='" . $rs1['Internal_Id'] . "' value='" . round((($rs1['datap'] / $sumdata) * 100),2) . "' link='Detailed.php?Internal_Id=" . $rs1['Internal_Id'] . "%26map=" . $rs2['map_swf'] . "' />";
}
$strXML .= "</data>";
$strXML .= "<styles><definition><style type='animation' name='animX' param='_xscale' start='0' duration='1' /><style type='animation' name='animY' param='_yscale' start='0' duration='1' /><style type='animation' name='animAlpha' param='_alpha' start='0' duration='1' /><style type='shadow' name='myShadow' color='FFFFFF' distance='1' />";
$strXML .= "</definition><application><apply toObject='PLOT' styles='animX,animY' /><apply toObject='LABELS' styles='myShadow,animAlpha' /></application></styles>";
$strXML .="</map>";
print renderMap("../../Maps/FCMap_USA.swf","",$strXML,"UsaMap", 750, 460,0,0);
}
else{
print '<h3>No Records</h3>';
}
mysql_close($link);
?>
</BODY>
</HTML> |
|
Let's now see through the lines: |
|
- As always, first we include FusionMaps.php and FusionMaps.js files that help in easy map rendering.
|
- We then include DBConn.php, which contains connection function connectToDB() to connect to database.
|
<?php
include("../Includes/DBConn.php");
include("../Includes/FusionMaps.php");
?>
...
<SCRIPT LANGUAGE="Javascript" SRC="../JSClass/FusionMaps.js"></SCRIPT> |
|
- Next , we create variables $strXML (to store XML data) and $sumdata to store sum of values from database.
- We connect to the database using connectToDB() function.
- We also start the root <map> element providing the map properties.
|
$strXML ="";
$sumData =0;
$link = connectToDB();
$strXML = "<map showCanvasBorder='0' borderColor='FFFFFF' connectorColor='000000' fillAlpha='80' hoverColor='FFFFFF' showBevel='0' numberSuffix='%25 of total US population' legendBorderColor='F1f1f1' hoverColor='FFFFFF' legendPosition='bottom'>"; |
|
- Next, we define color range for the main USA map.
|
$strXML .= "<colorRange>";
$strXML .= "<color minValue='0' maxValue='0.50' displayValue='0%25 to 0.50%25 of total' color='D64646' />";
$strXML .= "<color minValue='0.50' maxValue='1' displayValue='0.50%25 to 1%25 of total' color='F6BD0F' />";
$strXML .= "<color minValue='1' maxValue='3' displayValue='1%25 to 3%25 of total' color='8BBA00' />";
$strXML .= "<color minValue='3' maxValue='10' displayValue='3%25 or above of total' color='AFD8F8' />";
$strXML .= "</colorRange>"; |
|
Providing data |
|
- Here comes the database mining part where we start fetching data.
- First we initialize $sumdata to store sum total of all data found in the table - fcmap_distribution. That represents the total US population (ficticious of-course). This we did using SQL Query : "select sum(data) as datap from fcmap_distribution". We store the result in array $rs and access the value from it using $rs['datap'].
- Next , we fetch internal ID of each entity/state of US map and get the percentage of population of each entity/state against total US population. This is stored in array $rs1 using the SQL query - "select Internal_Id, sum(data) datap from fcmap_distribution group by Internal_Id". We again use fcmap_distribution table here.
- Iterating through each record stored in array we get total population of each state and divide it by sum total US population and multiply it with 100 to get the percentage value.
- Now we iterate through each record in the array and create entity element for each state.
|
$strXML .= "<data>";
if(count($rs)!=0)
$sumdata=$rs['datap'];
...
$strXML .= "<entity id='" . $rs1['Internal_Id'] . "' value='" . round((($rs1['datap'] / $sumdata) * 100),2) . "' link='Detailed.php?Internal_Id=" . $rs1['Internal_Id'] . "%26map=" . $rs2['map_swf'] . "' />";
}
$strXML .= "</data>";
|
- Here we extract internalID from $rs1["Internal_Id"] and set to id attribute.
- We get percentage of population of the state against total US population from round((($rs1['datap'] / $sumdata) * 100),2) and set to value attribute. We set the dacimal limit of the value to 2 places usingPHP function round().
|
|
Setting up the Links |
We also set up drill-down link to each entity. Each enity on click will drill down to another php file. This we did in the entity entry itself as shown above using link attribute. Lets see the how the link is structured.
...link='Detailed.asp?Internal_Id=" . $rs1["Internal_Id"] . "%26map=" .& $rs2["map_swf"]...
- All states/entities will drill-down to Detailed.php page.
- Each entity will pass a query string variable - Internal_Id whose value will be the internal id of that state. This is extracted out from the"Internal_id" field of the iterated record.
- Each entity will also pass the map file name to Detailed.asp page using querystirng variable -map. Note that another array $rs2 is created for each iteration which stores the map file name of the releted Internal_Id in "map_swf" field. array $rs2 is created from table fcmap_master using SQL query -"select * from fcmap_master where Internal_Id='" . $rs1['Internal_Id'] . "'".
|
|
Thus all entities are created. |
|
- Next we add styles like XScale, YScale, alpha & shadow styles to the map using <styles> element.
- Finally we call renderMap() function to render the map.
|
print renderMap("../../Maps/FCMap_USA.swf","",$strXML,"UsaMap", 750, 460,0,0); |
|
Here is the screenshot of the US map that we just created extracting data from database. |
|
|
|