Bazinga777
Bazinga777

Reputation: 5281

How to parse dates in highcharts from csv

I have a csv data in the following format

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

I am using highcharts to generate a graph from these values. I want to parse the timestamp into a format that Highcharts will process. I have looked around but couldn't find anything.

Below is my code snippet:

$.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);
                        }
                    });


                }
            });

Any help will be appreciated. I know that I have to convert the timestamp string to date but i'm not sure how.

Upvotes: 2

Views: 1033

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 10141

Put the code at the appropriate place in your code where you need to do this conversion. You have to convert the timestamp string to valid javascript date as below for example:

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);

Upvotes: 3

Related Questions