在柱状图中介绍php变量


Introducing php variables in column graphic

如何在构建柱状图、折线图和饼图时引入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"。但是,当我输入给定的序列时,它不会出现。

为JS中使用的PHP变量编码的最佳方法是json_encode它们。这将处理数字、对字符串进行编码并在其周围加引号,还将处理嵌套对象。在这种情况下可能并不严格需要,因为这是一个数字,但这是一种很好的习惯。

<script type="text/javascript">
var mes_I = <?php echo json_encode($numero); ?>;

在你的样本代码中,你有

<? p h p

代替

<?php