0

使用json_encode传递参数时出现问题,因为我需要将两个数组从 php 传递到 javascript。如果我不放置数组,我的代码运行正常

这是我的 javascript 图表代码:

function ChangeChartType(chart, series, newType) {
    newType = newType.toLowerCase();
    for (var i = 0; i < series.length; i++) {
        var srs = series[0];
        try {
            srs.chart.addSeries({
                type: newType,
                stack: srs.stack,
                yaxis: srs.yaxis,
                name: srs.name,
                color: srs.color,
                data: srs.options.data
            },
            false);
            series[0].remove();
        } catch (e) {
        }
    }
}

// Two charts definition
var chart1, chart2;

// Una vez que el DOM (documento) ha acabado de cargar
$(document).ready(function() {


    // First chart initialization
    chart1 = new Highcharts.Chart({
     chart: {
        renderTo: 'chart_1',
        type: 'column',
        height: 350,
     },
     title: {
        text: "<?php echo $titulo; ?>"
     },
     xAxis: {
        categories: ['Processing.js', 'Impact.js', 'Other', 'Ease.js', 'Box2D.js', 'WebGL', 'DOM', 'CSS', 'Canvas', 'Javascript']
     },
     yAxis: {
        title: {
           text: 'Meses'
        }
     },
    },function(chart){

       //var i = 0;

        for(i=0; i<3; i++) {

            chart.addSeries({
                data: [32, 43, 42],
                index: 0,
                zIndex:1
            });
        }


    });

    // Second chart initialization (pie chart)
    chart2 = new Highcharts.Chart({
        chart: {
            renderTo: 'chart_2',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
        },
        title: {
            text: "<?php echo $titulo; ?>"
        },
        tooltip: {
            formatter: function() {
                    var s;
                    if (this.point.name) { // the pie chart
                        s = ''+
                            this.point.name +': '+ this.y + ' totales';
                    } else {
                        s = ''+
                            this.x  +': '+ this.y + ' totales';
                    }
                    return s;
            }
            //pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                },
                showInLegend: true
            }
        },
         series: [{
            type: 'pie',
            name: 'Resultado',
            data: [<?php echo $resultado ?>]
        }]
    });

    // Switchers (of the Chart1 type) - onclick handler
    $('.switcher').click(function () {
        var newType = $(this).attr('name');
        ChangeChartType(chart1, chart1.series, newType);
    });
});

我用对话窗口打开两个图表

$(function() {

  $( "#dialog1" ).dialog({
    autoOpen: false,
    width: 950,
    height: 460,
    show: {
      effect: "blind",
      duration: 1000
    },
    hide: {
      effect: "blind",
      duration: 1000
    }
  });

  $( "#dialog2" ).dialog({
    autoOpen: false,
    width: 650,
    height: 760,
    show: {
      effect: "blind",
      duration: 1000
    },
    hide: {
      effect: "blind",
      duration: 1000
    }
  });

  $( "#opener1" ).click(function() {
    $( "#dialog1" ).dialog( "open" );
  });

  $( "#opener2" ).click(function() {
    $( "#dialog2" ).dialog( "open" );
  });

});

问题是当我尝试添加这个数组来添加月份和名称而不是['Processing.js', 'Impact.js', 'Other', 'Ease.js', 'Box2D.js', 'WebGL', 'DOM', 'CSS', 'Canvas', 'Javascript']默认名称时

// Two charts definition
var chart1, chart2;

// Una vez que el DOM (documento) ha acabado de cargar
$(document).ready(function() {

  var nombres = <?php echo json_encode($names); ?>;   //I CAN'T SEE MY FIRST PIE CHART
  var meses = <?php echo json_encode($valorX); ?>;      //WHEN I INSERT BOTH VARIABLES

    // First chart initialization
    chart1 = new Highcharts.Chart({
     chart: {
        renderTo: 'chart_1',
        type: 'column',
        height: 350,
     }, 

我的 php 部分

$valorX = array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
$names = array('Ana', 'Elena', 'Juan');

所以,我不知道我做错了什么。仅声明了这些数组,我的第二个图表(饼图)不会出现。我的代码正在建设中。对不起我的英语不好,编码和非常感谢你!!!!!!

4

0 回答 0