Graphing Your Data with CFCHART
It turns out a picture isn't just worth a thousands words; it's also worth a few lines of ColdFusion.
By now you've discovered that most of your time writing ColdFusion is spent working with data. So far you've learned to use Dreamweaver's code generation tools to insert, update, and display data in text and in tables. The Web, however, is a visual and dynamic medium. You'll often find that you can communicate more clearly by presenting some of your data graphically instead of textually.
ColdFusion MX introduces a powerful, intuitive graphing engine with the <cfchart> tag. Dreamweaver doesn't include pre-built server behaviors to create ColdFusion graphs for you, so in this chapter you'll need to roll up your sleeves and write the code yourself. True to the CF spirit, you need to learn only three tags<cfchart>, <cfchartdata>, and <cfchartseries>to produce elegant, dynamic, charts.
In this chapter, you first learn how to build static charts using data that you provide. You also learn how to manipulate the appearance of charts to tell the story you want to tell. Next, you learn how to populate charts with query data in order to automate their creation. Finally, you learn how to include links within your charts to build graphical, interactive applications.
As with nearly all CF coding, the more SQL you understand, the better your charts will be. If you need a SQL refresher before building your first graph, please read Chapter 4: "Retrieving Database Data." Otherwise, let's get started.
Building Basic Charts
ColdFusion lets you build charts from data you enter manually, from queries, or from a combination of the two. There are two or three basic steps to building any chart in ColdFusion. First, you must create a container for your chart by using the <cfchart> tag. With this tag's attributes, you can specify the size and format of your chart, as well as many features of its appearance.
Second, you must specify which series of data you want to show by using the <cfchartseries> tag. This tag also allows you to specify the type of graph you want to build, such as a line graph or a pie graph.
Finally, you can manually assign points of data to track with the <cfchartdata> tag.
Keeping in mind the container metaphor, think of <cfchartseries> as containing the optional points of data specified with <cfchartdata>. Likewise, <cfchart> contains the <cfchartseries>. If it all sounds a bit confusing, it will become much clearer as soon as you start coding.
GUI or Hand Coding?
All the steps in this chapter use hand-coding techniques to create graphs and queries. If you're comfortable with the Tag Editor, query builders, and server behaviors, editing code by hand may seem like a nuisance at first. After all, if the tools are available, why not use them?
It's true that Dreamweaver does have an extraordinary GUI (Graphical User Interface), but the more time you spend creating your pages by hand, the better you'll understand the code itself. In addition, many people find typing code by hand faster than relying on the GUI. Ideally, you'll become adept at both methods and learn to use each one when it best serves your needs.
In this section, you build static charts using <cfchart>, <cfchartseries>, and <cfchartdata>. Don't be too concerned at first with the appearance of your charts; just let ColdFusion do the formatting for now.
To build a container for a static chart:
Create a new ColdFusion page, title it, and save it.
Choose View > Code.
After the opening <body> tag, type this code and press the spacebar:
As you learned in the last chapter, you can use Dreamweaver's Code Hint and Auto Tag completion features to help you write your code. While working through the following steps, keep in mind that you can either select your tag attributes using Dreamweaver's code completion tools, or you can type the attributes.
Insert this code:
<cfchart showborder="yes" chartheight="300" chartwidth="400" yaxistitle="Projected Population (Millions)" xaxistitle="Year"> </cfchart>
You've just specified that you want to create a chart that's 400 pixels wide and 300 pixels high. You've also specified that you want to label the vertical, or y-axis, as "Projected Population (Millions)," and the horizontal, or x-axis, as "Year."
Don't forget that <cfchart>, just like most other ColdFusion tags, requires a closing tag.
Instead of building the container <cfchart> by hand, you can also choose Insert > ColdFusion Advanced Objects > CFCHART. If you've forgotten how to insert ColdFusion Objects, please refer to Chapter 11: "Introducing Hand Coding CFML."
If you're upgrading from ColdFusion 5, you may be familiar with the <cfgraph> tag. This tag has been deprecated in ColdFusion MX and replaced with the much more powerful <cfchart>.
It's hard to appreciate ColdFusion graphs in grayscale. You really owe it to yourself to run the code samples included in this chapter to see the graphs in color.
If you don't have a large monitor, try switching to Dreamweaver's Code view when you're working with tags that don't have code generation tools.
To define a chart type with the <cfchartseries> tag:
In between the opening and closing <cfchart> tags, type this code:
<cfchartseries type="line" serieslabel="United States"> </cfchartseries>
You've now specified that you want a line chart labeled United States. Figure 13.1 shows the code you've entered up to this point. You are ready to test what you've built so far.
Figure 13.1 Place the <cfchartseries> tags within the opening and closing <cfchart> tags.
Choose File > Preview in Browser and select your browser of choice.
Wait for your browser to open and you get ... an empty black border. But something funny is going on here.
Move your mouse somewhere within the black border and right-click (Windows) or Control+click (Mac).
You'll see the Flash Player menu (Figure 13.2)! ColdFusion has created an empty graph. In essence, you've built a container for your graph with <cfchart> and told ColdFusion that you want to create a line chart with <cfchartseries>. The next step is to pour some data into the line chart.
Figure 13.2 ColdFusion exports only the chart's border if you don't put any data into it.
Be careful when typing your <cfchartseries> tag by hand; Dreamweaver does not automatically close this tag for you.
Consider the Source
In case you're wondering, the population numbers for the first part of this chapter come from the US Census Bureau. You can download an amazing array of statistics from the American FactFinder Web site (http://factfinder.census.gov). It's even possible to incorporate the stats into your own databases. Sounds like <cfchart> heaven!
To use <cfchartdata> to manually add data:
In between the opening and closing <cfchartseries> tags, type this code:
<cfchartdata item="2000" value="275">
Press F12 to preview your work in a browser.
There's a lonely square at the top of your chart (Figure 13.3). Let's agree that it's a graph, albeit an unimpressive one, and move on.
Figure 13.3 This chart has a single plot point.
In Dreamweaver's Code view, type this code after the first <cfchartdata> tag:
<cfchartdata item="2005" value="290"> <cfchartdata item="2010" value="310"> <cfchartdata item="2015" value="325"> <cfchartdata item="2020" value="350"> <cfchartdata item="2025" value="380"> <cfchartdata item="2030" value="410"> <cfchartdata item="2035" value="450"> <cfchartdata item="2040" value="500"> <cfchartdata item="2045" value="525"> <cfchartdata item="2050" value="550">
You've just typed quite a few lines of code. Figure 13.4 shows the entire code listing for the chart.
Figure 13.4 When you're entering data manually, accuracy is critical.
Save your file, and then view it in a browser.
You should see a line graph showing the population of the United States increasing from 275 million in 2000 to a projected 550 million in 2050 (Figure 13.5). Wow! And you thought space was already tight in the Hamptons.
Figure 13.5 ColdFusion automatically connects your points of data in a line chart.
<cfchartdata> doesn't require a closing tag.
A graph with too little information just isn't interesting. To give your viewers useful information, you need to show how the data changes over time or how one set of data compares to another.
To track more than one set of data:
After the first <cfchartseries> closing tag, type this code:
<cfchartseries type="line" serieslabel="Western Europe" markerstyle="triangle"> </cfchartseries>
You've just told ColdFusion that you want to add a second line to your existing graph, that you want to label it Western Europe, and that you want to mark the points of data with triangles. Remember, however, that <cfchartseries> is a container tag and does not contain any data until you specify otherwise.
Press F12 to preview the chart in your browser (Figure 13.6).
Figure 13.6 This chart has two series: one populated and one empty.
In between the opening and closing <cfchartseries> tags you just created, type this code:
<cfchartdata item="2000" value="390"> <cfchartdata item="2005" value="395"> <cfchartdata item="2010" value="400"> <cfchartdata item="2015" value="400"> <cfchartdata item="2020" value="400"> <cfchartdata item="2025" value="393"> <cfchartdata item="2030" value="390"> <cfchartdata item="2035" value="380"> <cfchartdata item="2040" value="372"> <cfchartdata item="2045" value="360"> <cfchartdata item="2050" value="355">
Figure 13.7 shows the entire code listing for the chart.
Figure 13.7 shows the entire code listing for the chart.
Save your work, and then view it in a browser.
The graph shows two series of connected points, one gently descending after leveling off, the other climbing sharply (Figure 13.8). Note that even in grayscale, you can distinguish Western Europe's line by the triangle markers.
Figure 13.8 This chart shows two series: both are populated.