ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mysql-使用Coldfusion遍历在Charts.js中使用的查询

2019-11-11 15:15:44  阅读:229  来源: 互联网

标签:chart-js coldfusion mysql


我试图弄清楚如何循环查询与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>

请注意,我将其从豪华轿车更改为轿车,因为轿车缺少了几个月.我现在得到:

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

谢谢TROSE !!!

因此,我认为剩下的两个问题是:

1)我只需要写“轿车”一次.
2)我需要在缺失的月份中填写“零”

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

<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)我必须放入CFSILENT标记中的CFOUTPUT东西(而不是Charts.JS标记中的东西),或者数据以HTML格式显示.

2)数据库必须将月份格式化为MM,而不仅仅是M.

解决方法:

这更多的是超长评论.只是整理逻辑.数组和列表是我生存的祸根,并且总是很难有效地处理,但是我至少可以给您一些提示.

就像…结构是您的朋友!

我只是查了一下Chart.js的格式.您可以分别选择Limos和Charter Bus,然后将任一查询作为单独的数据集循环到同一图上.我认为这很重要,因此我们可以只关注“月”,这是您需要进行的工作,是吗?

来源: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

所以我要做的是列出一年中所有月份的清单,其格式类似于它们在数据库中的显示方式.

我遍历该列表以创建一个Struct,其中包含一年中的所有月份,默认的“ Count”为零.

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

接下来,获取您在豪华轿车租用几个月中实际拥有的数据.遍历该查询并更新您的Struct.

<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,但是为了清楚起见,我将其包括在内.

循环遍历后,您便拥有了列表.只需将其输入到您的Chart插件并格式化即可.如果我正确地解释了他们的信息页,则可以为数据添加多个标签.

我已经自由地填写了第一个数据(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)"
        }]
      }
    });

等等.

标签:chart-js,coldfusion,mysql
来源: https://codeday.me/bug/20191111/2020682.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有