如何从csv解析高图中的日期

问题描述 投票:2回答:1

我有以下格式的csv数据

time_stamp,"Phase 3","Phase 2","Phase 3"
"2014-06-03 07:59:48",24210,22744,26003
"2014-06-04 07:59:49",112603,103417,121368
"2014-06-05 07:59:50",21302,20165,23317
"2014-06-06 07:59:50",21561,20951,23875
"2014-06-07 07:59:03",408,1151,767
"2014-06-08 07:59:04",384,1151,767

我正在使用highcharts从这些值生成图表。我想将timestamp解析为Highcharts将处理的格式。我环顾四周但找不到任何东西。

以下是我的代码片段:

$.get("<?php echo base_url(); ?>uploads/<?php echo $username; ?>"+TimeofDay, function(data) {
        // Split the lines
        var lines = data.split('\n');

        // Iterate over the lines and add categories or series
        $.each(lines, function(lineNo, line) {
            var items = line.split(',');

            // header line containes categories
            if (lineNo == 0) {
                $.each(items, function(itemNo, item) {
                    if (itemNo > 0) {
                     options.series.push({
                              name: item,
                              data: []
                              });
                     }
              });

                } else {
                    $.each(items, function(itemNo, item) {
                        if (itemNo === 0) { 
                            options.xAxis.categories.push(item);
                        } else if (parseFloat(item/1000)) { 
                            options.series[itemNo - 1].data.push(parseFloat(item/1000));
                        } else if (item == "null") { /* adding nulls */
                            options.series[itemNo - 1].data.push(null);
                        }
                    });


                }
            });

任何帮助将不胜感激。我知道我必须将时间戳字符串转换为日期,但我不知道如何。

javascript datetime charts highcharts timestamp
1个回答
3
投票

将代码放在代码中需要执行此转换的适当位置。您必须将时间戳字符串转换为有效的javascript日期,例如:

var my_date = "2014-06-03 07:59:48";
my_date = my_date.replace(/-/g, "/"); //here the new date string would be like: 2014/06/03 07:59:48
//alert(my_date);
var javascript_date = new Date(my_date);
© www.soinside.com 2019 - 2024. All rights reserved.