簡體   English   中英

格式化數據以解析為高圖表餅圖

[英]Format data to parse to a highcharts pie chart

我有一個應用程序,可以從Google電子表格中獲取一天中最大的股票變動者的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數組並將其關聯以繪制餅圖

您可以簡單地匹配數組,例如:

        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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM