Now that you have an idea of what you can do with ColdFusion's charting features, it's time to get started with some basic examples. Most of the time, you will create charts with just two CFML tags, <cfchart> and <cfchartseries>. Introducing <cfchart> and <cfchartseries>To display a chart on a ColdFusion page, you use the <cfchart> tag. The <cfchart> tag is what controls the height, width, and formatting of your chart, but it doesn't actually display anything. Within the <cfchart> tag, you use the <cfchartseries> tag, which determines the type of chart (like bar or pie) and the actual data to show on the chart. NOTE Actually, you will occasionally want to place multiple <cfchartseries> tags within a <cfchart> tag. See the "Combining Multiple Chart Series" section, later in this chapter. Table 20.1 shows the most important attributes for the <cfchart> tag, and Table 20.2 shows the most important attributes for <cfchartseries>.
NOTE Because these tags have a large number of attributes (more than 40 in all), we are introducing only the most important attributes in this table. Others are introduced later in this chapter. NOTE In this chapter, you will see the term data point often. Data points are the actual pieces of data that are displayed on a chart. If you are creating a pie chart, the data points are the slices of the pie. In a bar chart, the data points are the bars. In a line or scatter chart, the data points are the individual points that have been plotted on the graph. NOTE You don't have to have a query object to create a chart. You can also create data points manually using the <cfchartdata> tag. See "Plotting Individual Points with <cfchartdata>," near the end of this chapter. Creating A ChartThe following example creates a simple bar chart using <cfchart> and <cfchartseries> to create a simple bar chart. <!--- Get information from the database ---> <cfquery datasource="myDsn" name="chartQuery"> SELECT movietitle, amountbudgeted FROM movies WHERE active=1 ORDER BY movietitle </cfquery> <h2>Chart: Film Budgets</h2> <!--- This defines the size and appearance of the chart ---> <cfchart chartwidth="750" chartheight="500" yaxistitle="Budget"> <!--- within the chart ---> <cfchartseries type="bar" query="chartquery" valuecolumn="amountbudgeted" itemcolumn="movietitle"> </cfchart> The <cfchart> tag is used to establish the size of the chart, and to specify that the word Budget appear along the y-axis (that is, at the bottom of the chart). Within the <cfchart> block, a <cfchartseries> tag is used to create a bar chart. ColdFusion is instructed to chart the information in the ChartQuery record set, plotting the data in the AmountBudgeted column and using the MovieTitle column to provide a label for each piece of information. The above code will create a Flash chart, as that is the default chart format generated by ColdFusion. Charts may also be generated as static images (in JPG and PNG formats), although static charts are not as functional or feature rich. Changing the Chart TypeThe previous example created a bar chart. To create a different chart (perhaps a pie chart, simply change the type to one of the values listed in Table 20.2. |