0

我的 highstocks 图表从 Yahoo 获取 JSON 数据,数据的结构类似于“www.blahAAPLblah.com”。

我想将 URL 中 AAPL 的值更改为其他公司股票代码,以便我可以获取数据并将其显示在我的图表上。如果我手动将字符串更改为 GOOG,那么它将正常工作。此外,如果我将var ticker = 'GOOG'URL 更改为"www.blah" + ticker + "blah.com"然后这也可以正常工作。

如果我有一个用户输入框,var ticker = document.getElementById('userInput').value;那么一切都会停止工作。

你有什么建议吗?

到目前为止,这是我的代码:http: //jsfiddle.net/SgvQu/

更新我尝试使用 JSONP 执行请求,但图表仍未加载。

    var closePrices = new Array();
    var dateArray = new Array();
    var timeStampArray = new Array();
    var timeClose = new Array();

function jsonCallback(data, ticker) {

            console.log( data );

            //Put all the closing prices into an array and convert to floats
            for(var i=0; i < data.query.results.quote.length; i++)
            {
                closePrices[i] = parseFloat( data.query.results.quote[i].Close );
            }

            //displays the values in the closePrices array
            console.log( closePrices );

            //Put all the dates into an array
            for(var i=0; i < data.query.results.quote.length; i++)
            {
                dateArray[i] = data.query.results.quote[i].date;
            }

            //Convert all the dates into JS Timestamps
            for(var i=0; i < dateArray.length; i++)
            {
                timeStampArray[i] = new Date( dateArray[i] ).getTime();
            }


            for(var i=0; i<data.query.results.quote.length; i++)
            {
                   timeClose.push( [timeStampArray[i], closePrices[i]] );
            }

            timeClose = timeClose.reverse();
            console.log ( timeClose );

            //displays the dateArray
            console.log( dateArray );
            console.log( timeStampArray );

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


            rangeSelector : {
                selected : 1
            },

            title : {
                text : ticker + ' Stock Price'
            },

            series : [{
                name : ticker,
                data: timeClose,
                tooltip: {
                    valueDecimals: 2
                }
            }]
        });
}

function createChart() {  

    var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22' + ticker +'%22%20and%20startDate%20%3D%20%222013-01-01%22%20and%20endDate%20%3D%20%222013-02-25%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=?';
    //Ajax call retrieves the data from Yahoo! Finance API
    $.ajax( url, {
        dataType: "jsonp",  
        success: function(data, status){
            console.log(status);
            jsonCallback(data, ticker);
        },
        error: function( jqXHR, status, error ) {
            console.log( 'Error: ' + error );
        }
    });
}


//Function to get ticker symbol from input box.
function getTicker() {
        var ticker = document.getElementById('userInput').value;
        createChart(ticker);
    }
4

1 回答 1

0

Thanks to Sebastian Bochan for pointing me in the right direction with JSONP. I now have the chart functioning correctly :)

于 2013-04-09T22:36:45.867 回答