我正在使用Symfony2。我将字符串传递到显示饼图的视图中,并且试图将此字符串传递到数据部分。
我的字符串是: [['EURUSD',7],['GBPUSD',0],['USDEUR',1]]
我是从我的symfony控制器获得的,如下所示:
<?php {{liste}}; ?>
但是当我尝试将其放入这样的数据中时,图表无法正确加载
<script>
...
series: [{ type: 'pie',name: '',data: "<?php {{liste}};?>"
...
</script>
但是当我这样做
data: [['EURUSD',7],['GBPUSD',0],['USDEUR',1]]
有用!
编辑:这是我的观点的代码
<html>
<head>
<title>Chart</title>
<!-- Chargement des librairies: Jquery & highcharts -->
<script type='text/javascript' src="{{asset('bundles/user/js/jquery.min.js')}}"></script>
<script type="text/javascript" src="{{asset('bundles/user/js/highcharts.js')}}" ></script>
</head>
<body>
<p><div align="center">{{liste}}</div></p>
<p>
<!-- Chargement des variables, et paramètres de Highcharts -->
<script type="text/javascript">
$(function () {
var chart;
$(document).ready(function() {
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
return {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')]
]
};
});
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'myData'
},
tooltip: {
pointFormat: '<b>{point.percentage}%</b>',
percentageDecimals: 1
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 1) +' %';
}
}
}
},
series: [{
type: 'pie',
name: '',
data: "<?php {{liste}};?>"
}]
});
});
});</script>
<!-- Affichage du graphique -->
<div id="container" style="width:100%; height:400px;">
</div></p>
</body>
</html>
问题在于您的字符串已经是JS格式,并且您也将它作为字符串传递给模板。删除引号,它应该工作。另外,您可能希望使用raw
过滤器,以使树枝不会逸出字符。例子:
series: [{
type: 'pie',
name: '',
data: {{ liste|raw }}
}]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句