2016-10-04 115 views
4

如何获得带有highcharts rangeselector的工作jQuery datepicker?与Highstocks/Highcharts jQuery日期选择器

这个小提琴是一个古老的例子(来自高层作家),它有问题。

http://jsfiddle.net/BWEm5/

更改结束日期将重置起始日期到数据的开头。

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

}); 
+0

这个问题似乎是特定于highcharts/highstock的最新版本(5.0.0)。如果我指向一个特定的旧版本(4.2.2),那么问题就会消失。 – user1984528

回答

3

使用onSelect事件并删除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(); 
     } 
    }); 

例子:

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

1

也许这将帮助

Change range in Highstock dynamically

我能够通过访问轴对象,以即时更新的图表显示的配置。

+0

感谢您指出了这一点,我真的更喜欢利用内置的rangeSelector,但如果我被强制转换为不能正常工作的highstock版本,这是一个很好的解决方法。 – user1984528