1

I'm trying to parse a string array to be consumed by Highcharts. When the values are static, the chart renders. When the array is passed, it does not render. I validated the string being parsed here.

The specific line is

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

Full

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

Console error

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 Answer 1

2

Your x and y are strings since toFixed() returns a string at:

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

This causes the Highcharts Error #14 - String value sent to series.data, expected Number

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.