Laravel 4 项目中的 Highcharts 未显示
Highcharts in Laravel 4 project are not displayed
我是 Laravel 的新手,所以每当我尝试向我的代码中添加 js 函数时,我仍然遇到问题。
我需要在我的 laravel 项目中添加一张图表。一开始,我只是复制了 highcharts.com 上可用图表之一的代码并将其粘贴到主视图中,但没有显示任何内容。
当我将相同的代码添加到普通 html 文件时,图表正常显示。
这是我的 javascript 代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
$(function () {
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
}())
}]
});
});
});
</script>
和html代码
<div id="container" style="width:100%; height:400px;"></div>
对如何显示图表有什么建议吗?
提前致谢。
我认为问题在于您创建图表的语法:$('#container').highcharts...
似乎 highcharts
函数未使用 laravel 定义。
您可以尝试另一种方法:
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
...
},
...
});
我是 Laravel 的新手,所以每当我尝试向我的代码中添加 js 函数时,我仍然遇到问题。 我需要在我的 laravel 项目中添加一张图表。一开始,我只是复制了 highcharts.com 上可用图表之一的代码并将其粘贴到主视图中,但没有显示任何内容。 当我将相同的代码添加到普通 html 文件时,图表正常显示。
这是我的 javascript 代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
$(function () {
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
}())
}]
});
});
});
</script>
和html代码
<div id="container" style="width:100%; height:400px;"></div>
对如何显示图表有什么建议吗? 提前致谢。
我认为问题在于您创建图表的语法:$('#container').highcharts...
似乎 highcharts
函数未使用 laravel 定义。
您可以尝试另一种方法:
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
...
},
...
});