如何将JSON数据导入highstocks烛台类型图?

时间:2014-10-24 14:06:05

标签: json highstock yahoo-finance

我正在研究股票分析器项目,我已经获得了雅虎财经的所有股票报价,现在我想用highcharts.com的highstock-candlestick制作他们的动态图表...我没有信息关于JSON。我已经看到有一个代码将数据导入图表。我使用STOCK ANALYZER TUTORIALS OF" THENEWBOSTON"我需要知道如何将这些数据链接到highstock ..

这是样本数据......如何从雅虎财经网站添加真实数据? Plz帮助

$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?a=e&filename=aapl-ohlc.json&callback=?', function (data)

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Highstock Example</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <style type="text/css">
${demo.css}
    </style>
    <script type="text/javascript">
$(function () {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?a=e&filename=aapl-ohlc.json&callback=?', function (data) {

    // create the chart
    $('#container').highcharts('StockChart', {


        rangeSelector : {
            inputEnabled: $('#container').width() > 480,
            selected : 1
        },

        title : {
            text : 'AAPL Stock Price'
        },

        series : [{
            type : 'candlestick',
            name : 'AAPL Stock Price',
            data : data,
            dataGrouping : {
                units : [
                    [
                        'week', // unit name
                        [1] // allowed multiples
                    ], [
                        'month',
                        [1, 2, 3, 4, 6]
                    ]
                ]
            }
        }]
    });
});
});

    </script>
</head>
<body>

1 个答案:

答案 0 :(得分:1)

虽然时间不多,但我认为这可能对寻找类似解决方案的人有用。 JSFiddle example使用YQL显示来自Yahoo Fiance的GOOG股票价格

$(function () {

$.getJSON("https://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent("select Date, Open, High, Low, Close from csv where url='http://real-chart.finance.yahoo.com/table.csv?s=GOOG&d=2&e=4&f=2015&g=d&a=2&b=27&c=2014&ignore=.csv' and columns='Date,Open,High,Low,Close,Volume,Adj Close'") + "&format=json&callback=?", function (data) {

    //console.log(data.query.results);
    var data = data.query.results.row;
    data = data.splice(1, data.length);
    //console.log(data);

    var processedData = [];
    $(data).each(function(index, value) {
       //console.log(Date.parse(value.Date).getTime());
       //console.log(Date.parse(value.Date));
        processedData.push([Date.parse(value.Date), parseFloat(value.Open), parseFloat(value.High), parseFloat(value.Low), parseFloat(value.Close)]);
    });
    processedData = processedData.reverse();
    //console.log(processedData);

    // Create the chart
    $('#container').highcharts('StockChart', {


        rangeSelector : {
            selected : 1
        },

        title : {
            text : 'GOOG Stock Price'
        },

        series : [{
            type : 'candlestick',
            name : 'GOOG Stock Price',
            data : processedData,
            tooltip: {
                valueDecimals: 2
            }
        }]
    });
});

});