PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/FusionCharts/Contents/PHP_Drill.html

https://bitbucket.org/royrutto/climatepal
HTML | 177 lines | 176 code | 1 blank | 0 comment | 0 complexity | 03db7fd81ccdc197c773d9521c986545 MD5 | raw file
  1. <?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>FusionCharts v3 Documentation</title>
  6. <link rel="stylesheet" href="Style.css" type="text/css" />
  7. </head>
  8. <body>
  9. <table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
  10. <tr>
  11. <td><h2 class="pageHeader">Using FusionCharts with PHP &gt; Creating Drill-down charts </h2></td>
  12. </tr>
  13. <tr>
  14. <td valign="top" class="text"><p>In our previous example, we had used FusionCharts to plot a chart using data stored in database. We'll now extend that example itself to create a drill-down chart which can show more information.</p>
  15. <p>If you recall from previous example, we were showing the sum of factory output in a pie chart as under: </p></td>
  16. </tr>
  17. <tr>
  18. <td valign="top" class="text"><img src="Images/Code_DBOut.jpg" width="572" height="273" class="imageBorder" /></td>
  19. </tr>
  20. <tr>
  21. <td valign="top" class="text">In this example, we'll extend this example, so that when a user clicks on a pie slice for a factory, he can drill down to see date wise production for that factory. </td>
  22. </tr>
  23. <tr>
  24. <td valign="top" class="text">&nbsp;</td>
  25. </tr>
  26. <tr>
  27. <td valign="top" class="header">Setting up the pie chart for Link </td>
  28. </tr>
  29. <tr>
  30. <td valign="top" class="text">To set up the pie chart to enable links for drill-down involves just minor tweaking of our previous <span class="codeInline">BasicDBExample.php</span>. We basically need to add the <span class="codeInline">link</span> attribute for each<span class="codeInline"> &lt;set&gt;</span> element. We create a new page <span class="codeInline">Default.php</span> from the previous page in <span class="codeInline">DBExample</span> folder with the following code changes:</td>
  31. </tr>
  32. <tr>
  33. <td valign="top" class="codeBlock">
  34. <p>&lt;?php<br />
  35. <span class="codeComment">
  36. //We've included ../Includes/FusionCharts.php and ../Includes/DBConn.php, which contains<br />
  37. //functions to help us easily embed the charts and connect to a database.</span><br />
  38. include(&quot;../Includes/FusionCharts.php&quot;);<br />
  39. include(&quot;../Includes/DBConn.php&quot;);<br />
  40. ?&gt;<br />
  41. &lt;HTML&gt;<br />
  42. &lt;HEAD&gt;<br />
  43. &nbsp;&nbsp;&nbsp;&lt;TITLE&gt; FusionCharts - Database and Drill-Down Example &lt;/TITLE&gt;<br />
  44. &nbsp;&nbsp;&nbsp;&lt;SCRIPT LANGUAGE=&quot;Javascript&quot; SRC=&quot;../../FusionCharts/FusionCharts.js&quot;&gt;&lt;/SCRIPT&gt;<br />
  45. &lt;/HEAD&gt;<br />
  46. &lt;BODY&gt;<br />
  47. &lt;?php<br />
  48. &nbsp;&nbsp;&nbsp;<span class="codeComment">//In this example, we show how to connect FusionCharts to a database.<br />
  49. &nbsp;&nbsp;&nbsp;//For the sake of ease, we've used a MySQL database containing two<br />
  50. &nbsp;&nbsp;&nbsp;//tables.<br />
  51. <br />
  52. &nbsp;&nbsp;&nbsp;//Connect to database</span><br />
  53. &nbsp;&nbsp;&nbsp;$link = connectToDB();<br />
  54. </p>
  55. &nbsp;&nbsp;&nbsp;<span class="codeComment">'strXML will be used to store the entire XML document generated</span><br />
  56. <p> &nbsp;&nbsp;&nbsp;<span class="codeComment">'Generate the chart element</span><br />
  57. &nbsp;&nbsp;&nbsp;$strXML = &quot;&lt;chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units' &gt;&quot;;<br />
  58. <br />
  59. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Fetch all factory records</span><br />
  60. &nbsp;&nbsp;&nbsp;$strQuery = &quot;select * from Factory_Master&quot;;<br />
  61. &nbsp;&nbsp;&nbsp;$result = mysql_query($strQuery) or die(mysql_error());<br />
  62. <br />
  63. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Iterate through each factory</span><br />
  64. &nbsp;&nbsp;&nbsp;if ($result) {<br />
  65. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while($ors = mysql_fetch_array($result)) {<br />
  66. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">'Now create second query to get details for this factory</span><br />
  67. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$strQuery = &quot;select sum(Quantity) as TotOutput from Factory_Output where FactoryId=&quot; . $ors['FactoryId'];<br />
  68. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result2 = mysql_query($strQuery) or die(mysql_error());<br />
  69. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ors2 = mysql_fetch_array($result2);<br />
  70. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;//Generate &lt;set label='..' value='..' link='..' /&gt;<br />
  71. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Note that we're setting link as Detailed.php?FactoryId=&lt;&lt;FactoryId&gt;&gt; and then URL Encoding it</span><br />
  72. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>$strXML .=&quot;&lt;set label='&quot; . $ors['FactoryName'] . &quot;' value='&quot; . $ors2['TotOutput'] . &quot;' link='&quot; . urlencode(&quot;Detailed.php?FactoryId=&quot; . $ors['FactoryId']) . &quot;'/&gt;&quot;;</strong><br />
  73. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;//free the resultset</span><br />
  74. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mysql_free_result($result2);<br />
  75. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
  76. &nbsp;&nbsp;&nbsp;}<br />
  77. &nbsp;&nbsp;&nbsp;mysql_close($link);<br /><br />
  78. &nbsp;&nbsp;&nbsp;<span class="codeComment">//Finally, close &lt;chart&gt; element</span><br />
  79. &nbsp;&nbsp;&nbsp;$strXML .=&quot;&lt;/chart&gt;&quot;;<br />
  80. <br />
  81. &nbsp;&nbsp;&nbsp;<span class="codeComment">//Create the chart - Pie 3D Chart with data from $strXML</span><br />
  82. &nbsp;&nbsp;&nbsp;echo renderChart(&quot;../../FusionCharts/Pie3D.swf&quot;, &quot;&quot;, $strXML, &quot;FactorySum&quot;, 600, 300, false, false);<br />
  83. ?&gt;<br />
  84. &lt;/BODY&gt;<br />
  85. &lt;/HTML&gt;</p></td>
  86. </tr>
  87. <tr>
  88. <td valign="top" class="text"><p>As you can see in the code above, we're doing the following:</p>
  89. <ol>
  90. <li>Include <span class="codeInline">FusionCharts.js</span> JavaScript class and <span class="codeInline">FusionCharts.php</span> , to enable easy embedding of FusionCharts.</li>
  91. <li>We then include <span class="codeInline"> DBConn.php</span>, which contains connection parameters to connect to MySQL database. </li>
  92. <li>Thereafter, we generate the XML data document by iterating through the recordset. We store the XML data in <span class="codeInline">strXML</span> variable. To each <span class="codeInline">&lt;set&gt;</span> element, we add the <span class="codeInline">link</span> attribute, which points to <span class="codeInline">Detailed.php</span> - the page that contains the chart to show details. We pass the factory id of the respective factory by appending it to the link. We finally URL Encode the link, which is a very important step. </li>
  93. <li>Finally, we render the chart using <span class="codeInline">renderChart()</span> method and pass <span class="codeInline">strXML</span> as <span class="codeInline">dataXML</span>. </li>
  94. </ol>
  95. <p>Let's now shift our attention to <span class="codeInline">Detailed.php</span> page. </p></td>
  96. </tr>
  97. <tr>
  98. <td valign="top" class="text">&nbsp;</td>
  99. </tr>
  100. <tr>
  101. <td valign="top" class="header">Creating the detailed data chart page </td>
  102. </tr>
  103. <tr>
  104. <td valign="top" class="text">The page <span class="codeInline">Detailed.php</span> contains the following code: </td>
  105. </tr>
  106. <tr>
  107. <td valign="top" class="codeBlock">
  108. <p>&lt;?php<br />
  109. <span class="codeComment">
  110. //We've included ../Includes/FusionCharts.php and ../Includes/DBConn.php, which contains<br />
  111. //functions to help us easily embed the charts and connect to a database.</span><br />
  112. include(&quot;../Includes/FusionCharts.php&quot;);<br />
  113. include(&quot;../Includes/DBConn.php&quot;);<br />
  114. ?&gt;<br />
  115. &lt;HTML&gt;<br />
  116. &lt;HEAD&gt;<br />
  117. &nbsp;&nbsp;&nbsp;&lt;TITLE&gt; FusionCharts - Database and Drill-Down Example &lt;/TITLE&gt;<br />
  118. &nbsp;&nbsp;&nbsp;&lt;SCRIPT LANGUAGE=&quot;Javascript&quot; SRC=&quot;../../FusionCharts/FusionCharts.js&quot;&gt;&lt;/SCRIPT&gt;<br />
  119. &lt;/HEAD&gt;<br />
  120. &lt;BODY&gt;
  121. <br />
  122. &lt;?php<br />
  123. &nbsp;&nbsp;<span class="codeComment">&nbsp;//This page is invoked from Default.php. When the user clicks on a pie<br />
  124. &nbsp;&nbsp;&nbsp;//slice in Default.php, the factory Id is passed to this page. We need<br />
  125. &nbsp;&nbsp;&nbsp;//to get that factory id, get information from database and then show<br />
  126. &nbsp;&nbsp;&nbsp;//a detailed chart.<br />
  127. <br />
  128. &nbsp;&nbsp;&nbsp;//First, get the factory Id</span><br />
  129. &nbsp;<span class="codeComment">&nbsp;&nbsp;//Request the factory Id from Querystring</span><br />
  130. &nbsp;&nbsp;&nbsp;$FactoryId = $_GET['FactoryId'];<br />
  131. <br />
  132. &nbsp;<span class="codeComment">&nbsp;&nbsp;//Connect to database</span><br />
  133. &nbsp;&nbsp;&nbsp;$link = connectToDB();<br />
  134. <br />
  135. &nbsp;<span class="codeComment">&nbsp;&nbsp;//$strXML will be used to store the entire XML document generated</span><br />
  136. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Generate the chart element string</span><br />
  137. &nbsp;&nbsp;&nbsp;$strXML = &quot;&lt;chart palette='2' caption='Factory &quot; . $FactoryId . &quot; Output ' subcaption='(In Units)' xAxisName='Date' showValues='1' labelStep='2' &gt;&quot;;<br />
  138. <br />
  139. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Now, we get the data for that factory</span><br />
  140. &nbsp;&nbsp;&nbsp;$strQuery = &quot;select * from Factory_Output where FactoryId=&quot; . $FactoryId;<br />
  141. &nbsp;&nbsp;&nbsp;$result = mysql_query($strQuery) or die(mysql_error());<br />
  142. <br />
  143. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Iterate through each factory</span><br />
  144. &nbsp;&nbsp;&nbsp;if ($result) {<br />
  145. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while($ors = mysql_fetch_array($result)) {<br />
  146. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;//Here, we convert date into a more readable form for set label.</span><br />
  147. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$strXML .=&quot;&lt;set label='&quot; . datePart(&quot;d&quot;,$ors['DatePro']) . &quot;/&quot; . datePart(&quot;m&quot;,$ors['DatePro']) . &quot;' value='&quot; . $ors['Quantity'] . &quot;'/&gt;&quot;; <br />
  148. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
  149. &nbsp;&nbsp;&nbsp;}<br />
  150. &nbsp;&nbsp;&nbsp;mysql_close($link);<br /><br />
  151. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Close &lt;chart&gt; element</span><br />
  152. &nbsp;&nbsp;&nbsp;$strXML .=&quot;&lt;/chart&gt;&quot;;<br />
  153. <br />
  154. &nbsp;&nbsp;<span class="codeComment">&nbsp;//Create the chart - Column 2D Chart with data from $strXML</span><br />
  155. &nbsp;&nbsp;&nbsp;echo renderChart(&quot;../../FusionCharts/Column2D.swf&quot;, &quot;&quot;, $strXML, &quot;FactoryDetailed&quot;, 600, 300, false, false);<br />
  156. ?&gt;<br />
  157. &lt;/CENTER&gt;<br />
  158. &lt;/BODY&gt;<br />
  159. &lt;/HTML&gt;</p> </td>
  160. </tr>
  161. <tr>
  162. <td valign="top" class="text"><p>In this page, we're:</p>
  163. <ol>
  164. <li>Including <span class="codeInline">FusionCharts.js</span> JavaScript class and <span class="codeInline">FusionCharts.php</span> , to enable easy embedding of FusionCharts.</li>
  165. <li>Requesting the factory id for which we've to show detailed data. This data was sent to us as querystring, as a part of pie chart link.</li>
  166. <li>We get the requisite data for this factory from database and then convert it into XML using string concatenation.</li>
  167. <li>Finally, we render a Column 2D chart using <span class="codeInline">renderChart()</span> method to show detailed data.</li>
  168. </ol>
  169. <p>When you now run the app, you'll see the detailed page as under: </p></td>
  170. </tr>
  171. <tr>
  172. <td valign="top" class="text"><img src="Images/Code_Drill.jpg" width="599" height="292" class="imageBorder" />&nbsp;</td>
  173. </tr>
  174. </table>
  175. </body>
  176. </html>