简体   繁体   English

无法使用json数据url在android中工作实时股票图

[英]cant work live stock graph in android using json data url

I'm a beginner with HIGHCHARTS. 我是HIGHCHARTS的初学者。

I want to start from this example: http://people.canonical.com/~bradf/media/highstock/examples/basic-line/index.htm 我想从这个例子开始: http : //people.canonical.com/~bradf/media/highstock/examples/basic-line/index.htm

 $(function() { $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) { // Create the chart window.chart = new Highcharts.StockChart({ chart: { renderTo: 'container' }, rangeSelector: { selected: 1 }, title: { text: 'AAPL Stock Price' }, series: [{ name: 'AAPL', data: data, tooltip: { valueDecimals: 2 } }] }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.highcharts.com/stock/highstock.js"></script> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> <div id="container" style="height: 500px; min-width: 500px"></div> 

I downloaded the corresponding JSON file : 我下载了相应的JSON文件:

http://chartapi.finance.yahoo.com/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/ http://chartapi.finance.yahoo.com/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/

And I want to run it locally (and after test it with my own JSON files). 我想在本地运行它(并用我自己的JSON文件对其进行测试后)。 But it doesn't works! 但这是行不通的!

I use the source code of the example, I've just modified the getJSON line. 我使用示例的源代码,我刚刚修改了getJSON行。

I have this:- 我有这个:-

  $.getJSON('./data/json/'+ name+'-c.json&callback=?', function(data) { ....... }

I think that the issue comes from the callback.Any ideas? 我认为问题出在回调上,有什么想法吗?

I tweaked the code from the example you linked to get data in the right format for highcharts. 我对您链接的示例中的代码进行了调整,以便以正确的格式获取图表的数据。 Not sure this is the best way to do it but you can use JSON.stringify to view the JSON data and extract from it the fields you want (I used "Timestamp" and "close"). 不确定这是否是最佳方法,但是您可以使用JSON.stringify来查看JSON数据并从中提取所需的字段(我使用“ Timestamp”和“ close”)。 More in comments-- hope this helps! 更多评论-希望这会有所帮助!

 $(function() { // add ?callback=? $.getJSON('http://chartapi.finance.yahoo.com/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/?callback=?', function(data) { // console.log(data.series); // console.log(JSON.stringify(data.series)); // extract the data you need myData = []; data.series.forEach(function(item) { myData.push([item.Timestamp, item.close]); }); console.log(JSON.stringify(myData)); // Create the chart window.chart = new Highcharts.StockChart({ chart: { renderTo: 'container' }, rangeSelector: { selected: 1 }, title: { text: 'AAPL Stock Price' }, series: [{ name: 'AAPL', // use extracted data data: myData, tooltip: { valueDecimals: 2 } }] }); }) // check for errors .done(function() { console.log('getJSON request succeeded!'); }) .fail(function() { console.log('getJSON request failed! '); }) .always(function() { console.log('getJSON request ended!'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.highcharts.com/stock/highstock.js"></script> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> <div id="container" style="height: 500px; min-width: 500px"></div> 

EDIT: and I would recommend to check for error messages as in https://stackoverflow.com/a/19075640/2314737 编辑:我建议检查错误消息,如https://stackoverflow.com/a/19075640/2314737

    // check for errors
    .done(function() {
      console.log('getJSON request succeeded!');
    })
    .fail(function() {
      console.log('getJSON request failed! ');
    })
    .always(function() {
      console.log('getJSON request ended!');
    });

请将您对应的JSON格式处理为与Highcharts使用的相似的this

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

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