2017-03-26 166 views
1

我想解析一個字符串數組以供Highcharts使用。當這些值是靜態時,圖表呈現。當數組通過時,它不會呈現。我驗證了正在解析的字符串here解析字符串數組到圖表

的具體線路是

//This works 
chart.addSeries({name: this.series_name, color: this.series_color, data: [[0.129, 0.066], [0.029, 0.218], [-0.113, 0.231]]}); 

//This does not 
chart.addSeries({name: this.series_name, color: this.series_color, data: series_clean}); 

完全

$(document).ready(function() { 
    var chart = new Highcharts.Chart({ 
     chart: { 
      renderTo: 'container', 
      type: 'scatter', 
      zoomType: 'xy' 
     }, 
     series: [ 
      {data: []} 
     ] 
    }); 

    function reloadData() { 
     $.ajax({ 
      type: 'GET', 
      url: "...", 
      dataType: "JSON" 
     }).done(function(data) { 
      //Remove existing series from chart 
      while(chart.series.length > 0) 
       chart.series[0].remove(true); 

      var i = 0; 
      $.each(data.users, function(firstIndex, user) { 
       $.each(user, function() { 
        //Parse incoming data array to float type 
        var series_clean = new Array(); 

        var series_data_groups = this.series_data.split("|"); 

        for(var j = 0; j < series_data_groups.length; j++) { 
         var series_data_values = series_data_groups[j].split(","); 
         var x = parseFloat(series_data_values[0]).toFixed(3); 
         var y = parseFloat(series_data_values[1]).toFixed(3); 
         series_clean.push([x, y]); 
        } 

        //Push series to chart 
        chart.addSeries({name: this.series_name, color: this.series_color, data: series_clean}); 
        i++; 
       }); 
      }); 

      chart.redraw(); 
     }); 
    } 

    setInterval(function(){reloadData(); }, 2000); 
}); 

控制檯錯誤

Uncaught Error: Highcharts error #14: www.highcharts.com/errors/14 
    at Object.a.error (highcharts.js:10) 
    at n.setData (highcharts.js:284) 
    at n.init (highcharts.js:277) 
    at a.Chart.initSeries (highcharts.js:243) 
    at highcharts.js:317 
    at a.fireEvent (highcharts.js:29) 
    at a.Chart.addSeries (highcharts.js:317) 
    at Object.<anonymous> (chart_scatter_live2.html:109) 
    at Function.each (jquery-3.2.0.min.js:2) 
    at Array.<anonymous> (chart_scatter_live2.html:95) 
因爲 toFixed()

回答

2

xy都是字符串在返回一個字符串:

var x = parseFloat(series_data_values[0]).toFixed(3); 
var y = parseFloat(series_data_values[1]).toFixed(3); 

這會導致Highcharts Error #14 - String value sent to series.data, expected Number