如何在构建柱形图,线条和饼图时引入php变量?
<?php $numero = 9; ?>
<script type="text/javascript">
var mes_I = "<?p h p echo $numero; ?>";
var chart;
$(function container_V () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container_V'
},
title: {
text: 'Média de Utilização de Animais'
},
xAxis: {
categories: ["mes_III", "mes_II", "mes_I"]
},
tooltip: {
formatter: function() {
var s;
if (this.point.name) { // the pie chart
s = ''+
this.point.name +': '+ this.y +'%';
} else {
s = ''+
this.x +': '+ this.y;
}
return s;
}
},
labels: {
items: [{
html: 'Utilização',
style: {
left: '40px',
top: '8px',
color: 'black'
}
}]
},
series: [{
type: 'column',
name: 'Mouse',
color: 'white',
data: [mes_I]
}, {
type: 'spline',
name: 'Average',
data: [3, 2.67, 3],
marker: {
lineWidth: 3,
lineColor: Highcharts.getOptions().colors[3],
fillColor: 'black'
}
}, {
type: 'pie',
name: 'Total consumption',
data: [{
name: 'LABCET',
y: 10,
color: '#006400'
}, {
name: 'LPEP',
y: 19,
color: '#00BFFF'
}],
center: [70, 75],
size: 100,
showInLegend: false,
dataLabels: {
enabled: false
}
}]
});
});
});
</script>
变量mes_I ok,包含值'9'。但是,当我输入给定的系列时,它不会出现。
答案 0 :(得分:1)
编码用于JS的PHP变量的最佳方法是json_encode
。这将处理数字,编码字符串并在它们周围加上引号,并处理嵌套对象。在这种情况下可能不是严格需要的,因为它是一个数字,但这是一个很好的习惯。
<script type="text/javascript">
var mes_I = <?php echo json_encode($numero); ?>;
在您的示例代码中
<? p h p
而不是
<?php