Search code examples
javascriptamcharts

How to create a Click event in AmChart-StockChart


Let say there is a Stockchart using AmCharts as below:

https://codepen.io/Volabos/pen/yZwdqg

HTML code part looks as below:

<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<script src="https://www.amcharts.com/lib/3/amstock.js"></script>
<div id="chartdiv"></div>       

<button type="button">Click Me!</button>

At the lower left corner of the page, there is a Button. Now I want a functionality that, when user clicks on this Button, the Zoom=MAX will be selected (if not selected already.)

Is there any way to achieve that functionality?

Thanks for your pointer


Solution

  • You can achieve this with periodSelector.selectPeriodButton(). Sadly that function is not documented (docs). I forked your code pen and extended it with a function at the end (updated code pen).

    document.getElementById('clickme').onclick = event => {
      const period = chart.periodSelector.periods.find(b => b.period === 'MAX');
      // if you know the index:
      // const button = chart.periodSelector.periods[3];
      chart.periodSelector.selectPeriodButton(period);
    };
    

    EDIT:

    According to xorspark's answer that's probably not the best method. Maybe you want to use that instead.