|
bar chart loop problem |
Posted by kevin on Dec-08-2010 21:57 |
|
Hi Peter,
I am trying to show a bar chart which displays the saving made by each bidder in a reverse auction. Currently it will only show the data for only 1 bidder on the chart. I have tested the cfloop and it is looping through the bidders and getting the saving amount OK but for some reason all the data is not being displayed on the bar chart. not quite sure where i have gone wrong?
here is the code i am using:
<CFSCRIPT>
// ChartDirector for ColdFusion API Access Point
cd = CreateObject("java", "ChartDirector.CFChart");
// A utility to allow us to create arrays with data in one line of code
function Array() {
var result = ArrayNew(1);
var i = 0;
for (i = 1; i LTE ArrayLen(arguments); i = i + 1)
result[i] = arguments[i];
return result;
}
// Create a XYChart object of size 600 x 360 pixels
c = cd.XYChart(600, 360);
// Add a title to the chart using 18pts Arial font
c.addTitle("Savings", "Arial", 18);
// Set the plotarea at (60, 40) and of size 500 x 280 pixels. Use a vertical gradient
// color from light blue (eeeeff) to deep blue (0000cc) as background. Set border and
// grid lines to white (ffffff).
c.setPlotArea(60, 40, 500, 280, c.linearGradientColor(60, 40, 60, 280, "0xeeeeff",
"0x0000cc"), -1, "0xffffff", "0xffffff");
</CFSCRIPT>
<!--- Invoke Method to find all the Bids in this Auction --->
<CFINVOKE COMPONENT="CFC.Main" METHOD="BidLatestDetails" RETURNVARIABLE="GetBids">
</CFINVOKE>
<!--- Invoke Method to find all the Bidders participating in this Auction --->
<CFINVOKE COMPONENT="CFC.Main" METHOD="BiddersInLot" RETURNVARIABLE="GetAllBidders">
</CFINVOKE>
<!--- Create a list of all the Bidders --->
<CFSET Request.AllBidders=ValueList(GetAllBidders.BidderID)>
<!--- Loop through the Bidders --->
<CFLOOP INDEX="Request.ListBidders" LIST="#Request.AllBidders#">
<!--- Query to find Data for the Graph --->
<CFQUERY NAME="GetChartData" DBTYPE="QUERY">
SELECT GetBids.BidderID, GetBids.Company, GetBids.Bid
FROM GetBids
WHERE GetBids.BidderID=<CFQUERYPARAM VALUE="#Request.ListBidders#" CFSQLTYPE="CF_SQL_VARCHAR" MAXLENGTH="50">
</CFQUERY>
<!--- Determine the savings --->
<CFSET Variables.Save_Amount=GetLots.Historical_Price-GetChartData.Bid>
<CFSCRIPT>
// The data for the bar chart
dataY0=Array(#Variables.Save_Amount#);
// The labels for the bar chart
labelsX0=Array(#GetChartData.Company#);
</CFSCRIPT>
</CFLOOP>
<CFSCRIPT>
// Add a multi-color bar chart layer using the supplied data. Use soft lighting
// effect with light direction from left.
c.addBarLayer3(dataY0).setBorderColor(cd.Transparent, cd.softLighting(cd.Left));
// Set x axis labels using the given labels
c.xAxis().setLabels(labelsX0);
// Draw the ticks between label positions (instead of at label positions)
c.xAxis().setTickOffset(0.5);
// Add a title to the y axis with 10pts Arial Bold font
c.yAxis().setTitle("Savings", "Arial Bold", 10);
// Set axis label style to 8pts Arial Bold
c.xAxis().setLabelStyle("Arial Bold", 8);
c.yAxis().setLabelStyle("Arial Bold", 8);
// Set axis line width to 2 pixels
c.xAxis().setWidth(2);
c.yAxis().setWidth(2);
// Output the chart
chart1URL = c.makeSession(GetPageContext(), "chart1");
// Include tool tip for the chart
imageMap1 = c.getHTMLImageMap("", "", "Savings'");
</CFSCRIPT> |
Re: bar chart loop problem |
Posted by Peter Kwan on Dec-09-2010 01:21 |
|
Hi kevin,
Your code only stores one data value in the array. So only one data value is plotted.
For example, the code "dataY0=Array(#Variables.Save_Amount#);" creates an array of one data value and stores it in the variable dataY0. It also simultaneously overwrites any previous value stored in dataY0. (In case you are not aware, assigning a value to a variable simultaneously overwrites the variable's previous value.)
So after the loop, your code should have overwritten all the data values except the last one. So only the last data value is plotted.
To solve the problem, please modify your code so that it does not overwrite previous values. You may use ArrayAppend to append the values to the array instead of overwriting one array with another array. For example, you may:
(a) Before you enter the loop, initialize your variables first:
dataY0 = ArrayNew(1);
labelsX0 = ArrayNew(1);
(b) Inside the loop, append the values to the arrays:
ArrayAppend(dataY0, Variables.Save_Amount);
ArrayAppend(labelsX0, GetChartData.Company);
Hope this can help.
Regards
Peter Kwan |
Re: bar chart loop problem |
Posted by kevin on Dec-09-2010 21:43 |
|
that's great peter, thanks! |
|