2015-01-13 25 views
-1

我正在使用Symfony2。我將一個字符串傳遞給顯示餅圖的視圖,並試圖將該字符串傳遞到數據部分。在Highcharts數據中使用字符串

我的字符串是:[['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> 
+0

哪個模板引擎您使用的爲您的項目?枝條?簡單的PHP? –

+0

我使用樹枝,我已經驗證了我傳遞的字符串的內容以及它在工作時通過的標識符。 – Hendrixexoperiences

+0

所以@Thomas答案是好方法! –

回答

1

問題是您的字符串已經是JS格式,並且您也將它作爲字符串傳遞給模板。刪除引號,它應該工作。此外,你可能想使用raw過濾器,所以枝不會逃脫你的字符。例如:

series: [{ 
    type: 'pie', 
    name: '', 
    data: {{ liste|raw }} 
}] 
+0

非常感謝{{liste | raw}} perefctly! – Hendrixexoperiences

+0

很高興幫助。考慮接受答案:) – lsouza

1

的代碼,因爲我相信你用樹枝作爲模板引擎,忘了PHP,包括括號。

<script> 
... 
    series: [{ type: 'pie',name: '',data: "{{liste}}" }] 
... 
</script> 
+0

當我這樣做,圖表根本不顯示! – Hendrixexoperiences

+0

你最終的HTML是什麼樣的? –

+0

我發佈我的代碼 – Hendrixexoperiences

1

問題與值的JSON解釋,所以使用json_encode過濾

series: [{ 
    type: 'pie', 
    name: '', 
    data: {{ liste|json_encode|raw }} 
}]