Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe ColdFusion

Graphing Your Data with CFCHART

Produce elegant, dynamic, charts with only three simple ColdFusion tags. Learn how to build static charts using data that you provide, to manipulate the appearance of charts, to populate charts with query data in order to automate their creation, and to include links within your charts to build graphical, interactive applications.
This chapter is from the book

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:

  1. Create a new ColdFusion page, title it, and save it.

  2. Choose View > Code.

  3. After the opening <body> tag, type this code and press the spacebar:

    <cfchart

    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.

  4. 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."

Tips

  • 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:

  1. 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.1Figure 13.1 Place the <cfchartseries> tags within the opening and closing <cfchart> tags.

  2. 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.

  3. 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.2Figure 13.2 ColdFusion exports only the chart's border if you don't put any data into it.


TIP

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:

  1. In between the opening and closing <cfchartseries> tags, type this code:

    <cfchartdata item="2000" value="275">
  2. 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.3Figure 13.3 This chart has a single plot point.


  3. 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.4Figure 13.4 When you're entering data manually, accuracy is critical.


  4. 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.5Figure 13.5 ColdFusion automatically connects your points of data in a line chart.


Tips

  • <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:

  1. 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.

  2. Press F12 to preview the chart in your browser (Figure 13.6).

    Figure 13.6Figure 13.6 This chart has two series: one populated and one empty.


  3. 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.7Figure 13.7 shows the entire code listing for the chart.


  4. 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.8Figure 13.8 This chart shows two series: both are populated.


Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020