1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.cafesip.gwtcomp.examples.server;
20
21 import java.security.Principal;
22 import java.util.Date;
23 import java.util.Random;
24
25 import javax.servlet.http.HttpSession;
26
27 import org.cafesip.gwtcomp.client.common.ChartData;
28 import org.cafesip.gwtcomp.client.common.ChartInfo;
29 import org.cafesip.gwtcomp.client.common.ChartInitData;
30 import org.cafesip.gwtcomp.client.common.SeriesInfo;
31 import org.cafesip.gwtcomp.client.common.TimeSeriesPlotData;
32 import org.cafesip.gwtcomp.client.common.TimeSeriesPlotInfo;
33 import org.cafesip.gwtcomp.server.LiveDataSource;
34 import org.cafesip.gwtcomp.server.LiveDataSessionInfo;
35
36
37
38
39
40 public class LiveDataSourceSample implements LiveDataSource
41 {
42 private Random rand = new Random(new Date().getTime());
43
44 public LiveDataSourceSample()
45 {
46 }
47
48 public ChartInitData getChartInfoAndInitialData(Principal user,
49 HttpSession session, LiveDataSessionInfo sessionData)
50 {
51 SeriesInfo[] serie = new SeriesInfo[2];
52
53 serie[0] = new SeriesInfo("Google");
54 serie[1] = new SeriesInfo("Microsoft");
55 TimeSeriesPlotInfo plot = new TimeSeriesPlotInfo(serie, 0.0, 100.0,
56 5000L);
57 ChartInfo cinfo = new ChartInfo("Stock Chart", true, plot, 5);
58
59 TimeSeriesPlotData[] pdata = initInitialData();
60
61 ChartInitData cdata = new ChartInitData(cinfo, new ChartData(
62 new Date(), pdata));
63
64 return cdata;
65
66 }
67
68 public ChartData getNextData(Principal user, HttpSession session,
69 LiveDataSessionInfo sessionData)
70 {
71 TimeSeriesPlotData[] pdata = new TimeSeriesPlotData[1];
72
73 long dt = new Date().getTime();
74
75 Double[] v1 = new Double[2];
76 v1[0] = new Double(genRandom());
77 v1[1] = new Double(genRandom());
78 pdata[0] = new TimeSeriesPlotData(new Date(dt), v1);
79
80 return new ChartData(new Date(), pdata);
81 }
82
83 private TimeSeriesPlotData[] initInitialData()
84 {
85 long period = 5 * 60 * 1000L;
86 long interval = 5 * 1000L;
87
88 int times = (int) (period / interval);
89
90 TimeSeriesPlotData[] pdata = new TimeSeriesPlotData[times];
91 long dt = new Date().getTime();
92 for (int i = 0; i < times; i++)
93 {
94 Double[] v = new Double[2];
95 v[0] = new Double(genRandom());
96 v[1] = new Double(genRandom());
97 pdata[i] = new TimeSeriesPlotData(new Date(dt
98 - (interval * (times - (i + 1)))), v);
99 }
100
101 return pdata;
102 }
103
104 private Double genRandom()
105 {
106 return new Double(rand.nextInt(100));
107 }
108
109 public void close(Principal user, HttpSession session,
110 LiveDataSessionInfo sessionData)
111 {
112 }
113 }