Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 131 lines (105 sloc) 4.988 kb
2d56f0e more gchart update
lkcl authored
1 import math
2
3 from pyjamas.chart import GChartUtil
4 from pyjamas.chart.GChart import GChart
5 from pyjamas.chart import AnnotationLocation
6 from pyjamas.chart import SymbolType
7 from pyjamas.chart import Double
8
02cf9af gchart update
lkcl authored
9 from pyjamas import DeferredCommand
2d56f0e more gchart update
lkcl authored
10 from pyjamas.ui.Button import Button
11
12
13 """* Sine curve with lots of points on it, to illustrate the
14 ** incremental update technique.
15 ** <p>
16 **
17 ** Prior to GChart 2.1, this approach would not have worked
18 ** GChart 2.1 allows curves that have not changed since the
19 ** last update (as long as they occur before the first curve
20 ** that HAS changed) to be skipped over in re-updates. So you
21 ** don't lose much time by updating incrementally in this
22 ** manner, and you gain much better user feedback during
23 ** that all-important first page display.
24 ** <p>
25 **
26 ** Whenever possible, try to avoid very numbers of HTML
27 ** elements in your charts (swapping in data as needed in
28 ** response to user requests instead). That way, this kind
29 ** of delay can be avoided entirely, and you won't have to
30 ** implement an incrementally updating chart at all.
31 **
32 **
33 *"""
34
35 PERIOD = 100
36 N_PERIODS = 5
37 DELTA_TIME = 1
38 DELTA_PHASE = 8
39 firstTime = True
40
41 class IncrementalUpdate:
42 def __init__(self, gchart, iCurve, phaseShift, n):
43 self.gchart = gchart
44 self.iCurve = iCurve
45 self.phaseShift = phaseShift
46 self.n = n
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
47
2d56f0e more gchart update
lkcl authored
48 def execute(self):
49 for iC in range(self.iCurve, self.iCurve+self.n):
02cf9af gchart update
lkcl authored
50 if self.gchart.getNCurves() <= self.iCurve:
2d56f0e more gchart update
lkcl authored
51 # gchart.getCurve(0).getPoint(0).setY(
52 # gchart.getCurve(0).getPoint(0).getY())
53 self.gchart.addCurve()
54 # copy symbol properties from curve 0, except, None
55 # legend label (so only one curve row on legend)
56 self.gchart.getCurve().getSymbol().setSymbolType(
57 self.gchart.getCurve(0).getSymbol().getSymbolType())
58 self.gchart.getCurve().getSymbol().setBorderWidth(
59 self.gchart.getCurve(0).getSymbol().getBorderWidth())
60 self.gchart.getCurve().getSymbol().setBackgroundColor(
61 self.gchart.getCurve(0).getSymbol().getBackgroundColor())
62 self.gchart.getCurve().getSymbol().setFillSpacing(
63 self.gchart.getCurve(0).getSymbol().getFillSpacing())
64 self.gchart.getCurve().getSymbol().setFillThickness(
65 self.gchart.getCurve(0).getSymbol().getFillThickness())
66 self.gchart.getCurve().getSymbol().setHeight(
67 self.gchart.getCurve(0).getSymbol().getHeight())
68 self.gchart.getCurve().getSymbol().setWidth(
69 self.gchart.getCurve(0).getSymbol().getWidth())
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
70
2d56f0e more gchart update
lkcl authored
71 for i in range(0, PERIOD, DELTA_TIME):
72 y = math.sin((2*math.pi*(iC*PERIOD+i+self.phaseShift))/PERIOD)
73 if self.gchart.getCurve(iC).getNPoints()*DELTA_TIME <= i:
74 self.gchart.getCurve(iC).addPoint(iC*PERIOD+i+self.phaseShift, y)
75 else:
76 self.gchart.getCurve(iC).getPoint(i/DELTA_TIME).setY(y)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
77
78
79
2d56f0e more gchart update
lkcl authored
80 self.gchart.update()
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
81
82
2d56f0e more gchart update
lkcl authored
83
84
85 class GChartExample14 (GChart):
86 def __init__(self):
87 GChart.__init__(self)
88
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
89
2d56f0e more gchart update
lkcl authored
90 self.phase = 0
91 self.btn = Button("Update", self)
92 self.setChartFootnotes(self.btn)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
93
2d56f0e more gchart update
lkcl authored
94 self.setChartSize(1000,100)
95 self.setChartTitle("<big><i>Sine vs Time</i></big>")
96 self.setPadding("2px")
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
97
2d56f0e more gchart update
lkcl authored
98 self.getXAxis().setAxisLabel("<small><i>Time (seconds)</i></small>")
99 self.getXAxis().setHasGridlines(True)
100 self.getXAxis().setTickCount(6)
101 self.getXAxis().setTickLabelFormat("#.##")
102 self.getXAxis().setAxisMin(0)
103 self.getXAxis().setAxisMax(PERIOD*N_PERIODS)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
104
2d56f0e more gchart update
lkcl authored
105 self.getYAxis().setHasGridlines(True)
106 self.getYAxis().setTickCount(5)
107 self.getYAxis().setAxisMin(-1)
108 self.getYAxis().setAxisMax(1)
109 self.getYAxis().setTickLabelThickness(10)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
110
2d56f0e more gchart update
lkcl authored
111 self.addCurve()
112 self.getCurve().getSymbol().setSymbolType(SymbolType.VBAR_BASELINE_CENTER)
113 self.getCurve().getSymbol().setBorderWidth(0)
114 self.getCurve().getSymbol().setBackgroundColor("blue")
115 self.getCurve().getSymbol().setFillSpacing(Double.NaN)
116 self.getCurve().getSymbol().setFillThickness(0)
117 self.getCurve().getSymbol().setHeight(1)
118 self.getCurve().getSymbol().setWidth(1)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
119
2d56f0e more gchart update
lkcl authored
120 for i in range(N_PERIODS):
02cf9af gchart update
lkcl authored
121 DeferredCommand.add(IncrementalUpdate(self, i, 0, 1))
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
122
2d56f0e more gchart update
lkcl authored
123 # for int phaseShift=0; phaseShift < N_PERIODS*PERIOD; phaseShift+=DELTA_PHASE)
124 # DeferredCommand.addCommand(IncrementalUpdate(this,0, phaseShift, N_PERIODS))
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
125
02cf9af gchart update
lkcl authored
126 def onClick(self, event):
127 self.phase += DELTA_PHASE
128 for i in range(N_PERIODS):
129 DeferredCommand.add(IncrementalUpdate(self, i, self.phase, 1))
2d56f0e more gchart update
lkcl authored
130
Something went wrong with that request. Please try again.