开发者

How do I load a second CSV in jQuery?

开发者 https://www.devze.com 2023-04-04 05:59 出处:网络
I have the following code snippet from highcharts $(function() { $.get(\'trades.csv\', function(csv, state, xhr) {

I have the following code snippet from highcharts

    $(function() {
        $.get('trades.csv', function(csv, state, xhr) {

            // inconsistency
            if (typeof csv != 'string') {
                csv = xhr.responseText;
            }

            // parse the CSV trades
            var trades = [], header, comment = /^#/, x;

            $.each(csv.split('\n'), function(i, line){
                if (!comment.test(line)) {
                    if (!header) {
                        header = line;
                    }
                    else {
                        var point = line.split(';'),
                            date = point[0].split('-');
                            time = point[1].split(':');

                        if (point.length > 1) {
                            x = Date.UTC(date[2], date[1] - 1, date[0], time[2], time[1], time[0]);

                            trades.push([
                                x, // time
                                parseFloat(point[2]) // close
                            ]);
                        }
                    }
                }

            });

Suppose I wish to load a 开发者_开发知识库second spreads.csv into var spreads = ...

I have tried pasting $.get again with the appropriate modifications but the page no longer loads. What is the correct syntax for loading a second CSV?


function getCsv(file_name, array_holder){
    $.get(file_name, function(csv, state, xhr) {

        // inconsistency
        if (typeof csv != 'string') {
            csv = xhr.responseText;
        }

        // parse the CSV trades
        var header, comment = /^#/, x;

        $.each(csv.split('\n'), function(i, line){
            if (!comment.test(line)) {
                if (!header) {
                    header = line;
                }
                else {
                    var point = line.split(';'),
                        date = point[0].split('-'),
                        time = point[1].split(':');

                    if (point.length > 1) {
                        x = Date.UTC(date[2], date[1] - 1, date[0], time[2], time[1], time[0]);

                        array_holder.push([
                            x, // time
                            parseFloat(point[2]) // close
                        ]);
                    }
                }
            }

        });
    });
}

$(function() {
    var trades  = [],
        spreads = [];
    getCsv('trades.csv', trades);
    getCsv('spreads.csv', spreads);

});
0

精彩评论

暂无评论...
验证码 换一张
取 消