简体   繁体   English

将Poloniex API回调JSON转换为适合Highcharts.Stockchart的格式

[英]Converting Poloniex API Callback JSON into format suitable for Highcharts.Stockchart

I am trying to get JSON from Poloniex's public API method (specifically the returnChartData method) to display chart history of cryptocurrencies against one another into a Highchart Stockchart graph (looking like the demo one here. ). 我正在尝试从Poloniex的公共API方法(特别是returnChartData方法)获取JSON,以将加密货币的图表历史记录彼此相对显示为Highchart Stockchart图(看起来像此处演示 )。

This is part of my JavaScript code to use the Poloniex returnChartData callback, get the JSON from it and implement it into the 'data' segment of the chart. 这是我的JavaScript代码的一部分,该代码使用Poloniex returnChartData回调,从中获取JSON并将其实现到图表的“数据”部分中。 So far it is not working and I can't for the life of me figure out what I need to change. 到目前为止,这是行不通的,我无法终生弄清楚需要改变的地方。

var poloniexUrl = "https://poloniex.com/public?command=returnChartData&currencyPair=BTC_XMR&start=1405699200&end=9999999999&period=14400";
$.getJSON(poloniexUrl, function(data){
  results = data;
});
// Creates Chart
var chart = new Highcharts.StockChart({


chart: {
 renderTo: 'cryptoChart',
 backgroundColor: 'white'
},

title: {
  text: currentTitle
},

series: [{
  data: results,
  turboThreshold: 1000
}],

xAxis: {
 original: false
},

rangeSelector: {
 selected: 1
},

plotOptions: {
 line: {
  gapSize: 2
 }
}
});

Would love any help! 希望有帮助!

Refer to this live demo: http://jsfiddle.net/kkulig/0f4odg5q/ 请参阅此现场演示: http : //jsfiddle.net/kkulig/0f4odg5q/

If you use turboThreshold the points' options need to be given as an integer or an array ( Explanation: https://api.highcharts.com/highstock/plotOptions.series.turboThreshold ). 如果使用turboThreshold则点的选项需要以整数或数组形式给出( 说明: https : turboThreshold )。 In your case the format is JSON, so I disabled turboThreshold to prevent Higcharts error 12 ( https://www.highcharts.com/errors/12 ): 在您的情况下,格式为JSON,因此我禁用了turboThreshold以防止Higcharts错误12https://www.highcharts.com/errors/12 ):

  turboThreshold: 0

$.getJSON is asynchronous - the best way to make sure that data variable is initialized is using it inside callback function (second argument of getJSON ): $.getJSON是异步的-确保data变量已初始化的最佳方法是在回调函数( getJSON第二个参数)中使用它:

$.getJSON(poloniexUrl, function(data) {

  // Creates Chart
  var chart = new Highcharts.StockChart({
    chart: {
    (...)

The data that you fetch looks like candlestick series - I changed the type of the series: 您获取的数据看起来像烛台系列-我更改了系列的类型:

  type: 'candlestick'

Date will be properly understood by Highcharts if it's kept in the x property of JSON object (not date ): 如果将日期保留在JSON对象的x属性中(而不是date ),Highcharts将正确理解date

  data: data.map((p) => {
    p.x = p.date;
    return p
  }),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM