Skip to content

Instantly share code, notes, and snippets.

@ripla
Created September 11, 2014 17:48
Show Gist options
  • Save ripla/812118e490551d0c3863 to your computer and use it in GitHub Desktop.
Save ripla/812118e490551d0c3863 to your computer and use it in GitHub Desktop.
package com.vaadin.test;
import com.vaadin.addon.charts.Chart;
import com.vaadin.addon.charts.model.*;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.UI;
import javax.servlet.annotation.WebServlet;
import java.util.Timer;
import java.util.TimerTask;
@Theme("mytheme")
@Push
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false,
ui = MyVaadinUI.class,
widgetset = "com.vaadin.test.AppWidgetSet")
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final GridLayout layout = new GridLayout();
layout.setSizeFull();
layout.setColumns(2);
layout.setMargin(true);
layout.addComponent(getChart());
layout.addComponent(getChart());
layout.addComponent(getChart());
layout.addComponent(getChart());
setContent(layout);
}
protected Component getChart() {
final Chart chart = new Chart();
chart.setHeight("450px");
chart.setWidth("100%");
Configuration configuration = new Configuration();
configuration.getChart().setType(ChartType.LINE);
configuration.getChart().setMarginRight(130);
configuration.getChart().setMarginBottom(25);
configuration.getChart().setAnimation(true);
configuration.getTitle().setText("Monthly Average Temperature");
configuration.getSubTitle().setText("Source: WorldClimate.com");
configuration.getxAxis()
.setCategories("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec");
Axis yAxis = configuration.getyAxis();
yAxis.setMin(-5d);
yAxis.setTitle(new Title("Temperature (°C)"));
yAxis.getTitle().setVerticalAlign(VerticalAlign.HIGH);
configuration
.getTooltip()
.setFormatter(
"''+ this.series.name +''+this.x +': '+ this.y +'°C'");
PlotOptionsLine plotOptions = new PlotOptionsLine();
plotOptions.setDataLabels(new Labels(true));
configuration.setPlotOptions(plotOptions);
Legend legend = configuration.getLegend();
legend.setLayout(LayoutDirection.VERTICAL);
legend.setHorizontalAlign(HorizontalAlign.RIGHT);
legend.setVerticalAlign(VerticalAlign.TOP);
legend.setX(-10d);
legend.setY(100d);
legend.setBorderWidth(0);
ListSeries ls = new ListSeries();
ls.setName("Tokyo");
ls.setData(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3,
18.3,
13.9, 9.6);
configuration.addSeries(ls);
ls = new ListSeries();
ls.setName("New York");
ls.setData(-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1,
14.1,
8.6, 2.5);
configuration.addSeries(ls);
ls = new ListSeries();
ls.setName("Berlin");
ls.setData(-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3,
9.0, 3.9,
1.0);
configuration.addSeries(ls);
chart.drawChart(configuration);
new Timer().schedule(new TimerTask() {
@Override public void run() {
getUI().access(new Runnable() {
@Override public void run() {
for (Series s : chart
.getConfiguration()
.getSeries()) {
((ListSeries) s)
.addData(
Math.round(
Math.random()
* 10));
}
}
});
}
}, 3000, 3000);
return chart;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment