4

我知道这个标题似乎有点奇怪,我无法更好地解释它。

这是我需要的。我有这个 javascript 函数,我需要将数据作为字符串变量传递给它:

var chart;
function drawChart() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ['Firefox',   45.0],
                ['IE',       26.8],
                {
                    name: 'Chrome',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Safari',    8.5],
                ['Opera',     6.2],
                ['Others',   0.7]
            ]
        }]
    });
}

我需要将此数据作为参数传递给我的函数

[ ['Firefox', 45.0], ['IE', 26.8], { name: 'Chrome', y: 12.8, sliced: true, selected: true }, ['Safari', 8.5], ['Opera', 6.2],['其他',0.7]]

我该怎么做?

我希望它看起来像这样

var chart;
function drawChart(dataString) {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2010'
        },
        tooltip: {
            pointFormat: '{series.name}: <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>: '+ this.percentage +' %';
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: dataString
        }]
    });
}

我尝试了@moonwave99 的解决方案:

var browsers = [
 ['Firefox',   45.0],
 ['IE',       26.8],
 {
     name: 'Chrome',
     y: 12.8,
     sliced: true,
     selected: true
 },
 ['Safari',    8.5],
 ['Opera',     6.2],
 ['Others',   0.7]
];

...........
        series: [{
            type: 'pie',
            name: 'Browser share',
            data:  JSON.stringify(browsers) 
        }]
............

我的结果是这样的:

在此处输入图像描述

解决方案:http : //jsfiddle.net/USzVy/1/

var browsers = [
 ['Firefox',   45.0],
 ['IE',       26.8],
 {
     name: 'Chrome',
     y: 12.8,
     sliced: true,
     selected: true
 },
 ['Safari',    8.5],
 ['Opera',     6.2],
 ['Others',   0.7]
];


function drawChart() {
var data_str = JSON.stringify(browsers);

    var options ={
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: JSON.parse(data_str)
        }]
    }

  chart = new Highcharts.Chart(options);
}

谢谢

4

5 回答 5

5

首先将您的数据定义为数组,而不是字符串:

 var data = [["Firefox",45.0],["IE",26.8],{name: "Chrome",y: 12.8, sliced: true,selected: true},["Safari",8.5],["Opera",6.2],["Others",0.7]];

然后字符串化并作为参数传递给您的函数:

 var data_str = JSON.stringify(data);
 drawChart(data);

然后转换回JSON

 series: [{
        ...
        data: JSON.parse(data)
    }
于 2012-09-27T12:38:39.997 回答
3

使用JSON.stringify

var browsers = [
     ['Firefox',   45.0],
     ['IE',       26.8],
     {
         name: 'Chrome',
         y: 12.8,
         sliced: true,
         selected: true
     },
     ['Safari',    8.5],
     ['Opera',     6.2],
     ['Others',   0.7]
]

...

data : JSON.stringify(browsers)

...

编辑见 fiddle

EDIT2:我查看了这个库 API,你不需要一个字符串,而是一个选项数组!阅读文档,您将完成它。

于 2012-09-27T11:53:35.140 回答
1
function drawChart(data) {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: JSON.parse(data)
        }]
    });
}

var data = "[ ['Firefox', 45.0], ['IE', 26.8], { name: 'Chrome', y: 12.8, sliced: true, selected: true }, ['Safari', 8.5], ['Opera', 6.2], ['Others', 0.7] ]";
drawChart(data);
于 2012-09-27T11:52:47.403 回答
1
var chart;
function drawChart(dataString) {
    // options to create the chart
    var options = {
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: dataString // using the parameter
        }]
    };
    chart = new Highcharts.Chart(options);
}
// here you get the dataString some way
var dataString = [
    ['Firefox',   45.0],
    ['IE',       26.8],
    {
        name: 'Chrome',
        y: 12.8,
        sliced: true,
        selected: true
    },
    ['Safari',    8.5],
    ['Opera',     6.2],
    ['Others',   0.7]
];
drawChart(dataString);
于 2012-09-27T12:22:41.117 回答
1

太好了,解决方案,我正在尝试您的代码,但我的图表没有显示任何内容,所以,我正在跟踪我像参数一样传递的值,我的问题是我像字符串一样发送我的值,我解决这个只是转换我的数据使用 parseInt() 命令。

            function graficar(var1,var2,var3,var4){

                    var chart;
                    var datos=[];
                    datos[0]=parseInt(var1);
                    datos[1]=parseInt(var2);
                    datos[2]=parseInt(var3);
                    datos[3]=parseInt(var4);

                    var datos_str =JSON.stringify(datos);

                    alert(datos_str);

                    chart = new Highcharts.Chart({
                        chart: {
                                renderTo: 'grafico',
                                type: 'column'
                                },
                        title: {
                                text: 'Titulo de ventas'
                               },
                        xAxis: {
                                categories: ['30%', '40%', '50%', '60%']
                                },

                        tooltip: {
                            formatter: function() {
                                        return ''+
                                        this.series.name +': '+ this.y +'';
                                        }
                                },
                        credits: {
                            enabled: false
                               },
                        series: [{
                                name: 'Valores',

                                data: JSON.parse(datos_str)

………………

谢谢

于 2013-02-13T03:04:32.813 回答