在一个图表中加载多个csv文件(正确的顺序) [英] load multiple csv files (correct order) in one chart

查看:178
本文介绍了在一个图表中加载多个csv文件(正确的顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载x csv文件并将数据呈现为折线图。加载1个csv文件并创建折线图工作已经很好。

I want to load x csv files and render the data to a line chart. Loading 1 csv file and create a line chart works already fine.

我的csv文件:

Date,PV,Energy
1355100420,0.000,0.851
1355100480,0.000,0.841
1355100540,0.000,1.000
1355100600,0.000,0.984
1355100660,0.000,1.006
1355100720,0.000,2.769
1355100780,0.000,2.791

我的问题:csv文件的数量很多,正确的顺序很重要,因为x轴是我的时间轴,我的csv的第一列有日期/时间。

My problems: the number of csv files is various and the correct order is important, because the x axis is my time axis and I have the dates/times in the 1st column of my csv.

阅读一个csv:

Read a single csv:

$.get(csv_file, function(data) {
    var series = [];
    // Split lines
    var lines = data.toString().split('\n');
    // For each line, split the record into seperate attributes
    $.each(lines, function(lineNo, line) {
        var items = line.split(',');
        // first line contains the series names
        if (lineNo === 0) {
            for (var i = 1; i < items.length; i++) {
                series.push({
                    name : items[i],
                    data : [],
                    dataGrouping : {
                        enabled : false
                    }
                });
            }
        } else {
            for (var i = 1; i < items.length; i++) {
                // get the serie
                var serie = series[i - 1];
                serie.data.push([parseFloat(items[0] * 1000), parseFloat(items[i])]);
            }
        }
    });
    chart = new Highcharts.StockChart({
        chart : {
            renderTo : container_id,
            type : 'line',
            reflow : true,
        },
        xAxis : {
            type : 'datetime'
        },
        series : series
    });
});

但是我怎样才能以正确的顺序读取多个csv文件?

But how can I read multiple csv files in the correct order?

非常感谢!

推荐答案

不同之处在于,您需要先解析这些值(如您在第一篇文章中所做的那样),然后将它们推入一个数组中。下一步就是对这个数组进行排序(简单的 data.sort(function(a,b){return a [0] - b [0];})应该足够了)并将排序后的数据放入 series.data

Difference is that, you need to first parse that values (as you are doing in first post), and push them into one array. Next step is simply to sort that array ( simple data.sort(function(a,b){ return a[0] - b[0]; }) should be enough ) and put sorted data into series.data.

这篇关于在一个图表中加载多个csv文件(正确的顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆