I am trying to create a simple line chart in ColdFusion 11 and would like to format the xAxis to show a date format like mm-dd-yy instead of the full date/timestamp that is showing by default.
My code is:
<cfchart format="html"
chartwidth="800"
chartheight="400"
xaxistitle="Date"
yaxistitle="Amount"
showlegend="yes"
fontsize="12"
font="Arial"
showMarkers="no"
xAxis=#[{"format"="Date","label":"Date"}]#>
<cfchartseries type="line"
query="getAmounts"
valueColumn="amount"
itemColumn="date">
</cfchart>
The xAxis attribute is giving this error:
You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members.
I have tried several different variations of the xAxis attribute with no luck - the documentation is unclear as to what format this should be in. Any help would be appreciated.
The format needs to be something like this...
<cfset myStruct = {"item"={"font-angle"=-90}}/>
And then just set xAxis = "#myStruct#" - you can do that all in the cfchart tag but it is just a little easier to read when the struct gets big. That is the correct format as that is working for us to set the angle of each item. But, I don't know what all of the options are for keys in xAxis. The docs just say "A struct of keys used to style x axis such as format, guide, item, and label."
With that being said, couldn't you just set the correct format to the field "date" in the "getAmounts" query? Then you wouldn't have to deal with it when you output it to the chart.
Related
Using CF10 Standard to create a spreadsheet from a query. No matter what I've tried so far, the formatting for a specific column stops at row 32 (1 header row, 31 data), even though the entire sheet is populated to 186 rows.
<cfscript>
dfStyle=StructNew();
dfStyle.fgcolor="pale_blue";
dfStyle.dataformat="mm/dd/yyyy";
theSheet = SpreadSheetNew('mysheet');
SpreadSheetAddRow(theSheet,'SID,FIRST,LAST,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,EMAIL,ADDED,PID');
SpreadSheetAddRows(theSheet,qry);
SpreadSheetFormatColumn(theSheet,dfStyle,10);
</cfscript>
I'm trying to get a consistent mm/dd/yyyy format on the 'ADDED' column. Instead, I'm getting that through row 31, and then getting dates like 41937.56594 on the of the rows.
In the formula bar I show "10/20/2014 12:25:23 PM" as the first value and "41932.552037037" as the 2nd value.
If I format the date in the query (i.e. date_format(sp_add,'%c/%e/%Y') AS spadd) I do get a nice date format all the way down the column, but the blue still stops at row 32.
Here is the cfoutput of the anonymized query - top row is formatted, bottom row loses formatting (background color - formatting dates in query). I also dropped the ADDRESS2 column from the query for now.
I'm not sure what the problem is, but try using the XML format option (the option to produce an xlsx file instead of the older style xls file). In your code add the "true" as a second argument to your spreadsheetnew() function call.
<cfscript>
dfStyle=StructNew();
dfStyle.fgcolor="pale_blue";
dfStyle.dataformat="mm/dd/yyyy";
theSheet = SpreadSheetNew('mysheet',TRUE);
SpreadSheetAddRow(theSheet,'SID,FIRST,LAST,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,EMAIL,ADDED,PID');
SpreadSheetAddRows(theSheet,qry);
SpreadSheetFormatColumn(theSheet,dfStyle,10);
</cfscript>
This will solve the problem - though we don't know why :)
I have a report stored as an .cfm file. I have been able to retrieve it fine with a cffile read. Now I want the option of retrieving only part of the report, say the first 50 lines. I decided to try a fileReadLine():
<cfset repname = url['rep']>
<cfset type = url['type']>
<cfset dataFile = fileOpen("/var/www/reports/moxrep/#repname#.cfm", "read" ) >
<cfset i = 0>
<cfoutput>
<cfloop
condition = "NOT FileIsEOF(dataFile) AND i LT 100">
<cfset i = i + 1>
<cfset inf = fileReadLine( dataFile ) >
#inf#
</cfloop>
</cfoutput>
<cfset fileClose( dataFile ) >
It did not retrieve things at all correctly. The formatting was messed up. All the dynamic data in the report was missing. The CSS links did not operate. And there were many extra blank lines.
Am I doing something wrong? Or is fileReadLine just not meant for retrieving a formatted report? And if not, is there any way to retrieve just part of the report with cffile?
Use cfhttp to get the report, then take that result and strip it down to what you need.
I am not sure you realize that the FileOpen() function is actually reading the raw CFML template and not actually executing the queries that populate your report. Using the CFHTTP tag is definitely a better approach, but be careful because your rendered page is likely to contain all your CSS that would be necessary for the report to render properly so use View Source on your report to see if you want just 50 lines.
The question burning in my mind is "why" do you want just 50 lines? are you previewing the report? it is only 1 page long? Are you embedding it into a dashboard? You may want to consider modifying the .cfm "report" so that the area you want to display elsewhere is wrapped with a specific tag (such as a Span or even something custom) and then when you fetch the report using CFHTTP, then you can parse the results with XMLParse() function (assuming it is properly formatted) and render the section of the report you actually want.
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.
I have a query which I'd like to output in a spreadsheet in Excel. I'd like some of the cell columns to be formatted in a certain way, which is thousands grouping and in Number format so that sums, additions etc can be done on that row without any further alteration.
I have read through the documentation but it has left me a bit confused on how to output to Excel in the first place.
I started out with a comment but it will be easier to read as an answer.
What have you tried? Have you read through the CFSpreadsheet documentation? Should be pretty straight forward. One of the parameters to the CFSpreadsheet tag is 'query'. Why not start with that and see how it formats the columns for you by default to see what needs tweaking.
Here is an example taken directly from the referenced documentation page:
<cfquery name="courses" datasource="cfdocexamples">
SELECT CORNUMBER, DEPT_ID, COURSE_ID, CORNAME
FROM COURSELIST
</cfquery>
<cfscript>
//Use an absolute path for the files. --->
theDir=GetDirectoryFromPath(GetCurrentTemplatePath());
theFile=theDir & "courses.xls";
//Create an empty ColdFusion spreadsheet object. --->
theSheet = SpreadsheetNew("CourseData");
//Populate the object with a query. --->
SpreadsheetAddRows(theSheet,courses);
</cfscript>
<!--- Write the sheet to a file --->
<cfspreadsheet action="write" filename="#theFile#" name="theSheet" sheetname="courses" overwrite=true>
See the documentation for SpreadsheetFormatColumn, SpreadsheetFormatColumns, SpreadsheetFormatRow and SpreadsheetFormatRows to read about formatting particular cells.
You just need to use the cfspreadsheet tag to create the file, and you can format the cells with the spreadsheetFormat* functions. You can find an example of how to do this at Ray Camden's site.
I'm using CFCHART to generate awesome charts on the fly.
It would be nice to dynamically change the title of the chart based on selection criteria.... and I've been attempting to do that by setting a string, graphTitle, that conditionally populates based on selections. All I want to do is simply start a new line for each criterion.
For instance: Suppose I have a chart that has a large number of selection criteria in it. I would want the chart title to look like this: (Break, of course, indicates the end of a line)
Fiscal Year 2006 to projected 2013 (Break)
Hires of African American Heritage candidates (Break)
Whom are Female
From New Mexico, California, Texas and Colorado (Break)
With an Age of 29+ (Break)
With a breakdown of Degree Achievement:
I tried using the <SPAN> and <BR /> tags in the title. With no luck.
Any other ideas?
If you mean you want to add line breaks into the chart title so it "wraps" you might try adding ascii coded line breaks like so...
<Csfet linefeed = chr(10) & chr(13)/>
Then your variable would be
<cfset mytitle = "fiscal year 2006 to projected 2012 #linefeed# Hires of..."/>
And so on - you get the idea. Note: you might need "just" chr(10) or you might need both. you'll need to experiment. I don't "know" that this will work. If it were me and I wanted something that complex to decorate the chart I think I would draw a plain "undecorated" chart with only x and y axis labels present and then set up my title outside the chart using HTML. I would have better control that way.
Anyway if that doesn't work try fiddling with the "style" attribute. You can provide an XML () var with all sorts of options - but it is not well documented I'm afraid. Ray has some stuff on his blog regarding this.