0

I was working on Am-charts and I want to trigger a function on clicking on each graph and display its value in alert. I am not able to find on which graph I am clicking. Please some one suggest me way to get its value on click. See JSFiddle here

angular.module('amChartsDirectiveExample',['amChartsDirective']).controller('amChartsController', function ($scope) {
    $scope.amChartOptions = {
        data: [{
            year: 2005,
            income: 23.5,
            expenses: 18.1
        }, {
            year: 2006,
            income: 26.2,
            expenses: 22.8
        }, {
            year: 2007,
            income: 30.1,
            expenses: 23.9
        }, {
            year: 2008,
            income: 29.5,
            expenses: 25.1
        }, {
            year: 2009,
            income: 24.6,
            expenses: 25
        }],
        type: "serial",
        theme: 'black',
        categoryField: "year",
        rotate: true,
        pathToImages: 'https://cdnjs.cloudflare.com/ajax/libs/amcharts/3.13.0/images/',
        legend: {
            enabled: true
        },
        chartScrollbar: {
            enabled: true,
        },
        categoryAxis: {
            gridPosition: "start",
            parseDates: false
        },
        valueAxes: [{
            position: "top",
            title: "Million USD"
        }],
        graphs: [{
            type: "column",
            title: "Income",
            valueField: "income",
            fillAlphas: 1,
        }]
    }

});

1 Answer 1

1

Add the following listener to your config:

listeners: [{
  event: "clickGraphItem",
  method: function(e) {

    console.log('Event object:', e);
    console.log('Clicked item:', e.item);
    console.log('Income:', e.item.dataContext.income);
  }
}]

Note that if you do something inside the event listener that interacts with Angular (scope modification for example), you need to call $scope.$apply() to trigger the digest loop manually.

Demo: http://jsfiddle.net/aggn062r/

Sign up to request clarification or add additional context in comments.

2 Comments

This is working well with bar charts. Why i can not apply for stacked area charts. I mean i was implementing this listener for below link but event is not triggered. jsfiddle.net/sqt2Lb8c/1
That is because an area is not a graph item. Bullets are graph items. Try clicking the bullets here: jsfiddle.net/na2L5dc1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.