Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datepicker with Highstocks / Highcharts

How can I get a working jQuery datepicker with highcharts rangeselector?

This fiddle is an old example (from a highcharts author) which has the problem.

http://jsfiddle.net/BWEm5/

Changing the end date will reset the start date to the beginning of the data.

$(function() {

    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
        // Create the chart
        window.chart = new Highcharts.StockChart({
            chart: {
                renderTo: 'container'
            },

            rangeSelector: {
                selected: 1,
                inputDateFormat: '%Y-%m-%d'
            },

            title: {
                text: 'AAPL Stock Price'
            },

            series: [{
                name: 'AAPL',
                data: data,
                tooltip: {
                    valueDecimals: 2
                }}]

        }, function(chart) {

            // apply the date pickers
            setTimeout(function() {
                $('input.highcharts-range-selector', $('#' + chart.options.chart.renderTo)).datepicker()
            }, 0)
        });
    });


    // Set the datepicker's date format
    $.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd',
        onSelect: function(dateText) {
            this.onchange();
            this.onblur();
        }
    });

});
like image 253
user1984528 Avatar asked Oct 04 '16 19:10

user1984528


1 Answers

You could set extremes once a date is selected, using your onSelect event and removing this.onchange().

$.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd',
        onSelect: function(dateText) {
            chart.xAxis[0].setExtremes($('input.highcharts-range-selector:eq(0)').datepicker("getDate").getTime(), $('input.highcharts-range-selector:eq(1)').datepicker("getDate").getTime()); 
            //this.onchange();
            this.onblur();
        }
    });

Example:

http://jsfiddle.net/BWEm5/542/

like image 172
Snoophogg Avatar answered Oct 05 '22 04:10

Snoophogg