2015-01-15 157 views
0

出於某種原因,我無法設置以字面形式創建的Javascript對象的適當位置。 使用PHP編寫Javascript代碼。第一個圖表對象chartObject1正確顯示,但第二個圖表chartObject2不顯示標題,因爲我試圖在文字定義之外設置標題文本屬性。無法設置Javascript對象屬性

爲什麼不讓我使用chartObject2.title.text = "chart2";設置屬性?

<?php 
$chart_text = <<<EOD 
<script type="text/javascript"> 

var chartObject1 = Object; 
$(document).ready(function(){ 
    chartObject1 = new Highcharts.Chart({ 

     chart: { 
      renderTo: 'chart1', 
      type: 'bar' 
     }, 
     title: { 
      text: 'chart1' 
      }, 
     xAxis: { 
      categories: ['Apples', 'Bananas', 'Oranges'] 
     }, 
     yAxis: { 
      title: { 
       text: 'Fruit eaten' 
      } 
     }, 
     series: [{ 
      name: 'Jane', 
      data: [1, 0, 4]}, 
      { 
      name: 'John', 
      data: [5, 7, 3]}] 
     }); 
}); 
</script> 
EOD; 
print ($chart_text); 

$chart_text = <<<EOD 
<script type="text/javascript"> 

var chartObject2 = Object; 
$(document).ready(function(){ 
chartObject2 = new Highcharts.Chart({ 

     chart: { 
      renderTo: 'chart2', 
      type: 'bar' 
     }, 
     xAxis: { 
      categories: ['Spiders', 'Grasshoppers', 'Scorpions'] 
     }, 
     yAxis: { 
      title: { 
       text: 'Bugs eaten' 
      } 
     }, 
     series: [{ 
      name: 'Jane', 
      data: [1, 0, 4]}, 
     { 
      name: 'John', 
      data: [5, 7, 3]}] 
    }); 
    chartObject2.title.text = "chart2"; 
    }); 
</script> 
EOD; 
print ($chart_text); 

?> 

回答

0

因爲它是一個Highcharts對象,所以您需要使用它們的api來更改文本。 想要這樣:

chartObject2.setTitle({text: "chart2"});