AmCharts, capturing click event for period selector

别来无恙 提交于 2019-12-22 08:04:57

问题


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?

The Docs state I can add a listener to the period selector of the type 'changed' but then the event runs almost anytime

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

回答1:


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. It should work.




回答2:


You can use period selector's changed event for that.

I.e.:

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.

Please also keep in mind that default period is selected on chart init as well. So this event will be triggered when the chart is first loaded without any user interaction.

Here's a working demo.



来源:https://stackoverflow.com/questions/33065926/amcharts-capturing-click-event-for-period-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!