I am using jsreport.net to create a report. I am trying to use an array of data to bring it into a chart that uses JavaScript to function, but I am having trouble calling the data to the JavaScript. A table that i mad before works and it is as follows:
<div id="table" style="height: 300px; width: 50%;">
<table>
<thead>
<tr>
<th style="border: 1px solid">Date of restock</th>
<th style="border: 1px solid">Units sold</th>
<th style="border: 1px solid">Total profit</th>
</tr>
</thead>
<tbody>
{{#each sale}}
<tr>
<td style="border: 1px solid">{{date}}</td>
<td style="border: 1px solid">{{units}}</td>
<td style="border: 1px solid">R {{profit}}</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
My data looks like this:
{
"sale":[{
"date":"20 January 2018",
"units":"549",
"profit":"1514"
},{
"date":"16 February 2018",
"units":"483",
"profit":"1332"
},{
"date":"23 March 2018",
"units":"678",
"profit":"2596"
}]
}
And the chart looks like this:
<script>
window.onload = function () {
var chart= new CanvasJS.Chart("chartContainer2", {
animationEnabled: true,
theme: "light2",
title:{
text: "Progress of the number of units sold per restock"
},
axisY:{
includeZero: false
},
data: [{
type: "line",
dataPoints: [
{{#each sale}}
{ y: {{profit}}, label: {{date}} }
{{/each}}
]
}]
});
chart.render();
}
</script>
<div id="chartContainer2" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
I am new to this jsreport so please don't scold me if there is something I missed. Please help me figure this out.
I figured it out. All it needed is quotation marks around date.
"{{date}}"
And the profit needs to an integer not a string