在 angularjs 控制器中将 pandas json 转换为 highstock 数据格式

Convert pandas json for highstock data format in a angularjs controller

我正在为如何将系列图表从 json 转换为 highstock 数组格式而绞尽脑汁。

在我的代码中,我执行以下操作:

df2.reset_index().to_json(orient='records')

结果如下(例如):

[{"date":1456185600000,"adj_close":94.69},
{"date":1456099200000,"adj_close":96.88},
{"date":1455840000000,"adj_close":96.04},
{"date":1455753600000,"adj_close":96.26}]

然后我在我的 angularjs 控制器中得到 json 作为对象数组,我不知道如何将其转换为 highstock 格式,例如:

 [
 [1456185600000, 94.69],
 [1456099200000, 96.88],
 [1455840000000, 96.04],
 [1455753600000, 96.26],
 ]

我应该怎么做才能进行此转换?

d = [{"date":1456185600000,"adj_close":94.69},
{"date":1456099200000,"adj_close":96.88},
{"date":1455840000000,"adj_close":96.04},
{"date":1455753600000,"adj_close":96.26}]

然后像这样的列表理解

[[di['date'], di['adj_close']] for di in d]

给你想要的输出:

[[1456185600000, 94.69],
 [1456099200000, 96.88],
 [1455840000000, 96.04],
 [1455753600000, 96.26]]

您可以使用 Array.map() 方法重新格式化对象:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

var objectArray = [
  {"date":1456185600000,"adj_close":94.69},
  {"date":1456099200000,"adj_close":96.88},
  {"date":1455840000000,"adj_close":96.04},
  {"date":1455753600000,"adj_close":96.26}]

var highStockSeries = objectArray.map(function(object){
  return [object.date, object.adj_close]
})

console.log(highStockSeries)
 <div id="result">look in the JS console for your array</div>