Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a series dynamically with HighCharts Stock Charts

I have the following code: http://jsfiddle.net/maniator/vTjW8/

var createChartTemplate = function() {
    return {
        chart: new Highcharts.StockChart({

            chart: {
                renderTo: 'container'
            },
            series: []
        }),
        addSeries: function(name) {
            this.chart.addSeries({
                name: name,
                data: [],
                id: Math.floor(Math.random()*1000)
            });
        },
        addPoint: function(data, series) {
            var seriesIndex = this.seriesExists(series);
            if (!(seriesIndex === false)) {
                this.chart.series[seriesIndex].addPoint(data, false);
            }
            this.chart.redraw();
        },
        seriesExists: function(series) {
            var seriesIndex = false;
            $.each(this.chart.series, function(index, item) {
                if ($.trim(item.name) == $.trim(series)) {
                    seriesIndex = index;
                    return false;
                }
            });
            return seriesIndex;
        }
    }
}
$(function() {
    var data = usdeur.splice(0, 700);
    var chart = createChartTemplate();
    chart.addSeries("New Series");
    for (var i = 0; i < data.length; i++) {
        chart.addPoint(data[i], "New Series");
    }

});

It has the following error in the console:

Uncaught TypeError: Cannot read property 'options' of undefined

This code works fine if it is a normal highchart, but for some reason it does not work with a HighStock chart.

How can I make it so that it works with the chart type that I need?


Update:

I figures out a sort of way around getting the 1st series dynamically, but then when I try to add a second series it has an error of:

Uncaught TypeError: Cannot read property 'stacks' of undefined

Fiddle: http://jsfiddle.net/maniator/V5WAJ/

like image 776
Naftali Avatar asked Oct 06 '11 14:10

Naftali


People also ask

How do I create a dynamic series in Highcharts?

Then you should store that data in a variable (so you can refer to it) and use it in series option when creating a chart. If you want to add series dynamically then you could them using chart. addSeries() or recreate the chart with new data.

What is the difference between Highcharts and Highstock?

Re: Highstock vs Highcharts | Performance Exactly Highstock is much better for big data sets than Highcharts. It's because it was designed for this purpose. It has built-in data grouping facility, data is blazingly fast grouped into optional groups, which fastens the process a lot.

What is Highchart series?

A series is a set of data, for example a line graph or one set of columns. All data plotted on a chart comes from the series object.

What is plotOptions in Highcharts?

The plotOptions is a wrapper object for config objects for each series type. The config objects for each series can also be overridden for each series item as given in the series array. Configuration options for the series are given in three levels. Options for all series in a chart are given in the plotOptions.


3 Answers

You are creating the chart with an empty series, hence the error. When this line of you code runs it's initializing the Highchart immediately, before the series option has been set.

var chart = createChartTemplate();

I've had better experience with Highcharts when I build the series array first, then add it to the options of the constructor in the last step.

Specific to your example, the usdeur.js file already includes an initialized array of data. You simply need to pass it along in the options array. Your jsfiddle can be simplified to this.

$(function() {
    var chart = new Highcharts.StockChart({
        chart: {
            renderTo: 'container'
        },
        series: [{
            name: 'New Series',
            data: usdeur
        }]
    });
});
like image 191
Jonathan Beebe Avatar answered Oct 31 '22 20:10

Jonathan Beebe


I found one workaround. My use case was to plot values over time, so first value that I was adding to the chart was a pair of valid time stamp with a null for data:

series : [{
    name : 'Random data',
    data : (function() {
        var data = [], time = (new Date()).getTime();
        data.push([time, null]);                
        return data;
    })()
}]

Then whenever new values had to be plotted just added them simply as:

var value = rawData['My Data Set']
var plot  = [new Date().getTime(), value]
mychart.series[0].addPoint(plot, true, false)

This can be tested here, just replace the original definition of the series with the one from here.

like image 31
unexplored Avatar answered Oct 31 '22 19:10

unexplored


From the HighStock API Reference on addSeries:

In a StockChart with the navigator enabled, the base series can't be added dynamically.

That is to say, if you're using the Navigator, you have to start with at least one series. According to the same API Reference, the Navigator displays by default.

like image 2
kcvikander Avatar answered Oct 31 '22 18:10

kcvikander