将多个csv文件中的时间戳数据添加到highchart

时间:2016-04-15 06:22:23

标签: javascript jquery csv highcharts

我正在尝试添加2个csv文件中的数据,这些文件的时间戳数据如下:9/30/2015 6:39:14 PM,20.709217。 我试图从文件中读取这些值并将字符串转换为highcharts接受的日期时间格式。

对于每个文件,我将这个转换后的数据添加到我推送到图表的数据数组中。

它出现以下错误:Invalid value for <path> attribute d="M 687.5 "

以下是我正在尝试的代码:jsfiddle

我的csv数据文件如下。

Data1.csv

9/30/2015 6:39:14 PM,20.709217
9/29/2015 6:38:16 PM,32.215775
9/28/2015 6:38:16 PM,32.215775

Data2.csv

9/30/2015 6:39:14 PM,24.709217
9/29/2015 6:38:16 PM,18.012775
9/28/2015 6:38:16 PM,33.245775

请帮助我。

1 个答案:

答案 0 :(得分:1)

$.get()是异步的,因此当您调用drawChart时,data1数组未完全设置:ajax调用未完成。

您需要在drawChart来电结束时移动$.get()来电。

以下是工作代码:

var options1 = {
  chart: {
    renderTo: 'container'
  },
  title: {
    text: ''
  },
  xAxis: {
    title: {
      text: 'Select on Parameters to change data in chart.'
    },
  },
  vAxis: {
    title: {
      text: ''
    },
  },
  tooltip: {
    enabled: true,
    shared: true
  },
  series: []
};

var drawChart = function(data, type, name, color) {
  var newSeriesData = {
    name: name,
    type: type,
    data: data,
    color: color
  };

  options1.series.push(newSeriesData);

  var chart = new Highcharts.Chart(options1);
};


var data1 = [
    []
  ],
  data2 = [
    []
  ];

$.get('data1.csv', function(csv) {
  var lines = csv.trim().split('\n');
  console.log("CSV: ", csv);

  $.each(lines, function(lineNo, line) {
    var items = line.split(',');
    console.log('Item1:', items[0])
    if ((line !== " ")) {

      var datetime = new Date(items[0]);
      console.log("Datetime variable: ", datetime);
      var value = parseFloat(items[1]);

      var year = datetime.getFullYear();
      var month = datetime.getUTCMonth();
      var day = datetime.getDay();
      var hour = datetime.getHours();
      var min = datetime.getMinutes();

      var thisDate = Date.UTC(year, month, day, hour, min);
      console.log("Date: ", thisDate);
      console.log("Value: ", value);
      //                        console.log("Date Generated: ",thisDate);
      data1.push([thisDate, value]);

    }
  });

  $.each(lines, function(lineNo, line) {
    var items = line.split(',');
    console.log('Item1:', items[0])
    if ((line !== " ")) {

      var datetime = new Date(items[0]);
      console.log("Datetime variable: ", datetime);
      var value = parseFloat(items[1]);

      var year = datetime.getFullYear();
      var month = datetime.getUTCMonth();
      var day = datetime.getDay();
      var hour = datetime.getHours();
      var min = datetime.getMinutes();

      var thisDate = Date.UTC(year, month, day, hour, min);
      console.log("Date: ", thisDate);
      console.log("Value: ", value);
      //                        console.log("Date Generated: ",thisDate);
      data1.push([thisDate, value]);

    }
  });
  console.log("Data1 Array: ", data1);
  drawChart(data1, 'line', 'DC Voltage (V)', 'red');
});

这是控制台中的输出:

CSV:  9/30/2015 6:39:14 PM,20.709217
9/29/2015 6:38:16 PM,32.215775
9/28/2015 6:38:16 PM,32.215775
Item1: 9/30/2015 6:39:14 PM
Datetime variable:  Wed Sep 30 2015 18:39:14 GMT+0200 (Paris, Madrid (heure d’été))
   "Datetime variable: "
   [date] Wed Sep 30 2015 18:39:14 GMT+0200 (Paris, Madrid (heure d’été))

Date:  1441305540000
Value:  20.709217
Item1: 9/29/2015 6:38:16 PM
Datetime variable:  Tue Sep 29 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))
   "Datetime variable: "
   [date] Tue Sep 29 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))

Date:  1441219080000
Value:  32.215775
Item1: 9/28/2015 6:38:16 PM
Datetime variable:  Mon Sep 28 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))
   "Datetime variable: "
   [date] Mon Sep 28 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))

Date:  1441132680000
Value:  32.215775
Item1: 9/30/2015 6:39:14 PM
Datetime variable:  Wed Sep 30 2015 18:39:14 GMT+0200 (Paris, Madrid (heure d’été))
   "Datetime variable: "
   [date] Wed Sep 30 2015 18:39:14 GMT+0200 (Paris, Madrid (heure d’été))

Date:  1441305540000
Value:  20.709217
Item1: 9/29/2015 6:38:16 PM
Datetime variable:  Tue Sep 29 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))
   "Datetime variable: "
   [date] Tue Sep 29 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))

Date:  1441219080000
Value:  32.215775
Item1: 9/28/2015 6:38:16 PM
Datetime variable:  Mon Sep 28 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))
   "Datetime variable: "
   [date] Mon Sep 28 2015 18:38:16 GMT+0200 (Paris, Madrid (heure d’été))

Date:  1441132680000
Value:  32.215775
Data1 Array:  ,1441305540000,20.709217,1441219080000,32.215775,1441132680000,32.215775,1441305540000,20.709217,1441219080000,32.215775,1441132680000,32.215775
   "Data1 Array: "
   [
      0: [ ],
      1: [ ],
      2: [ ],
      3: [ ],
      4: [ ],
      5: [ ],
      6: [ ],
      length: 7
   ]

Highcharts error #15: www.highcharts.com/errors/15
Highcharts error #15: www.highcharts.com/errors/15
Highcharts error #15: www.highcharts.com/errors/15
Highcharts error #15: www.highcharts.com/errors/15