简体   繁体   English

AmCharts,捕获句点选择器的点击事件

[英]AmCharts, capturing click event for period selector

I'm using AmCharts 3 and I was wondering is there a way to capture an event for clicking the buttons of the period selector? 我正在使用AmCharts 3,我想知道有没有办法捕获一个事件来点击周期选择器的按钮?

The Docs state I can add a listener to the period selector of the type 'changed' but then the event runs almost anytime Docs状态我可以将一个监听器添加到“已更改”类型的句点选择器中,但事件几乎可以随时运行

There is a listener for 'zoomed', but it runs every time I zoom my chart regardless if I clicked the period selector 有一个'缩放'的监听器,但每当我缩放图表时它都会运行,无论我是否点击了周期选择器

Here's my attempt: 这是我的尝试:

chart = AmCharts.makeChart(scope.vm.chartId, chartSettings);
    AmCharts.addInitHandler(function () {
    // add events to item
    _.each(chart.panels, function (item) {
    //item.addListener("zoomed", onZoomed);
    item.addListener("changed", function(e) {
    console.debug("changed " + e.periodString);
   });
 });
});

You can use period selector's changed event for that. 您可以使用期间选择器的changed事件。

Ie: 即:

var chart = AmCharts.makeChart( "chartdiv", {
  "type": "stock",

  // ...

  "periodSelector": {
    "position": "left",
    "periods": [ {
      "period": "MM",
      "selected": true,
      "count": 1,
      "label": "1 month"
    }, {
      "period": "YYYY",
      "count": 1,
      "label": "1 year"
    }, {
      "period": "YTD",
      "label": "YTD"
    }, {
      "period": "MAX",
      "label": "MAX"
    } ],
    "listeners": [ {
      "event": "changed",
      "method": function( event ) {
        if ( event.predefinedPeriod !== undefined ) {
          console.log( event.predefinedPeriod, event.count );
        }
      }
    } ]
  },

  // ...
} );

Please note that this event will be executed when changing dates using date input fields as well, hence the check if event.predefinedPeriod is defined. 请注意,此事件也将在使用日期输入字段更改日期时执行,因此检查是否定义了event.predefinedPeriod

Please also keep in mind that default period is selected on chart init as well. 还请记住,在图表init上也选择了默认时间段。 So this event will be triggered when the chart is first loaded without any user interaction. 因此,首次加载图表时将触发此事件,而无需任何用户交互。

Here's a working demo . 这是一个有效的演示

Since you are initiating through a configuration object, I suggest you use the new 'listeners' property supported in the latest version of AmCharts to add an event handler to the periodSelector object on your chartSettings object. 由于您是通过配置对象启动的,因此我建议您使用最新版AmCharts中支持的新“侦听器”属性,将事件处理程序添加到chartSettings对象上的periodSelector对象。 It should work. 它应该工作。

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

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