我尝试使用Google图表,SnappyPDF和Laravel在PDF上动态显示图表,最初基于this帖子。我已经在视图内发送了Laravel $arrayChart
变量进行测试,并获得成功:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function init() {
google.load("visualization", "1.1", {
packages: ["corechart"],
callback: 'drawCharts'
});
}
function drawCharts() {
var data = google.visualization.arrayToDataTable({!! $arrayChart !!});
var options = {
title: 'My Daily Activities',
};
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart'));
chart.draw(data, options);
}
</script>
</head>
<body onload="init()">
<div id="columnchart"></div>
</body>
看来,我在页面上有一个带有“ columnchart” ID的div,并在他身上显示了图表。但是,我真正要做的是将Chart的动态数据集传递给drawCharts()
函数以动态显示数据,诸如此类:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function init(data) {
google.load("visualization", "1.1", {
packages: ["corechart"],
callback: 'drawCharts(data)'
});
}
function drawCharts(obj) {
var data = google.visualization.arrayToDataTable(obj);
var options = {
title: 'My Daily Activities',
};
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart'));
chart.draw(data, options);
}
</script>
</head>
<body onload="init({!! $arrayChart !!})">
<div id="columnchart"></div>
</body>
不起作用。如何将参数传递给google.load回调?
答案 0 :(得分:2)
一种方法是传递对回调函数的引用,而不是字符串,并将参数绑定到该引用:
callback: drawCharts.bind(null, data)
或者,您可以创建一个新函数,该函数调用原始函数,并向其传递参数:
callback: () => drawCharts(data)