Skip to content

Event Listeners

ArsenyMalkov edited this page Jan 4, 2019 · 2 revisions

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.

pieChart.setOnClickListener(new ListenersInterface.OnClickListener(new String[]{"x", "value"}) {
    @Override
    public void onClick(Event event) {
        event.getData().get("x");
        event.getData().get("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".

List<CustomDataEntry> data = new ArrayList<>();
data.add(new CustomDataEntry("Apples", 20, "black"));
data.add(new CustomDataEntry("Pears", 30, "green"));
data.add(new CustomDataEntry("Bananas", 40, "red"));

pieChart.setData(data);

pieChart.setOnClickListener(new ListenersInterface.OnClickListener(new String[]{"x", "value", "color"}) {
    @Override
    public void onClick(Event event) {
        event.getData().get("x");
        event.getData().get("value");
        event.getData().get("color");
    }
});

private class CustomDataEntry extends DataEntry {
    CustomDataEntry(Integer x, Integer value, String color) {
        setValue("x", x);
        setValue("value", value);
        setValue("color", color);
    }
}
Clone this wiki locally