Loading a canvasjs chart with data from a database

I use coldfusion to load data into a pie chart, but instead of a pie chart, I get a list of 35 datapoints and the chart does not appear.

This is my code:

<script type="text/javascript">
$(document).ready(function () {
    $.getJSON("display.cfc?method=getData&lob=all", function (result) {
        alert(result);
        var chart2 = new CanvasJS.Chart("piechart", {
            theme: "theme2",
            legend: {
                fontSize: 12,
                horizontalAlign: "right",
                verticalAlign: "center"
            },
            data: [{
                type: "pie",
                showInLegend: true,
                dataPoints: result
            }]
        });
        chart2.render();
    });
});
</script>
<div id="piechart" style="display: inline-block; height: 200px; width: 400px;"></div>

my coldfusion function returns this:

{y: 142, legendtext: "In Progress"}, {y: 1083, legendtext: "New"},{y: 48, legendtext: "Error"} 

This is the function:

<cffunction name="getData" access="remote" returnformat="json">
<cfargument name="lob" type="string" required="yes" />
<cfset var theQuery = getTransitionStatusCounts(#arguments.lob#)>
<cfset var rObj = "">
<cfset var rObjArray = []>

<cfoutput query="theQuery">
    <cfset rObj = '{y: #count#, legendtext: "#status#"}'>
    <cfset arrayAppend(rObjArray, rObj)>
</cfoutput>

<cfreturn rObjArray>

</cffunction>

Has anyone else had this problem?

+4
source share
3 answers

2 things.
1. datapoints should be in an array
2. use coldfusion json as is. do not manually create json in cfc

$(document).ready(function () {
   $.getJSON("display.cfc?method=getData&lob=all", function (result) {       
      dp = [];
      for(var i = 0 ; i< result.DATA.length; i++){
          dp.push({y: result.DATA[i][0], label: result.DATA[i][1]})};       
      var chart2 = new CanvasJS.Chart("piechart", {
        theme: "theme2",
        data: [{
            type: "pie",
            dataPoints: dp }]});
        chart2.render();});});

This is what I have in my cfc

<cfcomponent access="remote">
<cffunction name="getData" access="remote" returnformat="json">
    <cfargument name="lob" type="string" required="yes" />
    <cfset var myQuery = QueryNew("y,label")/>
    <cfset QueryAddRow(myQuery,3) />

    <cfset QuerySetCell(myQuery, "y" , 142,1) />
    <cfset QuerySetCell(myQuery, "label" , "In Progress", 1) />

    <cfset QuerySetCell(myQuery, "y" , 1083,2) />
    <cfset QuerySetCell(myQuery, "label" , "New",2) />

    <cfset QuerySetCell(myQuery, "y" , 48,3) />
    <cfset QuerySetCell(myQuery, "label" , "Error",3) />

    <cfreturn myQuery/>    
  </cffunction>
</cfcomponent>
+2
source

, JSON returnformat="json". , CF , , ( ) , . , JSON, .. :

  [ "{y: 142, legendtext: \"In Progress\"}",.... ]

... :

  [ {"y":142, "legendtext":"In Progress"},....]

JSON. , CF :

    ...
    <cfoutput query="theQuery">
        <!--- use structure notation to preserve case --->
        <cfset rObj = {} />
        <cfset rObj["y"] = count />
        <cfset rObj["legendtext"] = status />
        <cfset arrayAppend(rObjArray, rObj)>
    </cfoutput>

    <cfreturn rObjArray>

, , JSON ?returnformat=json, .cfc jQuery.

+2

json - [] . JSON - .

[{"y": 142, "legendtext": "In Progress"}, {"y": 1083, "legendtext": "New"},{"y": 48, "legendtext": "Error"}]
0

All Articles