Skip to content

Event Listeners

ArsenyMalkov edited this page Jan 4, 2019 · 1 revision

In AnyChart charting library, when a user interacts with a chart, there's an event object that represents the user's actions. Events are distributed to the chart elements (title, legend, axis, etc.), and to make those elements respond to events, you should attach event listeners to them.

Listeners are able to tell some extra information about the point.

@objc func foo(event: NSDictionary) {
    print(event["x"]!)
    print(event["value"]!)
}

pieChart.addTarget(target: self, action: #selector(foo), fields: ["x", "value"])

If you use custom data entry. Do not forget to indicate fields to return. The CustomDataEntry class has three fields which are "x", "value", "color".

@objc func foo(event: NSDictionary) {
    print(event["x"]!)
    print(event["value"]!)
    print(event["color"]!)
}


let data: Array<DataEntry> = [
    CustomDataEntry(x: "Apples", value: 6371664, "black"),
    CustomDataEntry(x: "Pears", value: 789622, "green"),
    CustomDataEntry(x: "Bananas", value: 7216301, "red")
]

pieChart.data(data: data)

pieChart.addTarget(target: self, action: #selector(foo), fields: ["x", "value", "color"])

class CustomDataEntry: ValueDataEntry {
        init(x: String, value: Double, color: String) {
            super.init(x: x, value: value)
            setValue(key: "color", value: color)
        }
}
Clone this wiki locally