动态添加到 Highcharts [英] Dynamically adding to Highcharts

查看:29
本文介绍了动态添加到 Highcharts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 json 对象,我想将其添加到我的 highcharts 选项中.

I have a json object which I am trying to add to my highcharts options.

我能够通过 API 接收对象,并将其传递给我的 highcharts 函数,但我无法将我的数据添加到它周围的静态数据中.

I am able to recieve the object via API, and pass it to my highcharts function, But I am unable to add my data to the static data around it.

        var datas;
        datas = getData();
        //getdata();

        alert('2datass'+datas);
        console.log(datas);
        createChart(datas);


        function createChart(data){

            alert('createChart'+data); // works - outputs createChart[object Object][object Object][object Object][object Object][object Object]
            var dynamicData;


                    for(var i =0;i <= data.length-1;i++)
                    {
                        var item = data[i];

                        dynamicData = dynamicData + {
                            type: 'column',
                            name: item.name,
                            data: [item.difference]
                        };

                    }

            alert('dynamic' +dynamicData); // works, but says undefined before - outputs dynamic undefined[object Object][object Object][object Object][object Object][object Object]



            var series = [data /*This is where my data should sit*/,{
                    type: 'column',
                    name: 'Jane',
                    data: [300, 30]
                }, {
                    type: 'column',
                    name: 'John',
                    data: [-200, 50]
                }, {
                    type: 'column',
                    name: 'Joe',
                    data: [444, -25]
                }, {
                    type: 'column',
                    name: 'Jobe',
                    data: [444, -25]
                }, {
                    type: 'column',
                    name: 'Jooe',
                    data: [444, -25]
                },{
                    type: 'column',
                    name: 'Jane',
                    data: [300, 30]
                }
                , {
                    type: 'pie',
                    name: 'Total consumption',
                    data: [{
                            name: 'Jane',
                            y: 13
                            //color: '#4572A7' // Jane's color
                        }, {
                            name: 'John',
                            y: 23
                            //color: '#AA4643' // John's color
                        }, {
                            name: 'Joe',
                            y: 19
                            //color: '#89A54E' // Joe's color
                        }],
                    center: [30, 80],
                    size: 100,
                    showInLegend: false,
                    dataLabels: {
                        enabled: false
                    }
                }];




            var options = {
                chart: {
                    renderTo: 'container'
                },
                title: {
                    text: 'Account Managers Leaderboard'
                },
                xAxis: {
                    categories: ['Month on Month', 'Day on Day']
                },
                tooltip: {
                    formatter: function() {
                        var s;
                        if (this.point.name) { // the pie chart
                            s = ''+
                                this.point.name +': '+ this.y +' sales';
                        } else {
                            s = ''+
                                this.x  +': '+ this.y;
                        }
                        return s;
                    }
                },
                labels: {
                    items: [{
                            html: 'Total proportion of sales <br />this month',
                            style: {
                                left: '40px',
                                top: '-5px',
                                color: 'black'
                            }
                        }]
                },
                series: series
            };

            $(document).ready(function() {
                var chart;
                chart = new Highcharts.Chart(options);
            });

        }



       function getData(){
            //alert('ajax');

            var receivedData; // store your value here
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "API URL",
                async: false,
                success: function(data){
                    alert('data'+data); //works
                    receivedData = data;
                }
            }); 

            return receivedData;
        };

我的解决方案似乎是错误的,因为没有添加动态数据.任何帮助将不胜感激.

My solution seems to be the wrong one as the dynamic data is not being added. Any help would be greatly appreciated.

更新代码:

 var dynamicData = {
                    type: 'column',
                    name: 'FIRST',
                    data: [300, 30]
                };

            //Without the following loop, my chart will display the above entry "FIRST"
            //With the loop, the chart omits everything in dynamicData
                    for(var i =0;i <= data.length-1;i++)
                    {
                        var item = data[i];

                        dynamicData = dynamicData + {
                            type: 'column',
                            name: item.name,
                            data: [item.difference]
                        };

                    }

            alert('dynamic' +dynamicData); // works, but says undefined before - outputs dynamic undefined[object Object][object Object][object Object][object Object][object Object]


                //dynamicData should be 'FIRST' and then 5 other returned rows
            var series = [dynamicData, {
                    type: 'column',
                    name: 'John',
                    data: [-200, 50]
                }, {
                    type: 'column',
                    name: 'Joe',
                    data: [444, -25]
                }, {
                    type: 'column',
                    name: 'Jobe',
                    data: [444, -25]
                }, {
                    type: 'column',
                    name: 'Jooe',
                    data: [444, -25]
                },{
                    type: 'column',
                    name: 'Jane',
                    data: [300, 30]
                }
                , {
                    type: 'pie',
                    name: 'Total consumption',
                    data: [{
                            name: 'Jane',
                            y: 13
                            //color: '#4572A7' // Jane's color
                        }, {
                            name: 'John',
                            y: 23
                            //color: '#AA4643' // John's color
                        }, {
                            name: 'Joe',
                            y: 19
                            //color: '#89A54E' // Joe's color
                        }],
                    center: [30, 80],
                    size: 100,
                    showInLegend: false,
                    dataLabels: {
                        enabled: false
                    }
                }];

使用 .push 修改代码:

AMENDED CODE, USING .push:

                // series has been moved straight into the options variable
                var options = {
                chart: {
                    renderTo: 'container'
                },
                title: {
                    text: 'Account Managers Leaderboard'
                },
                xAxis: {
                    categories: ['Month on Month', 'Day on Day']
                },
                tooltip: {
                    formatter: function() {
                        var s;
                        if (this.point.name) { // the pie chart
                            s = ''+
                                this.point.name +': '+ this.y +' sales';
                        } else {
                            s = ''+
                                this.x  +': '+ this.y;
                        }
                        return s;
                    }
                },
                labels: {
                    items: [{
                            html: 'Total proportion of sales <br />this month',
                            style: {
                                left: '40px',
                                top: '-5px',
                                color: 'black'
                            }
                        }]
                },
                series: [{
                        type: 'pie',
                        name: 'Total consumption',
                        data: [{
                                name: 'Jane',
                                y: 13
                                //color: '#4572A7' // Jane's color
                            }, {
                                name: 'John',
                                y: 23
                                //color: '#AA4643' // John's color
                            }, {
                                name: 'Joe',
                                y: 19
                                //color: '#89A54E' // Joe's color
                            }],
                        center: [30, 80],
                        size: 100,
                        showInLegend: false,
                        dataLabels: {
                            enabled: false
                        }
                    }]
            };


            //I can now push data straight to seriese (although item.difference doesn't       
            // work .. perhaps because it's an array)

            for(var i =0;i <= data.length-1;i++)
            {
                var item = data[i];

                options.series.push({
                    "type": "column",
                    "name": item.name,
                    "data": item.data
                });
                alert('datavalue' +item.data);       
            }


               $(document).ready(function() {
                var chart;
                chart = new Highcharts.Chart(options);
            });

回答!

这似乎可以解决问题.感谢 Speransky Danil 和我一起奋斗 - 非常感谢.

This seems to do the trick. Thank you to Speransky Danil for struggling along with me - much appreciated.

        var datas;
        datas = getData();
        createChart(datas);


        function createChart(data){

            var options = {
                chart: {
                    renderTo: 'container'
                },
                title: {
                    text: 'Account Managers Leaderboard'
                },
                xAxis: {
                    categories: ['Month on Month', 'Day on Day']
                },
                tooltip: {
                    formatter: function() {
                        var s;
                        if (this.point.name) { // the pie chart
                            s = ''+
                                this.point.name +': '+ this.y +' sales';
                        } else {
                            s = ''+
                                this.x  +': '+ this.y;
                        }
                        return s;
                    }
                },
                labels: {
                    items: [{
                            html: 'Total proportion of sales <br />this month',
                            style: {
                                left: '40px',
                                top: '-5px',
                                color: 'black'
                            }
                        }]
                },
                series: [{
                        type: 'pie',
                        name: 'Total consumption',
                        data: [{
                                name: 'Jane',
                                y: 13
                                //color: '#4572A7' // Jane's color
                            }, {
                                name: 'John',
                                y: 23
                                //color: '#AA4643' // John's color
                            }, {
                                name: 'Joe',
                                y: 19
                                //color: '#89A54E' // Joe's color
                            }],
                        center: [30, 80],
                        size: 100,
                        showInLegend: false,
                        dataLabels: {
                            enabled: false
                        }
                    }]
            };




            for(var i =0;i <= data.length-1;i++)
            {
                var item = data[i];

                options.series.push({
                    "type": "column",
                    "name": item.name,
                    "data": [item.data]
                });

            }



















            $(document).ready(function() {
                var chart;
                chart = new Highcharts.Chart(options);
            });

        }



        function getData(){
            //alert('ajax');

            var receivedData; // store your value here
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "API URL",
                async: false,
                success: function(data){
                    alert('data'+data); //works
                    receivedData = data;
                }
            }); 

            return receivedData;
        };

推荐答案

问题出在包含以下内容的循环中:

The problem is in the loop which contains:

dynamicData = dynamicData + {
   type: 'column',
   name: item.name,
   data: [item.difference]
};

第一次 dynamicDataundefined.当我们对 undefinedObject 求和时,我们将它们都转换为 string 并连接起来,所以你得到 string 'undefined'[objectObject]',然后'undefined[object Object][object Object]'等等(转换为string的对象就是'[object Object]').

First time dynamicData is undefined. When we sum undefined and Object we convert both of them to string and concatinate it, so you get string 'undefined'[object Object]', then 'undefined[object Object][object Object]' and so on (converted to string object is '[object Object]').

这篇关于动态添加到 Highcharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆