简体   繁体   English

从Highstock的RangeSelector获取当前选择的范围

[英]Get the currently selected range from RangeSelector in Highstock

I'm using both highcharts and highstock and have some charts that use the rangeSelector . 我正在使用highcharts和highstock,并且有一些使用rangeSelector图表。

Everything's working fine, but I'd like to GET the currently selected range (when the user clicks one of the rangeSelector buttons), so that I can store it in a cookie to know which range I want to display by default next time. 一切正常,但我想获取当前选定的范围(当用户点击其中一个rangeSelector按钮时),这样我就可以将它存储在一个cookie中,以便下次默认显示我想要显示的范围。

I've tried various things so far, like adding a chart.events.redraw test to try and catch the chart.rangeSelector.buttons object, but it doesn't seem to contain anything interesting in my case. 到目前为止,我已经尝试过各种各样的东西,比如添加一个chart.events.redraw测试来尝试捕获chart.rangeSelector.buttons对象,但它似乎并没有包含任何有趣的东西。

To me, the ideal would be an event callback on the rangeselector.buttons, with a simple getter function, like the equivalent of the chart.rangeSelector.buttons[x].setState() , named chart.rangeSelector.buttons[x].getState() ? 对我来说,理想情况是chart.rangeSelector.buttons[x].setState()上的事件回调,带有一个简单的getter函数,就像chart.rangeSelector.buttons[x].setState()的等效函数一样,名为chart.rangeSelector.buttons[x].getState()

I'm surprised this doesn't exist... I must be missing something. 我很惊讶这不存在......我必须遗漏一些东西。 Anybody can help on that ? 有人可以提供帮助吗?

The jsfiddle from jsfiddle.net/E6GHC/1 seems to answer the question partially (though I'm still surprised there is no event callback for this) 来自jsfiddle.net/E6GHC/1的jsfiddle似乎部分地回答了这个问题(虽然我仍然感到惊讶,因为没有事件回调)

The setExtremes event on the xaxis does the job: xaxis上的setExtremes事件完成了这项工作:

 xAxis: {
        events: {
            setExtremes: function(e) {
                console.log(this);
                if(typeof(e.rangeSelectorButton)!== 'undefined')
                {
                  alert('count: '+e.rangeSelectorButton.count + 'text: ' +e.rangeSelectorButton.text + ' type:' + e.rangeSelectorButton.type);
                }
            }
        }
    }

This http://jsfiddle.net/E6GHC/5/ should do what you want: 这个http://jsfiddle.net/E6GHC/5/应该做你想要的:

xAxis: {
  events: {
    setExtremes: function(e) {
      if(typeof(e.rangeSelectorButton)!== 'undefined') {
        var c = e.rangeSelectorButton.count;
        var t = e.rangeSelectorButton.type;
        var btn_index = null;
        if(c == 1 && t == "month"){
          btn_index = 0;
        } else if(c == 3 && t == "month"){
          btn_index = 1;
        } else if(c == 6 && t == "month"){
          btn_index = 2;
        } else if(t == "ytd"){
          btn_index = 3;
        } else if(c == 1 && t == "year"){
          btn_index = 4;
        } else if(t == "all"){
          btn_index = 5;
        }
        // Store btn_index in a cookie here and use it
        // to initialise rangeSelector -> selected next
        // time the chart is loaded
        alert(btn_index);
      }
    }
  }
}

chart event: redraw can get the display range. 图表事件:重绘可以获得显示范围。 Code is tested in highstock 3.10 代码在highstock 3.10中进行测试

events: {
    redraw: function(event) {
        console.log('==============> chart redraw <==============');
        console.log(
            Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', event.currentTarget.xAxis[0].min),
            Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', event.currentTarget.xAxis[0].max)
        );
        // log the min and max of the y axis
        console.log(event.currentTarget.yAxis[0].min, event.currentTarget.yAxis[0].max);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM