2

I successfully generated a chart from 3 CSV files, the problem is their x-axis value is not sorted.

Here's the demo.

I have tried storing all the data into an array variable then sort it, but I can't seem to figure it out.

Piece of code(Please view the demo):

$.get('https://dl.dropboxusercontent.com/u/32420108/WL_FORECAST.CSV', function (data) {
    $.get('https://dl.dropboxusercontent.com/u/32420108/WL_ACTUAL.CSV', function (data1) {
        $.get('https://dl.dropboxusercontent.com/u/32420108/RAINFALL_ACTUAL.CSV', function (data2) {
            var lines = data.split('\n');
            options.series.push({
                name: "Forecasted Water Level",
                data: [],
                tooltip: {
                    valueSuffix: "  mm/hr."
                },
                color: "#FF0000"
            });
            $.each(lines, function (lineNo, line) {
                var items = line.split(',');
                if (lineNo > 0) {
                    $.each(items, function (itemNo, item) {
                        if (itemNo === 0) {
                            options.xAxis.categories.push(item);
                        } else if (itemNo === 1) {
                            options.series[0].data.push(parseFloat(item));
                        }
                    });
                }
            });

... I have managed to convert my string date to epoch time for Highcharts datetime type x-axis but I got this error: Uncaught TypeError: Cannot read property '3' of null.

 $.each(lines, function (lineNo, line) {
        var items = line.split(',');
        if (lineNo > 0) {
            var parts = items[0].match(/(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2})/);
            d.push([Date.UTC(+parts[3], parts[1] - 1, +parts[2], +parts[4], +parts[5]), parseFloat(items[1])]);

        }
    });
Sachi Tekina
  • 1,800
  • 5
  • 28
  • 50

1 Answers1

2

I managed to solve this and all I did is convert the date and time(string) to epoch, something like this:

$.each(lines2, function (lineNo, line) {
    var items = line.split(',');
    if (lineNo > 0 && lineNo < lines2Len) {
        var parts = items[0].match(/(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2})/);
        f.push([Date.UTC(+parts[3], parts[1] - 1, +parts[2], +parts[4], +parts[5]), parseFloat(items[1])]);
    }
});

Highcharts will do the sorting. Thanks to @Sebastian Bochan for the idea. You can view the final output here: DEMO

Sachi Tekina
  • 1,800
  • 5
  • 28
  • 50