I am trying to figure out how to loop over a query for use with Charts.JS.
The query:
<cfquery name="bymonth" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*)
FROM rsvs
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month;
</cfquery>
That gives me data like this:
r_vehicle r_month COUNT(*)
1 Limo 01 2
2 Limo 02 1
3 Limo 05 1
4 Limo 07 3
5 Limo 08 3
6 Limo 09 3
7 Limo 11 2
8 Charter Bus 01 3
9 Charter Bus 02 2
10 Charter Bus 03 2
The main excerpt from Chart.JS where I need to loop over this query:
labels: ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"],
{
label: "r_vehicle",
backgroundColor: window.chartColors.red,
borderColor: window.chartColors.red,
data: [
(Number of times this vehicle was used per month -
Displayed as: 3,6,5,2,3,7,9,2,3,8,3,2)
],
fill: false,
}
For any months where r_vehicle was not used I need to insert a 'zero' (since they do not appear in the query results).
I have tried this a number of ways but its just a few notches over my paygrade! Much appreciate any help.
UPDATE: This should add the '0' when no vehicle was used in a given month:
<cfquery name="bymonth2" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, IFNULL(count(r_vehicle),0) AS vehCount
FROM rsvs
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month;
</cfquery>
So, at this point I think I just need to know how to loop over the output data like this:
LIMO (r_vehicle) 2,1,0,0,1,0,3,3,3,0,2,0 (vehCount)
2nd Update: I have the following code -
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, IFNULL(count(r_vehicle),0) AS vehCount
FROM rsvs
WHERE r_vehicle = 'Limo'
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
</cfquery>
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = structNew()>
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
<cfoutput query="bymonthLimos">
<cfset MonthCount = ValueList(bymonth2.vehCount, ",")>
#r_vehicle# - #MonthCount#<br>
</cfoutput>
I must be doing the output query wrong, this is what I'm getting:
Limo - 3,2,2,2,1,1,3,4,3,5,5,7,1,5,13,17,16,12,17,7,16,7,9,13,1,3,8,7,7,7,13,9,5,6,7,12,3,3,8,10,3,7,7,5,8,1,3,7,1,1,3,4,7,7,2,1,2,1,1,3,3,3,2,1,1,3,1,2,3,5,5,1,2,1,1,2,1,1,5,3,6,7,8,6,11,8,7,3
3rd Update Changed code to this:
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*) as count
FROM rsvs
WHERE r_vehicle = 'Sedan'
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
</cfquery>
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = structNew()>
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
<cfoutput query="bymonthLimos">
<cfset StructUpdate(MonthValues, "Month#r_month#", #count#)>
#r_vehicle# - #count#<br>
</cfoutput>
Please note, i changed it from Limo to Sedan because Sedan is missing some months. I am now getting:
Sedan - 1 Sedan - 1 Sedan - 3 Sedan - 1 Sedan - 2 Sedan - 3 Sedan - 5 Sedan - 5 Sedan - 1 Sedan - 2
Thank you TRose!!!
So, the two issues I see remaining:
1) I only need 'sedan' to be written once. 2) I need to fill in the missing months with a 'zero'
Update 4:
Thank you again so much TRose!
So, I now have the following code:
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*) as count
FROM rsvs
WHERE r_vehicle = 'Sedan'
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
</cfquery>
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = createObject("java", "java.util.LinkedHashMap").init() />
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
OUTPUT:
<cfoutput>
<cfloop collection="#MonthValues#" item="key">
#key#: #MonthValues[key]# <br />
</cfloop>
</cfoutput>
<br>
OUTPUT2:
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
<br>
OUTPUT3:
<cfoutput query="bymonthLimos">#r_vehicle# -
<cfset StructUpdate(MonthValues, "Month#r_month#", #count#)>
#count#<br>
</cfoutput>
RESULTS:
OUTPUT: Month01: 0 Month02: 0 Month03: 0 Month04: 0 Month05: 0 Month06: 0 Month07: 0 Month08: 0 Month09: 0 Month10: 0 Month11: 0 Month12: 0
OUTPUT2: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
OUTPUT3: Sedan - 1 Sedan - 1 Sedan - 3 Sedan - 1 Sedan - 2 Sedan - 3 Sedan - 5 Sedan - 5 Sedan - 1 Sedan - 2
Obviously I'm missing something. I'm playing around with it to see what i can do.
Update 5
This seems to be working, not sure what it was!
OUTPUT4:
<cfoutput>
#bymonthLimos.r_vehicle#
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
RESULTS:
OUTPUT4: Sedan 1, 1, 3, 0, 0, 1, 2, 3, 5, 5, 1, 2
Yahoo!!! Thank you again!!
Works great with the Chart.js script now!
data: [
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
],
Just wanted to point out some things:
1) The CFOUTPUT stuff (not the ones in the Charts.JS tag) I had to put in CFSILENT tags or the data shows up in HTML.
2) The DB must have the month formatted as MM and not just M.