gpt4 book ai didi

mysql - 使用 Coldfusion 迭代查询以在 Charts.js 中使用

转载 作者:可可西里 更新时间:2023-11-01 08:09:46 27 4
gpt4 key购买 nike

我正在尝试弄清楚如何循环查询以与 Charts.JS 一起使用。

查询:

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

这给了我这样的数据:

    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

我需要循环查询的 Chart.JS 的主要摘录:

            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,
}

对于任何未使用 r_vehicle 的月份,我都需要插入一个“零”(因为它们不会出现在查询结果中)。

我已经尝试了很多方法,但它只比我的薪水高出几个档次!非常感谢任何帮助。

更新:当给定月份没有车辆使用时,这应该添加“0”:

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

所以,在这一点上,我想我只需要知道如何像这样循环输出数据:

LIMO(r_vehicle)2,1,0,0,1,0,3,3,3,0,2,0 (vehCount)

第二次更新:我有以下代码-

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

我一定是做错了输出查询,这就是我得到的:

豪华轿车 - 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

第三次更新将代码更改为:

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

请注意,我将它从 Limo 更改为 Sedan,因为 Sedan 缺少几个月。我现在得到:

轿车 - 1轿车 - 1轿车 - 3轿车 - 1轿车 - 2轿车 - 3轿车 - 5轿车 - 5轿车 - 1轿车 - 2

谢谢你 TRose!!!

所以,我看到剩下的两个问题:

1) 我只需要写一次'sedan'。2) 我需要用“零”填写缺失的月份

更新 4:
再次非常感谢 TRose!
所以,我现在有以下代码:

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

结果:

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

显然我遗漏了一些东西。我正在玩弄它,看看我能做什么。

更新 5

这似乎有效,但不确定它是什么!

 OUTPUT4:
<cfoutput>
#bymonthLimos.r_vehicle#
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>

结果:

OUTPUT4: Sedan 1, 1, 3, 0, 0, 1, 2, 3, 5, 5, 1, 2

雅虎!!!再次感谢!!

现在可以很好地与 Chart.js 脚本配合使用!

data: [
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
],

只是想指出一些事情:

1) 我必须将 CFOUTPUT 内容(不是 Charts.JS 标签中的内容)放入 CFSILENT 标签中,否则数据将以 HTML 格式显示。

2) DB 必须将月份格式设置为 MM 而不仅仅是 M。

最佳答案

这更像是一条超长评论。只是整理一下逻辑。数组和列表是我存在的祸根,并且总是难以有效地处理,但我至少可以给你一些提示。

就像...结构是你的 friend !

我刚刚查找了 Chart.js 的格式。您可以分别选择 Limos 和 Charter Buses,然后将任一查询作为同一图表上的单独数据集进行循环。我认为这很重要,这样我们就可以只关注月,这是你需要努力的,是吗?

来源:https://www.sitepoint.com/introduction-chart-js-2-0-six-examples/

例如,对于 Limos:

<cfquery name="bymonthLimos" datasource="#application.dsn#"> 
SELECT DISTINCT r_vehicle, r_month, COUNT(*)
FROM rsvs
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
WHERE r_vehicle LIKE 'Limo';
</cfquery>

它会给你这样的结果集:

    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

所以我所做的是我制作了一个包含一年中所有月份的列表,其格式与它们在您的数据库中的显示方式相同。

我遍历该列表以创建一个包含一年中所有月份和默认“计数”为零的结构。

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

接下来,获取您租用 Limo 的月份的实际数据。遍历该查询并更新您的结构。

<cfoutput query="bymonthLimos">
<cfset StructUpdate(MonthValues, "Month#r_month#", #count#)>
</cfoutput>

现在您按顺序获得了每个月的正确计数。我把它扔了,根据你的数据应该是这样的:

enter image description here

从这里您可以以任何您认为合适的方式循环它以生成该数字列表并将其插入 Chart.js。

您可以用同样的方式为您想要的任何其他类型的车辆创建另一个数据集。

编辑

我看到您更改了查询以包含 0。如果您仍然需要为 Chart.js 生成列表,请尝试 valueList()。例如:

<cfset MonthCount = ValueList(bymonth2.COUNT, ",")>

编辑 2


好的,替换这个:

<cfset MonthValues = structNew()>

有了这个。

<cfset MonthValues = createObject("java", "java.util.LinkedHashMap").init() />

它们做完全相同的事情,但第二个以特定顺序保存结构信息。

之后,您可以遍历它以获取值。

<cfoutput>
<cfloop collection="#MonthValues#" item="key">
#key#: #MonthValues[key]# <br />
</cfloop>
</cfoutput>

您只需要 #MonthValues[key]#(计数),它会产生 2, 1, 0, 0, 1, 0, 3, 3, 3, 0, 2, 0 但为了清晰起见,我将所有内容都包括在内。

一旦你遍历它,你就得到了你的列表。只需将其提供给您的图表插件并对其进行格式化。如果我正确地解释了他们的信息页面,您可以为数据制作任意数量的标签。

我冒昧地填写了第一个数据 (Limos) - 所以当您完成后,代码将看起来像这样。

     data: {
labels: ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"],
datasets: [{
label: 'Limos',
data: [
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
],
backgroundColor: "rgba(153,255,51,1)"
},
{
label: 'Charter Buses',
data: [
YOUR DELIMITED DATA FOR CHARTER BUSES HERE
],
backgroundColor: "rgba(255,153,0,1)"
}]
}
});

等等。

关于mysql - 使用 Coldfusion 迭代查询以在 Charts.js 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com