将字符串数组解析为图表

时间:2017-03-26 01:09:49

标签: javascript highcharts

我试图解析要由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)

1 个答案:

答案 0 :(得分:2)

您的asyncasync是字符串,因为x返回一个字符串:

y

这会导致toFixed()