格式化数据以解析为highcharts饼图

时间:2014-05-26 11:45:34

标签: javascript jquery ajax json highcharts

我有一个应用程序,可以在一天内从谷歌电子表格中获取最大的股票推动者的json数据。然后我使用这些数据生成一个饼图,显示不同的搬运工和使用ajax销售的股票数量来填充图表

这是我的代码:

<html>
<head>
    <title>NSE VISUALIZATIONS</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="highcharts.js"></script>
</head>
<body>  
  <div id="quo", style="width: 220px, height:320px">
  <script type="text/javascript">

    $(document).ready(function(){
    localhost = {}; //global namespace variable
    localhost.moverHTML = ""; //mover HTML built here
    localhost.moverValue = []; //array of percentage changes
    localhost.mover = []; //array of mover names
    localhost.chart1 = {yAxisMin : null, yAxisMax : null};//obj holds things belonging to chart1

    var url = "https://spreadsheets.google.com/feeds/list/1hb9O9MulweXASHaRzYbIOpRpkT7Mksx5xayfsUtv_8g/od6/public/basic?hl=en_US&alt=json";


$.ajax({
url: url,
cache: false,
dataType: 'jsonp', //will set cache to false by default
context: localhost,
success: function(data){

  for(i=0; i<data.feed.entry.length; i++){
    this.moverObj = data.feed.entry[i].title;
    this.moverHTML += '<br><strong>' + this.moverObj.$t + '</strong><br>';

    for(prop in this.moverObj){
      this.moverHTML += prop + ': ' + this.moverObj[prop] + '<br>';
    };
    var a = data.feed.entry[i].content.$t;
    var b = a.split(" ");
    var c = b[1].split(",");
    var f = c.join("");
    var g = parseFloat(f);
    this.moverValue.push(g);
    this.mover.push(data.feed.entry[i].title.$t);
  };
  this.chart1.yAxisMax = (function(array){
    //get the largest number in mover array

    var number_array = [];
    //John Resigs

    for(var i=0; i < array.length; i++){
      if(array[i] != null){
        number_array.push(array[i]);
      }
    };
    return Math.max.apply(Math, number_array);
  })(this.moverValue);

  this.chart1.xAxisMin = (function(array){
    var number_array = [];

    for(var i=0; i<array.length; i++){
      if(array[i] != null){
        number_array.push(array[i]);
      }
    };
    return Math.min.apply(Math, number_array);
  })(this.moverValue);

  this.chart1.data.series[0].data = this.moverValue;
  this.chart1.data.xAxis.categories = this.mover;


   $('#stockInfo').html(this.moverHTML);
  // $('#quo').css({height: '3500px'});
   chart = new Highcharts.Chart(this.chart1.data);
   console.log(data);
  }
  });


localhost.chart1.data = { //js single-threaded, this obj created before callback function completed
chart: {
  renderTo: 'quo',
  type: 'pie',
  options3d: {
        enabled: true,
        alpha: 45,
        beta: 0
      }
},
title: {
  text: "Daily Movers"
},
subtitle: {
  text: "Source: nse.co.ke"
},
xAxis: {
  categories: null, //will be assigned array value during ajax callback
  title: {
    text: null
  }
},
yAxis: {
  min: localhost.chart1.yAxisMin,
  max: localhost.chart1.yAxisMax,

  title: {
    text: 'Largest Movers',
    align: 'high'
  },
  labels: {
    overflow: 'justify'
  }
},
tooltip: {
  formatter: function(){
    return ''+
        this.series.name + ': '+this.y+'%';
  }
},
plotOptions: {
  pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            depth: 35,
            dataLabels: {
                enabled: true,
                format: '{point.name}'
            }
        }
},
legend: {
  layout: 'vertical',
  align: 'right',
  verticalAlign: 'top',
  x: -1,
  y: 1,
  floating: true,
  borderWidth: 1,
  backgroundColor: '#FFFFFF',
  shadow: true
},
credits: {
  enabled: false
},
series: [{
  name: 'Daily Change',
  data: null
}]
};
 });
     </script>
    </div>
  </body>
</html>

highcharts识别的数据格式如下:

 [["SCOM",2392492],["EQTY",2089121]]
然而,我把它作为我的2个数组中的数据:第一个是一组最大的移动器名称,第二个是它们的值列表,这些值按照移动器名称在第一个阵列中排列的相应顺序排列。即。

 ["SCOM","EQTY"]

 [2392492, 2089121]

如何格式化我的代码以接受这个2数组并将它们联系起来绘制饼图

1 个答案:

答案 0 :(得分:2)

您可以简单地匹配数组,例如:

        var points = [],
            mv = this.mover;
        $.each(this.moverValue, function(i ,e) {
            points[i] = [mv[i],e];
        })
        this.chart1.data.series[0].data = points;

演示:http://jsfiddle.net/9Yjc4/3/