Skip to main content
August 27, 2011
Question

CFCHART with dates

  • August 27, 2011
  • 1 reply
  • 683 views

I am having a problem with getting dates to line up in the appropriate order in a CFCHART from 3 different queries. In other words, August dates might come before July dates on the chart. Ideally I would like to have these 3 queries as one query because the records I am accessing are all in the same table. That can be battle for another day once I resolve this issue. Hints would be helpful on the query dilemma.

Just a little background:

  • MSSQL database
  • ColdFusion 9
  • Each record has 3 bit datatype fields and a timestamp datatype field.

What I am trying to do is loop over the selected records and count each bit that is 'true'. Here is what I have.

                                   
                    <!--- Get record bit 1 --->
                    <cfquery ="Datasource" name="record1q" cachedwithin="#CreateTimeSpan(0, 0, 10, 0)#">
                        SELECT Month(myDate) AS DateMonth, Day(myDate) AS recordDate,
                                Year(myDate) AS DateYear, COUNT(record1bit) AS record1bitCount
                        FROM myTable
                        WHERE record1bit = 'true'
                        GROUP BY Month(myDate), Day(myDate), Year(myDate), record1bit
                    </cfquery>
                   
                    <!--- Get record bit 2 --->
                    <cfquery ="Datasource" name="record2q">
                        SELECT Month(myDate) AS DateMonth, Day(myDate) AS recordDate,
                                Year(myDate) AS DateYear, COUNT(record2bit) AS record2bitCount
                        FROM myTable
                        WHERE record2bit = 'true'
                        GROUP BY Month(myDate), Day(myDate), Year(myDate), record2bit
                    </cfquery>
                   
                   
                    <!--- Get record bit 3 --->
                    <cfquery ="Datasource" name="record3q">
                        SELECT Month(myDate) AS DateMonth, Day(myDate) AS recordDate,
                                Year(myDate) AS DateYear, COUNT(record3bit) AS record2bitCount,
                                record3bit
                        FROM myTable
                        WHERE record3bit = 'true'
                        GROUP BY Month(myDate), Day(myDate), Year(myDate), record2bit, record3bit
                    </cfquery>

                    <!--- Reformat the generated numbers. --->
                    <cfloop index = "i" from = "1" to = "#record1q.RecordCount#">
                    <cfset record1q.record1bitCount = record1q.record1bitCount>
                    <cfset record1q.recordDate = record1q.DateMonth & "/" & record1q.recordDate & "/" & record1q.DateYear>
                    </cfloop>
                   
                    <!--- Reformat the generated numbers. --->
                    <cfloop index = "i" from = "1" to = "#record2q.RecordCount#">
                    <cfset record2q.record2bitCount = record2q.record2bitCount>
                    <cfset record2q.recordDate = record2q.DateMonth & "/" & record2q.recordDate & "/" & record2q.DateYear>
                    </cfloop>
                                       
                    <!--- Reformat the generated numbers. --->
                    <cfloop index = "i" from = "1" to = "#record3q.RecordCount#">
                    <cfset record3q.record2bitCount = record3q.record2bitCount>                   
                    <cfset record3q.recordDate = record3q.DateMonth & "/" & record3q.recordDate & "/" & record3q.DateYear>
                    </cfloop>


                            <cfchart showlegend="yes"
                                format="flash"
                                backgroundColor="white"
                                xAxisTitle="Date"
                                yAxisTitle="Activity"
                                font="Arial"
                                gridlines=6
                                showXGridlines="yes"
                                showYGridlines="yes"
                                showborder="yes"
                                show3d="yes"
                                chartwidth="500" title="Total Views and Shares"
                            >
                            <cfchartseries
                                type="bar"
                                query="record1q"
                                valueColumn="record1bitCount"
                                itemColumn="recordDate"
                                seriesColor="gray"
                                paintStyle="plain"
                                seriesLabel="record1bit">
                            </cfchartseries>   
                            <cfchartseries
                                type="bar"
                                query="record2q"
                                valueColumn="record2bitCount"
                                itemColumn="recordDate"
                                seriescolor="FF9900"
                                paintStyle="plain"
                                seriesLabel="record2bit">
                            </cfchartseries>
                            <cfchartseries
                                type="bar"
                                query="record3q"
                                valueColumn="record2bitCount"
                                itemColumn="recordDate"
                                 seriescolor="000099"
                                paintStyle="plain"
                                seriesLabel="record3qbit Viewed">
                            </cfchartseries>   
                        </cfchart>

I know it is a little sloppy but it is a good start. Any help will be appreciated.

    This topic has been closed for replies.

    1 reply

    Inspiring
    August 27, 2011

    You need a query with sorted dates.  That battle for another day, fight it now.

    August 28, 2011

    Dan, It does make sense. What I had previously was something similar to this:

                        <!--- Get record bits --->
                        <cfquery ="Datasource" name="recordq" cachedwithin="#CreateTimeSpan(0, 0, 10, 0)#">
                            SELECT Month(myDate) AS DateMonth, Day(myDate) AS recordDate,
                                    Year(myDate) AS DateYear, COUNT(record1bit) AS record1bitCount,

                                    COUNT(record2bit) AS record2bitCount, COUNT(record3bit) AS record3bitCount
                            FROM myTable
                            WHERE record1bit = 'true'
                            GROUP BY Month(myDate), Day(myDate), Year(myDate), record1bit, record2bit, record3bit
                        </cfquery>

    The problem with this was that all the records were grouped by date but was not totaled on one date line on the output. It also causes issues with the WHERE statement. I could have record1bit = 'true' but record2bit = 'false'. I have basic SQL skills so I generally gravitate to dealing with the output which is not always the best solution. A SQL solution would be ideal if you had one. Otherwise, grouping the output seems to have me at a roadblock with my ability. If there is no SQL solution and I could use this query, I would be thrilled since it would reduce the traffic from the database. Thanks for your help.