gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:27:49 26 4
gpt4 key购买 nike

我有一个应用程序可以从谷歌电子表格中获取一天内最大的股票插入者的 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/

关于javascript - 格式化数据以解析为 highcharts 饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23869368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com