我正在尝试通过Laravel中的jqPlot插件显示一个饼图。Jqplot脚本通过laravel混合编译到主app.js文件中。我收到的错误:
jQuery.Deferred例外:无法读取属性'startAngle'的未定义TypeError:无法读取未定义的属性'startAngle'(在doDraw上)
doDraw函数(来自app.js):
function doDraw(rad) {
// Fix for IE and Chrome that can't seem to draw circles correctly.
// ang2 should always be <= 2 pi since that is the way the data is converted.
// 2Pi = 6.2831853, Pi = 3.1415927
if (ang2 > 6.282 + this.startAngle) { **it fails on this if statement**
ang2 = 6.282 + this.startAngle;
if (ang1 > ang2) {
ang1 = 6.281 + this.startAngle;
}
}
// Fix for IE, where it can't seem to handle 0 degree angles. Also avoids
// ugly line on unfilled pies.
if (ang1 >= ang2) {
return;
}
ctx.beginPath();
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.arc(0, 0, rad, ang1, ang2, false);
ctx.lineTo(0, 0);
ctx.closePath();
if (fill) {
ctx.fill();
} else {
ctx.stroke();
}
}
饼图代码:
<div id="pie_seminar{{ $seminar->id }}" class="piechart"></div>
<script>
var id, plot;
$(document).ready(function () {
id = '{{ $seminar->id }}';
var plot{{ $seminar->id }} = $.jqplot('pie_seminar' + id, [[['Attempted',{{ $statisticResult['incomplete'] }}], ['Completed',{{ $statisticResult['completed'] }}]]], {
gridPadding: {top: 1, right: 1, bottom: 1, left: 1},
grid: {
drawBorder: false,
drawGridlines: false,
background: '#ffffff',
shadow: false
},
seriesDefaults: {
renderer: $.jqplot.PieRenderer,
rendererOptions: {
sliceMargin: 3,
// rotate the starting position of the pie around to 12 o'clock.
startAngle: -90,
showDataLabels: true,
diameter: null,
padding: 8,
margin: 1
},
trendline: {show: false}
},
legend: {
show: false,
},
highlighter: {
tooltipContentEditor: function (str, seriesIndex, pointIndex) {
return str;
},
show: false,
useAxesFormatters: false,
tooltipFormatString: '%s'
},
seriesColors: ["#cccccc", "#aaccff", "#F5811F"]
});
});
</script>
我设法通过手动将脚本导入公共文件夹来显示Piechart插件(这不是解决方案,因为所有脚本都需要编译到一个文件中,所以这表明代码应该可以。
[使用laravel混合重新编译资产无法解决问题。代码不断落在startAngle未定义错误处。我对jqplot失去了理智。 这里是什么问题?
由于app.js中的已编译脚本属于doDraw函数(无法访问startAngle变量),因此修改doDraw函数可以完成此任务。 doDraw function中的startAngle变量已全局声明,并且在刀片模板中也为startAngle,以便为每个生成的jqPlot饼图全局访问变量。当然,通过npm run dev / production编译资产后。