User Guide Cancel

Create histograms in ColdFusion

 

A histogram is a graphical representation of a dataset's distribution. It's a bar chart that shows the frequency of data points within specified ranges (bins). Using the cfchart tag, you can create both vertical and horizontal histograms (type=hhist).

Create a histogram

In this example, we’ll create a histogram with some customization options. 

The first customization to use is the plot object that defines the rules and the rule-based background color.

plot={"rules":[
{
"rule":"%v<=80",
"background-color":"yellow"
}
]};
plot={"rules":[ { "rule":"%v<=80", "background-color":"yellow" } ]};
plot={"rules":[ 
        { 
            "rule":"%v<=80", 
            "background-color":"yellow" 
        } 
]}; 

The "rule":"%v<=80" is a condition used within the rules attribute to apply different sets of attributes to parts of your chart that meet the prerequisites listed in your rules.

The %v token represents the value of the plot. So, the rule %v<=80 checks if the plot value is less than or equal to 80.

If this condition is met, the styling attributes you provide in that object will be applied. For example, you could change the color of a bar in a bar chart to yellow if its value is less than or equal to 80.

After you’ve applied the plot customization options, you can proceed to customizing the crosshair styling options. A crosshair is a graphical overlay of horizontal and/or vertical lines that intersect at the cursor's position. A crosshair enhances data visualization and user interaction by providing precise reference points on the chart. 

crosshair={"line-color":"green","line-style":"solid","alpha"=0.5};
crosshair={"line-color":"green","line-style":"solid","alpha"=0.5};
crosshair={"line-color":"green","line-style":"solid","alpha"=0.5}; 

In this example, the crosshair has a line color of green, a solid line type, and a transparency of 0.5.

To create a vertical histogram, specify either hist or histogram as type.

Tying it all up, here’s the full code to generate a histogram.

code

<cfscript>
plot={
"rules":[
{ "rule":"%v<=80",
"background-color":"yellow"
}
]
}
crosshair={"line-color":"green","line-style":"solid","alpha"=0.5};
</cfscript>
<cfchart type="hist" format="html" chartWidth="600" chartHeight="400"
showborder="no" title="Website Traffic 2024 ('000s)"
tipbgcolor="##FF0045" showLegend="false" plot="#plot#"
crosshair="#crosshair#" >
<cfchartseries serieslabel="Visits" color="gray">
<cfchartdata item="January" value="120">
<cfchartdata item="February" value="80">
<cfchartdata item="March" value="30">
<cfchartdata item="April" value="67">
<cfchartdata item="May" value="50">
<cfchartdata item="June" value="100">
</cfchartseries>
</cfchart>
<cfscript> plot={ "rules":[ { "rule":"%v<=80", "background-color":"yellow" } ] } crosshair={"line-color":"green","line-style":"solid","alpha"=0.5}; </cfscript> <cfchart type="hist" format="html" chartWidth="600" chartHeight="400" showborder="no" title="Website Traffic 2024 ('000s)" tipbgcolor="##FF0045" showLegend="false" plot="#plot#" crosshair="#crosshair#" > <cfchartseries serieslabel="Visits" color="gray"> <cfchartdata item="January" value="120"> <cfchartdata item="February" value="80"> <cfchartdata item="March" value="30"> <cfchartdata item="April" value="67"> <cfchartdata item="May" value="50"> <cfchartdata item="June" value="100"> </cfchartseries> </cfchart>
<cfscript> 
    plot={
        "rules":[ 
            { "rule":"%v<=80", 
              "background-color":"yellow" 
            } 
        ]
    }
    crosshair={"line-color":"green","line-style":"solid","alpha"=0.5}; 
</cfscript> 
<cfchart type="hist" format="html" chartWidth="600" chartHeight="400" 
    showborder="no" title="Website Traffic 2024 ('000s)" 
    tipbgcolor="##FF0045" showLegend="false" plot="#plot#" 
    crosshair="#crosshair#" > 
    <cfchartseries serieslabel="Visits" color="gray"> 
        <cfchartdata item="January" value="120"> 
        <cfchartdata item="February" value="80"> 
        <cfchartdata item="March" value="30"> 
        <cfchartdata item="April" value="67"> 
        <cfchartdata item="May" value="50"> 
        <cfchartdata item="June" value="100"> 
    </cfchartseries> 
</cfchart>

Output

Horizontal histogram

Like a histogram (vertical), you can also create a horizontal histogram. Creating a horizontal histogram is almost the same as that of a vertical.

To create a horizontal histogram, specify either hhist or hhistogram as type.

For example,

code

<cfscript>
plot={"rules":[
{
"rule":"%v<=80",
"background-color":"yellow"
}
]};
crosshair={"line-color":"green","line-style":"solid","alpha"=0.5};
</cfscript>
<cfchart type="hhist" format="html" chartWidth="600" chartHeight="400"
showborder="no" title="Website Traffic 2024 ('000s)" tipbgcolor="##FF0045" showLegend="false" plot="#plot#" crosshair="#crosshair#" >
<cfchartseries serieslabel="Visits" color="gray">
<cfchartdata item="January" value="120">
<cfchartdata item="February" value="80">
<cfchartdata item="March" value="30">
<cfchartdata item="April" value="67">
<cfchartdata item="May" value="50">
<cfchartdata item="June" value="100">
</cfchartseries>
</cfchart>
<cfscript> plot={"rules":[ { "rule":"%v<=80", "background-color":"yellow" } ]}; crosshair={"line-color":"green","line-style":"solid","alpha"=0.5}; </cfscript> <cfchart type="hhist" format="html" chartWidth="600" chartHeight="400" showborder="no" title="Website Traffic 2024 ('000s)" tipbgcolor="##FF0045" showLegend="false" plot="#plot#" crosshair="#crosshair#" > <cfchartseries serieslabel="Visits" color="gray"> <cfchartdata item="January" value="120"> <cfchartdata item="February" value="80"> <cfchartdata item="March" value="30"> <cfchartdata item="April" value="67"> <cfchartdata item="May" value="50"> <cfchartdata item="June" value="100"> </cfchartseries> </cfchart>
<cfscript> 
    plot={"rules":[ 
        { 
            "rule":"%v<=80", 
            "background-color":"yellow" 
        } 
    ]}; 
    crosshair={"line-color":"green","line-style":"solid","alpha"=0.5}; 
</cfscript> 

<cfchart type="hhist" format="html" chartWidth="600" chartHeight="400"  

showborder="no" title="Website Traffic 2024 ('000s)" tipbgcolor="##FF0045" showLegend="false" plot="#plot#" crosshair="#crosshair#" > 
     <cfchartseries serieslabel="Visits" color="gray"> 
        <cfchartdata item="January" value="120"> 
        <cfchartdata item="February" value="80"> 
        <cfchartdata item="March" value="30"> 
        <cfchartdata item="April" value="67"> 
        <cfchartdata item="May" value="50"> 
        <cfchartdata item="June" value="100"> 
    </cfchartseries> 
</cfchart> 

Output

Get help faster and easier

New user?