1.4.4. Using Events

Let us consider the use of events. We will add handling of a slice shift out to the screen created in Section 1.4.3, “Creating Charts”. Open the screen XML descriptor in the IDE and inject the chart:

@Inject
    private Chart pieChart;

Then add a listener in the initPieChart() method:

pieChart.addSlicePullOutListener(new Chart.SlicePullOutListener() {
    @Override
    public void onPullOut(Chart.SlicePullOutEvent event) {
        BooksByGenre booksByGenre = (BooksByGenre) event.getItem();
        String msg = booksByGenre.getGenre() + ": " + booksByGenre.getCountOfBooks() + " book(s)";
        showNotification(msg, NotificationType.HUMANIZED);
    }
});

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 pie slices to shift it out.

Figure 1.4. Chart that handles slice pull out event

Chart that handles slice pull out event