|
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 > ASP > DB_Drill folder. |
Let's discuss the code from the main file : Index.asp |
<%@ LANGUAGE="VBScript" %>
<!-- #include file="../Includes/DBConn.asp" -->
<!-- #include file="../Includes/FusionMaps.asp" -->
<HTML>
<HEAD>
<TITLE>FusionMaps v3 - Database Example</TITLE>
<%
%>
<SCRIPT LANGUAGE="Javascript" SRC="../JSClass/FusionMaps.js"></SCRIPT>
</HEAD>
<BODY>
<%
dim strXML, sumdata
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 = strXML + "<colorRange>"
strXML = strXML + "<color minValue='0' maxValue='0.50' displayValue='0%25 to 0.50%25 of total' color='D64646' />"
strXML = strXML + "<color minValue='0.50' maxValue='1' displayValue='0.50%25 to 1%25 of total' color='F6BD0F' />"
strXML = strXML + "<color minValue='1' maxValue='3' displayValue='1%25 to 3%25 of total' color='8BBA00' />"
strXML = strXML + "<color minValue='3' maxValue='10' displayValue='3%25 or above of total' color='AFD8F8' />"
strXML = strXML + "</colorRange>"
strXML = strXML + "<data>"
sumdata=0
strQuery = "select sum(data) as datap from fcmap_distribution"
set rs = Server.CreateObject("ADODB.Recordset")
rs.open strQuery,oConn
if rs.bof=false then
sumdata=rs("datap")
end if
strQuery = "select Internal_Id, (sum(data) / " & sumdata & ")*100 as datap from fcmap_distribution group by Internal_Id"
set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.open strQuery,oConn
if rs1.bof=false then
do while not rs1.eof
strQuery ="select * from fcmap_master where Internal_Id='" + rs1("Internal_Id") + "'"
set rs2 = Server.CreateObject("ADODB.Recordset")
rs2.open strQuery,oConn
strXML = strXML & "<entity id='" & rs1("Internal_Id") & "' value='" & round(rs1("datap"),2) & "' link='Detailed.asp?Internal_Id=" & rs1("Internal_Id") & "%26map=" & rs2("map_swf") & "' />"
rs1.movenext
loop
strXML = strXML + "</data>"
strXML = 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 = strXML + "</definition><application><apply toObject='PLOT' styles='animX,animY' /> <apply toObject='LABELS' styles='myShadow,animAlpha' /></application></styles>"
strXML = strXML + "</map>"
Call renderMap("../../Maps/FCMap_USA.swf", "", strXML, "Map1Id", "750", "460", false, false)
else
response.write "<h3>No Records</h3>"
end if
set rs=nothing
set rs1=nothing
set rs2=nothing
oConn.close
set oConn=nothing
%>
</BODY>
</HTML> |
|
Let's now see through the lines: |
|
- As always, first we include FusionMaps.asp and FusionMaps.js files that help in easy map rendering.
|
- We then include DBConn.asp, which contains connection parameters to connect to Access database.
|
<!-- #include file="../Includes/DBConn.asp" -->
<!-- #include file="../Includes/FusionMaps.asp" -->
...
<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 also started the root <map> element providing the map properties.
|
dim strXML, sumdata
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 US Map.
|
strXML = strXML + "<colorRange>"
strXML = strXML + "<color minValue='0' maxValue='0.50' displayValue='0%25 to 0.50%25 of total' color='D64646' />"
strXML = strXML + "<color minValue='0.50' maxValue='1' displayValue='0.50%25 to 1%25 of total' color='F6BD0F' />"
strXML = strXML + "<color minValue='1' maxValue='3' displayValue='1%25 to 3%25 of total' color='8BBA00' />"
strXML = strXML + "<color minValue='3' maxValue='10' displayValue='3%25 or above of total' color='AFD8F8' />"
strXML = 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). We did this using SQL Query : "select sum(data) as datap from fcmap_distribution".
- 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 recordset rs1 using the SQL query - "select Internal_Id, (sum(data) / " & sumdata & ")*100 as datap from fcmap_distribution group by Internal_Id'. We again used fcmap_distribution table here.
- 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 recordset and create entity element for each state.
|
strXML = strXML & "<data>"if rs.bof=false then
sumdata=rs("datap")
end if
strXML = strXML & "<entity id='" & rs1("Internal_Id") & "' value='" & round(rs1("datap"),2) & "' link='Detailed.asp?Internal_Id=" & rs1("Internal_Id") & "%26map=" & rs2("map_swf") & "' />"
...
strXML = 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"),2) and set to value attribute. We set the dacimal limit of the value to 2 places using ASP 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 asp file. This we did in the entity entry itself as shown above using link attribute. Let's 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.asp 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 recordset rs2 is created for each iteration which stores the map file name of the releted Internal_Id in "map_swf" field. Recordset 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.
|
Call renderMap("../../Maps/FCMap_USA.swf", "", strXML, "Map1Id", "750", "460", false, false) |
|
Here is the screenshot of the US map that we just created extracting data from database. |
|