Pablo Abdelhay
Pablo Abdelhay

Reputation: 1008

How can I dynamically update pointStart series attribute from highcharts.js?

I have a very simple highcharts js chart, which has dates on x-axis and values on y-axis. It works fine with this code:

chart = new Highcharts.Chart({
    chart: {
        renderTo: 'chart_container',
        type: 'line',
    },
    xAxis: {
        type: 'datetime',
            dateTimeLabelFormats: { 
                day: '%e / %b'              
            }
    },
    yAxis: {
            title: {
                text: null
            },              
            tickInterval: 1,
            tickmarkPlacement: 'on',
            min: 1,
            max: 5
    },
    series: [{
            name: 'serie_1',
            pointInterval: 24 * 3600 * 1000, // one day
            pointStart: Date.UTC(2011, 11, 22),            
            data: [
                2, 3, 5, 4, null, 4, 4, 4, 4, 3, 2, 2, 1, 2, 2, null               
            ]
   }]
});

I also have an event that changes the chart's data when some action is triggered:

function reloadChart(){
    $.get('/my/ajax/link/', { ajax_param: 10 },                                         
        function(data){
            // HOW TO GET THIS TO WORKS??? series.pointStart is readonly
            chart.series[0].pointStart = data.newPointStart;

            chart.series[0].setData(data.data, true);
        }
    );
}

My question is: How can I update my series[0].pointStart after chart has been initialized?

Upvotes: 1

Views: 6063

Answers (2)

Mark
Mark

Reputation: 108557

I would rethink this. It would be easier to create an x,y points from your y-value series instead of using the pointStart and pointInterval options.

startDate = Date.UTC(2010, 0, 1);
createData = function(beginDate){
    someData = [];
    for (var i = 0; i < 11; i++){
     someData.push([beginDate + (3600 * 1000 * 24 * i), Math.random() * 100]);
    }
    return someData;
}

....

series: [{
        data: createData(startDate)
    }]

Then in your function:

 nextDay = function(){
    startDate += 3600 * 1000 * 24;        
    chart.series[0].setData(createData(startDate), true);
 }

Working fiddle here.

Upvotes: 2

Mina Gabriel
Mina Gabriel

Reputation: 25100

i think this is what you are looking for

 chart.xAxis[0].setCategories();

see also the working jsFiddle

Upvotes: 1

Related Questions