我正在尝试通过Laravel中的jqPlot插件显示一个饼图.Jqplot脚本与laravel mix一起编译到主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 mix重新编译资产无法解决问题。代码不断落在startAngle未定义错误处。我对jqplot失去了理智。这是什么问题?
最佳答案
由于app.js中的已编译脚本属于doDraw函数(无法访问startAngle变量),因此修改doDraw函数即可完成任务。在doDraw函数中全局声明了startAngle变量,在刀片模板中也将startAngle声明为startAngle,以便可以为每个生成的jqPlot饼图全局访问该变量。当然,通过npm编译资产后,运行dev / production。