2016-10-04 103 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