【问题标题】:Pass Data String as parameter to a Function for HighCharts将数据字符串作为参数传递给 HighCharts 的函数
【发布时间】:2012-09-27 11:49:06
【问题描述】:

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

这是我需要的。我有这个 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]
            ]
        }]
    });
}

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

[ ['火狐',45.0], ['IE',26.8], { 名称:“铬”, y: 12.8, 切片:真的, 选择:真 }, ['Safari',8.5], ['歌剧',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);
}

谢谢

【问题讨论】:

标签: javascript jquery json highcharts


【解决方案1】:

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

 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)
    }

【讨论】:

【解决方案2】:

使用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)

...

编辑see fiddle

EDIT2:我看过这个library API,你不需要需要一个字符串,但需要一个选项数组!阅读文档,您就可以完成。

【讨论】:

  • 我试过这个`系列:[{ type: 'pie', name: 'Browser share', data: { paramName : JSON.stringify(browsers) } }]`。代码运行没有错误,图表框架已加载但未绘制任何内容。我是不是做错了什么?
  • 查看我的最新版本,我添加了一个不必要的paramName
  • 请查看我的最新更新。数据出现严重问题
【解决方案3】:
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);

【讨论】:

  • 我已经尝试过了,但收到错误http://www.highcharts.com/errors/14。我想这是因为我将数据作为字符串传递并且它期望 int 值:(
【解决方案4】:
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);

【讨论】:

  • here you get the dataString some way 是什么意思?这就是我想要弄清楚的:)
  • 我试过了,框架加载正常,但图表上没有数据
  • 我一定尝试过,但不确定为什么它对我不起作用。非常感谢!
【解决方案5】:

非常好,解决方案,我正在尝试您的代码,但我的图表没有显示任何内容,所以,我正在跟踪我像参数一样传递的值,我的问题是我像字符串一样发送我的值,我解决了这个问题使用 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)

.. .. ...

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 2020-12-19
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    相关资源
    最近更新 更多