Search in sources :

Example 6 with ChartClickListener

use of com.vaadin.addon.charts.ChartClickListener in project charts by vaadin.

the class DateAxisAndClickEvent method getChart.

@Override
protected Component getChart() {
    Chart chart = new Chart();
    chart.setHeight("450px");
    chart.setWidth("100%");
    Configuration configuration = chart.getConfiguration();
    configuration.setTitle("Date axis and click events");
    configuration.getChart().setType(ChartType.SPLINE);
    configuration.getxAxis().setType(AxisType.DATETIME);
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MILLISECOND, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.HOUR_OF_DAY, 12);
    c.set(2013, 2, 11);
    DataSeries dataSeries = new DataSeries();
    Number[] values = new Number[] { 71.5, 29.9, 106.4 };
    Random r = new Random(0);
    for (Number number : values) {
        c.add(Calendar.MINUTE, r.nextInt(5));
        DataSeriesItem item = new DataSeriesItem(c.getTime(), number);
        dataSeries.add(item);
    }
    configuration.addSeries(dataSeries);
    chart.drawChart(configuration);
    chart.addChartClickListener(new ChartClickListener() {

        @Override
        public void onClick(ChartClickEvent event) {
            /*
                 * The axis value is in client side library's raw format: unix
                 * timestamp, "shifted" to UTC time zone
                 */
            ;
            double timeStampShiftedToUc = event.getxAxisValue();
            /*
                 * When working with Date objects, developers probably want to
                 * convert it to Date object at their local time zone.
                 */
            Notification.show("Clicked @ " + Util.toServerInstant(timeStampShiftedToUc));
        }
    });
    chart.addPointClickListener(new PointClickListener() {

        @Override
        public void onClick(PointClickEvent event) {
            /*
                 * Same with point clicks...
                 */
            ;
            double timeStampShiftedToUc = event.getX();
            Notification.show("Clicked Point with Date value " + Util.toServerInstant(timeStampShiftedToUc));
        }
    });
    return chart;
}
Also used : Configuration(com.vaadin.addon.charts.model.Configuration) Calendar(java.util.Calendar) PointClickEvent(com.vaadin.addon.charts.PointClickEvent) ChartClickEvent(com.vaadin.addon.charts.ChartClickEvent) ChartClickListener(com.vaadin.addon.charts.ChartClickListener) Random(java.util.Random) PointClickListener(com.vaadin.addon.charts.PointClickListener) DataSeries(com.vaadin.addon.charts.model.DataSeries) Chart(com.vaadin.addon.charts.Chart) DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem)

Aggregations

ChartClickEvent (com.vaadin.addon.charts.ChartClickEvent)6 ChartClickListener (com.vaadin.addon.charts.ChartClickListener)6 Chart (com.vaadin.addon.charts.Chart)5 PointClickEvent (com.vaadin.addon.charts.PointClickEvent)5 PointClickListener (com.vaadin.addon.charts.PointClickListener)5 Configuration (com.vaadin.addon.charts.model.Configuration)4 DataSeries (com.vaadin.addon.charts.model.DataSeries)3 DataSeriesItem (com.vaadin.addon.charts.model.DataSeriesItem)2 Legend (com.vaadin.addon.charts.model.Legend)2 XAxis (com.vaadin.addon.charts.model.XAxis)2 YAxis (com.vaadin.addon.charts.model.YAxis)2 SolidColor (com.vaadin.addon.charts.model.style.SolidColor)2 VerticalLayout (com.vaadin.ui.VerticalLayout)2 CheckboxClickEvent (com.vaadin.addon.charts.CheckboxClickEvent)1 CheckboxClickListener (com.vaadin.addon.charts.CheckboxClickListener)1 LegendItemClickEvent (com.vaadin.addon.charts.LegendItemClickEvent)1 LegendItemClickListener (com.vaadin.addon.charts.LegendItemClickListener)1 PointSelectEvent (com.vaadin.addon.charts.PointSelectEvent)1 PointSelectListener (com.vaadin.addon.charts.PointSelectListener)1 PointUnselectEvent (com.vaadin.addon.charts.PointUnselectEvent)1