In this section, we will present an example that employs both database and image maps.
This example displays a bar chart that shows the revenue for the last 10 years. When the user clicks on a bar, it will "drill down" to another bar chart showing the monthly revenue for the year represented by the bar clicked. All data come from a database.
The code that creates the clickable bar chart for the last 10 years are as follows.
[File: cfdemo/dbdemo3.cfm]
<!--- Read the revenue from 1990 to 2001 and into arrays ---> <cfquery name="selectedData" datasource="cdsample"> Select Sum(Software + Hardware + Services) As annualRevenue, Year(TimeStamp) As Year From revenue Where Year(TimeStamp) >= 1990 And Year(TimeStamp) <= 2001 Group By Year(TimeStamp) Order By Year(TimeStamp) </cfquery> <cfset revenue = selectedData["annualRevenue"].toArray()> <cfset timestamp = selectedData["Year"].toArray()> <cfscript> // ChartDirector for ColdFusion API Access Point cd = CreateObject("java", "ChartDirector.CFChart"); // Create a XYChart object of size 600 x 360 pixels c = cd.XYChart(600, 360); // Set the plotarea at (60, 40) and of size 480 x 280 pixels. Use a vertical gradient color from // light blue (eeeeff) to deep blue (0000cc) as background. Set border and grid lines to white // (ffffff). c.setPlotArea(60, 40, 480, 280, c.linearGradientColor(60, 40, 60, 280, "0xeeeeff", "0x0000cc"), -1, "0xffffff", "0xffffff"); // Add a title to the chart using 18pt Times Bold Italic font c.addTitle("Annual Revenue for Star Tech", "Times New Roman Bold Italic", 18); // Add a multi-color bar chart layer using the supplied data layer = c.addBarLayer3(revenue); // Use glass lighting effect with light direction from the left layer.setBorderColor(cd.Transparent, cd.glassEffect(cd.NormalGlare, cd.Left)); // Set the x axis labels c.xAxis().setLabels(timestamp); // Set y-axis tick density to 30 pixels. ChartDirector auto-scaling will use this as the guideline // when putting ticks on the y-axis. c.yAxis().setTickDensity(30); // Synchronize the left and right y-axes c.syncYAxis(); // Set the y axes titles with 10pt Arial Bold font c.yAxis().setTitle("USD (Millions)", "Arial Bold", 10); c.yAxis2().setTitle("USD (Millions)", "Arial Bold", 10); // Set all axes to transparent c.xAxis().setColors(cd.Transparent); c.yAxis().setColors(cd.Transparent); c.yAxis2().setColors(cd.Transparent); // Set the label styles of all axes to 8pt Arial Bold font c.xAxis().setLabelStyle("Arial Bold", 8); c.yAxis().setLabelStyle("Arial Bold", 8); c.yAxis2().setLabelStyle("Arial Bold", 8); // Create the image chart1URL = c.makeSession(GetPageContext(), "chart1"); // Create an image map for the chart imageMap = c.getHTMLImageMap("dbdemo3a.cfm", "", "title='{xLabel}: US$ {value|0}M'"); </cfscript> <html> <cfoutput> <body style="margin:5px 0px 0px 5px"> <div style="font-size:18pt; font-family:verdana; font-weight:bold"> Database Clickable Charts </div> <hr style="border:solid 1px ##000080" /> <div style="font-size:10pt; font-family:verdana; width:600px; margin-bottom:20px"> The example demonstrates creating a clickable chart using data from a database. Click on a bar below to "drill down" onto a particular year. <br /><br /> <a href='viewsource.cfm?file=#CGI.SCRIPT_NAME#'> View source code </a> </div> <img src="getchart.cfm?#chart1URL#" border="0" usemap="##map1"> <map name="map1"> #imageMap# </map> </body> </cfoutput> </html> |
The above code first performs a database query and read the data into arrays. It then uses the data to create a bar chart. The image map for the chart is created using BaseChart.getHTMLImageMap with "dbdemo3a.cfm" as the handler.
When the user clicks on the bar chart, "dbdemo3a.cfm" will be invoked with a number of HTTP query parameters to indicate which bar the user has clicked. In particular, the xLabel parameter will contain the x-axis label for the bar clicked. With this parameter, "dbdemo3a.cfm" can determine which year the user has clicked. It can then query the database for the data in that year, and produces the bar chart for that year.
In this example, "dbdemo3a.cfm" will produce another clickable chart using "xystub.cfm" as the handler.
[File: cfdemo/dbdemo3a.cfm]
<!--- The selected year should be in a query parameter called "xLabel" ---> <cfset selectedYear = IIf(IsDefined("URL.xLabel"), "URL.xLabel", "2001")> <!--- Query data from the selected year and read them into arrays ---> <cfquery name="selectedData" datasource="cdsample"> SELECT * FROM Revenue WHERE Year(timeStamp) = #selectedYear# Order By timeStamp </cfquery> <cfset software = selectedData["Software"].toArray()> <cfset hardware = selectedData["Hardware"].toArray()> <cfset services = selectedData["Services"].toArray()> <cfscript> // ChartDirector for ColdFusion API Access Point cd = CreateObject("java", "ChartDirector.CFChart"); // A utility to allow us to create arrays with data in one line of code function Array() { var result = ArrayNew(1); var i = 0; for (i = 1; i LTE ArrayLen(arguments); i = i + 1) result[i] = arguments[i]; return result; } // Create a XYChart object of size 600 x 360 pixels c = cd.XYChart(600, 360); // Set the plotarea at (60, 50) and of size 480 x 270 pixels. Use a vertical gradient color from // light blue (eeeeff) to deep blue (0000cc) as background. Set border and grid lines to white // (ffffff). c.setPlotArea(60, 50, 480, 270, c.linearGradientColor(60, 50, 60, 270, "0xeeeeff", "0x0000cc"), -1, "0xffffff", "0xffffff"); // Add a title to the chart using 15pt Times Bold Italic font c.addTitle("Global Revenue for Year " & selectedYear, "Times New Roman Bold Italic", 18); // Add a legend box at (60, 25) (top of the plotarea) with 9pt Arial Bold font c.addLegend(60, 25, False, "Arial Bold", 9).setBackground(cd.Transparent); // Add a line chart layer using the supplied data layer = c.addLineLayer2(); layer.addDataSet(software, "0xffaa00", "Software").setDataSymbol(cd.CircleShape, 9); layer.addDataSet(hardware, "0x00ff00", "Hardware").setDataSymbol(cd.DiamondShape, 11); layer.addDataSet(services, "0xff0000", "Services").setDataSymbol(cd.Cross2Shape(), 11); // Set the line width to 3 pixels layer.setLineWidth(3); // Set the x axis labels. In this example, the labels must be Jan - Dec. labels = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"); c.xAxis().setLabels(labels); // Set y-axis tick density to 30 pixels. ChartDirector auto-scaling will use this as the guideline // when putting ticks on the y-axis. c.yAxis().setTickDensity(30); // Synchronize the left and right y-axes c.syncYAxis(); // Set the y axes titles with 10pt Arial Bold font c.yAxis().setTitle("USD (Millions)", "Arial Bold", 10); c.yAxis2().setTitle("USD (Millions)", "Arial Bold", 10); // Set all axes to transparent c.xAxis().setColors(cd.Transparent); c.yAxis().setColors(cd.Transparent); c.yAxis2().setColors(cd.Transparent); // Set the label styles of all axes to 8pt Arial Bold font c.xAxis().setLabelStyle("Arial Bold", 8); c.yAxis().setLabelStyle("Arial Bold", 8); c.yAxis2().setLabelStyle("Arial Bold", 8); // Create the image and save it in a temporary location chart1URL = c.makeSession(GetPageContext(), "chart1"); // Create an image map for the chart imageMap = c.getHTMLImageMap("xystub.cfm", "", "title='{dataSetName} @ {xLabel} = USD {value|0}M'"); </cfscript> <html> <cfoutput> <body style="margin:5px 0px 0px 5px"> <div style="font-size:18pt; font-family:verdana; font-weight:bold"> Database Clickable Charts </div> <hr style="border:solid 1px ##000080" /> <div style="font-size:10pt; font-family:verdana; width:600px; margin-bottom:20px"> You have click the bar for the year #selectedYear#. Below is the "drill-down" chart showing the monthly details. <br /><br /> <a href='viewsource.cfm?file=#CGI.SCRIPT_NAME#'> View source code </a> </div> <img src="getchart.cfm?#chart1URL#" border="0" usemap="##map1"> <map name="map1"> #imageMap# </map> </body> </cfoutput> </html> |
For demo purpose, "xystub.cfm" simply displays information on what is clicked. It's source code is as follows.
[File: cfdemo/xystub.cfm]
<html> <cfoutput> <body style="margin:5px 0px 0px 5px"> <div style="font-size:18pt; font-family:verdana; font-weight:bold"> Simple Clickable XY Chart Handler </div> <hr style="border:solid 1px ##000080" /> <div style="font-size:10pt; font-family:verdana; margin-bottom:20px"> <a href="viewsource.cfm?file=#CGI.SCRIPT_NAME#">View Source Code</a> </div> <div style="font-size:10pt; font-family:verdana;"> <b>You have clicked on the following chart element :</b><br /> <ul> <li>Data Set : #URL.dataSetName#</li> <li>X Position : #URL.x#</li> <li>X Label : #URL.xLabel#</li> <li>Data Value : #URL.value#</li> </ul> </div> </body> </cfoutput> </html> |