質問

I'm trying to create an area chart using cfchart, but not sure how to go about it. I'm doing a query of all comments in a table and want to show time period (days or months on x-axis) and output number of comments as they are created. Here's the comment query:

<cfquery name="qGetComments" datasource="#session.datasource#">
SELECT PersonFirstName
       , PersonLastName
       , PersonZip
       , CommentCode
       , refTopicID
       , tbComment.CommentID
       , tbComment.CreateDateTime
FROM tbPerson 
JOIN tbComment ON tbPerson.PersonID = tbComment.PersonID
</cfquery>

From here, I'm not sure the values to put in the cfchart to output the area chart the way I want. Something like this?

<cfchart chartWidth="400" show3D="yes"> 
    <cfchartseries type="area" query="qGetComments" 
       valueColumn="CommentID" 
       itemColumn="CreateDateTime" /> 
</cfchart> 
役に立ちましたか?

解決

A simple way to group the records by date in Server 2008+ above is cast the datetime column to a date. Then use simply use the "CommentDate" column as the chart item and "NumOfComments" for the value:

SELECT CONVERT(DATE, tbComment.CreateDateTime) AS CommentDate
      , COUNT(*) NumOfComments
FROM  tbPerson INNER JOIN tbComment ON tbPerson.PersonID = tbComment.PersonID
GROUP BY CONVERT(DATE, tbComment.CreateDateTime); 


You can make the chart labels a little more user friendly by formatting the date within the SQL (see cast and convert). Just be sure to order the results by the date value, and not the formatted string. Otherwise, the chart labels may not appear in proper chronological order (it is a common mistake):

--- format as:  Mon dd, yyyy
SELECT CONVERT(VARCHAR, tbComment.CreateDateTime, 107) AS CommentDateString
      , COUNT(*) NumOfComments 
FROM  tbPerson INNER JOIN tbComment ON tbPerson.PersonID = tbComment.PersonID 
GROUP BY CONVERT(VARCHAR, tbComment.CreateDateTime, 107) 
ORDER BY CONVERT(DATE, CONVERT(VARCHAR, tbComment.CreateDateTime, 107), 107)

SQLFiddle

As an aside, if HTML charts are an option, you might look into Scott's suggestion. Generally CFChart is fine for simple cases, but once you start to customize the appearance (which invariably happens), you are usually better off using the underlying tools directly.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top