0

我编写了一个代码,它以以下格式从mixpanel返回JSON 对象:

**{"legend_size": 1, "data":  { "series": ["2013-09-25 14:00:00", "2013-09-25 15:00:00", "2013-09-25 16:00:00"], "values": { "Demoevent": {"2013-09-25 20:00:00": 0,  "2013-09-25 12:00:00": 44, "2013-09-25 15:00:00": 1, "2013-09-25 01:00:00": 0 }}}}**

代码处理这个传递给HighCharts 方法的JSON 对象以动态生成图表。

这是原始格式的代码:

<html>
<head>

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js" />       
 <script type="text/javascript" src="jquery.md5.js"></script></script>
 <script type="text/javascript" src="mixpanelClient.js"></script></script>
 <script type="text/javascript" src="underscore-min.js"></script></script>
 <script type="text/javascript" src="md5.js"></script></script>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
 <script src="http://code.highcharts.com/highcharts.js"></script>

<script> 

$(document).ready(function() {

  var eventToDisplay = "acall finished";
   var apiKey = "BigSecret1";
   var apiSecret = "BigSEcret2";

   var mpEvent = {
   'event': [eventToDisplay, ],
   'type': 'general',
   'unit': 'hour',
  }

var mpClient = MixpanelClient(apiKey, apiSecret);
var requestUrl = mpClient.getRequestUrl(['events'], mpEvent);
console.log("The RequestUrl is:", requestUrl);

var JsonData=null;


var options = {

chart: {
        renderTo: 'container',
        type: 'column'
       },
        title:{
         text: 'Total Calls Finished'
       },
       xAxis: {
        type: 'datetime',
        title: {
        text: "Date"
       },
    },
       yAxis: {
        title:{
           text: 'Count of Calls',
             },
            },

            series:[]
        };



$.ajax({

type:"GET", 
url: requestUrl, 
//The request url returns a JSON object.I have checked it on the console.Attached the   screenshot too.

success: function(data) { 

     JsonData=data;

     **options.series[0].data.values = JsonData.data.values["acall finished"];
     var chart = new Highcharts.Chart(options);** 
     alert("Chart Created");

}, 

  error: function(jqXHR, textStatus, errorThrown) {
  alert(jqXHR.status);
},

dataType: "jsonp"

});
});

</script>
</head>
<body>
 <div id="container"></div>
</body>
</html>

现在这里是控制台的屏幕截图,它清楚地显示了我的控制台上的 JSON 对象。

控制台截图

问题是我得到一个空白图表。没有列,什么都没有。请帮助。我对 HighCharts 很陌生。

谢谢卡维什

4

1 回答 1

0

检查小提琴。在ajax的成功函数中,调用以下函数。我想这会对你有所帮助。

function buildChart(data){
    var seriesOptions = [],
        yAxisOptions = [],
        seriesCounter = 0,
        colors = Highcharts.getOptions().colors;

        dataLength = data[0].length;
        var today = data[0][dataLength-1][0];
        var namearray = new Array();
    for(var i = 0; i < data.length; i++){
        var dataarray = new Array();
        for(var j = 0; j < data[i].length; j++){
            dataarray[j] = new Array();
                dataarray[j][0] = data[i][j][0]; //TS
                dataarray[j][1] = data[i][j][2]; //END VALUE
                namearray[i] = data[i][j][data[i][j].length-2]; //DESCRIPTION
        }

        seriesOptions[i] = {
            name: namearray[i],
            data: dataarray
        };

        console.log(seriesOptions);
        // As we're loading the data asynchronously, we don't know what order it will arrive. So
        // we keep a counter and create the chart when all the data is loaded.
        seriesCounter++;

        if (seriesCounter == data.length) {
            createChart();
        }

    };

    // create the chart when all data is loaded
    function createChart() {
        var options = {
            chart: {
                renderTo: 'line'
            },

            title: {
                text: $(".item.selected td").first().text()
            },

            rangeSelector : {
                buttons: [{
                    type: 'month',
                    count: 1,
                    text: '1M'
                }, {
                    type: 'month',
                    count: 6,
                    text: '6M'
                }, {
                    type: 'year',
                    count: 1,
                    text: '1y'
                }, {
                    type: 'ytd',
                    text: 'YTD'
                }, {
                    type: 'all',
                    text: 'All'
                }],
                selected : 2 // year
            },
            xAxis: {
                type: 'datetime'
            },
            series: [{
                data: seriesOptions[0].data,
                type: "area"


            }]
        } 

        chart = new Highcharts.Chart(options);

        $.each(seriesOptions, function (itemNo, item) {
            chart.addSeries({                        
                name: item.name,
                data: item.data
            }, false);

        });
        chart.redraw();
};
};

var data = 
[[[1041375600000,28.95,28.95,28.95,28.95,0,"Quote"],[1041462000000,29,29.9,28.6,29.9,27300,"Quote"],[1041548400000,30.5,30,29.8,30.8,27700,"Quote"],[1041807600000,30.8,30.3,28.9,30.8,25600,"Quote"],[1041894000000,30.5,30.1,29.1,30.5,41700,"Quote"],[1041980400000,30.1,28.7,28.4,30.1,33400,"Quote"],[1042066800000,28.37,28.5,27.35,28.9,66900,"Quote"]],[[1041375600000,28.95,28.95,28.95,28.95,0,"series2"],[1041462000000,29,29.9,28.6,29.9,27300,"series2"],[1041548400000,30.5,30,29.8,30.8,27700,"series2"],[1041807600000,30.8,30.3,28.9,30.8,25600,"series2"],[1041894000000,30.5,30.1,29.1,30.5,41700,"series2"],[1041980400000,30.1,28.7,28.4,30.1,33400,"series2"],[1042066800000,28.37,28.5,27.35,28.9,66900,"series2"]]];

buildChart(data);
于 2013-09-25T10:52:42.660 回答