简体   繁体   English

jsReport不通过javascript读取数据

[英]jsReport not reading data through javascript

I am using jsreport.net to create a report.我正在使用 jsreport.net 创建报告。 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.我正在尝试使用一组数据将其放入使用 JavaScript 运行的图表中,但我无法将数据调用到 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.我是这个 jsreport 的新手,所以如果我遗漏了什么,请不要责骂我。 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 而且利润需要一个整数而不是一个字符串

convert data into js string using function from asset global helpers.js this results into something like var data = { "a": "foo" } which gets afterwards interpreted in chrome into object使用资产全局 helpers.js 中的函数将数据转换为 js 字符串,这会导致类似 var data = { "a": "foo" } 之后在 chrome 中将其解释为对象
var obj = {{{toJS date}}}; var obj = {{{toJS 日期}}};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM