1.5.4. Using Events

Let us consider the use of events. We will add handling of a graph item click to the screen created in Creating Chart. Open the screen controller in the IDE and inject the chart:

@Inject
private Chart chart;

Then add a listener at the bottom of the init(Map<String, Object> params) method:

chart.addGraphItemClickListener(event -> {
        CountryGrowth countryGrowth = (CountryGrowth) event.getItem();
        String message = String.format("GDP grow in %s (%s): %.1f%%",
        countryGrowth.getCountry(),
        event.getGraphId().substring(5),
        "graph2014".equals(event.getGraphId()) ? countryGrowth.getYear2014() : countryGrowth.getYear2015());
        showNotification(message, NotificationType.HUMANIZED_HTML);
});

To see the results, rebuild the project using Run → Restart application server and log in to the system. Open the screen and click one of the columns.

chart with event
Figure 3. Chart that handles graph item click event