File: GChartExample25.py

package info (click to toggle)
pyjamas 0.7~%2Bpre2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,656 kB
  • ctags: 12,331
  • sloc: python: 74,493; php: 805; sh: 291; makefile: 59; xml: 9
file content (110 lines) | stat: -rw-r--r-- 3,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import math

from pyjamas.chart import GChartUtil
from pyjamas.chart.GChart import GChart
from pyjamas.chart import AnnotationLocation
from pyjamas.chart import SymbolType

from pyjamas.ui.Button import Button
from pyjamas.ui.FocusPanel import FocusPanel
from pyjamas.ui.Grid import Grid
from pyjamas.ui import KeyboardListener
from pyjamas import DOM




"""*
* Example of how to add direct keyboard support to a GChart
* by wrapping it within a FocusPanel.
* <p>
*
* The example allows lets the user press the left and right arrow keys
* to move the selected point to the previous or next point after the
* currently selected point.
*
* For the more common case where you are handling mouse click
* events only, as of v2.61, GChart implements both GWT's
* HasMouse*Handlers and HasClickHandlers interfaces (c.f.
* GChartExample24.java) so this wrapper technique is usually not
* needed.
*
"""

N_POINTS = 100
BLUE = "#318ce0"
SKY_BLUE = "#c6defa"

class ChildGChart(GChart):

    def __init__(self):
        GChart.__init__(self)

        self.setChartTitle(
        "Click on chart, then use left/right arrows to switch selected point")
        self.setHoverTouchingEnabled(True)
        self.setChartSize(500, 150)
        self.setPadding("10px")
        self.getXAxis().setTickCount(11)
        self.getYAxis().setTickCount(11)
        self.addCurve()
        for i in range(N_POINTS+1):
            self.getCurve().addPoint(i, math.sin((2* math.pi * i)/N_POINTS))

        self.getCurve().getSymbol().setWidth(5)
        self.getCurve().getSymbol().setBorderColor(BLUE)
        self.getCurve().getSymbol().setBackgroundColor(SKY_BLUE)
        self.getCurve().getSymbol().setHoverSelectionBackgroundColor(BLUE)
        self.getCurve().getSymbol().setHoverSelectionBorderColor(SKY_BLUE)
        self.getCurve().getSymbol().setSymbolType(
                                SymbolType.VBAR_BASELINE_CENTER)
        self.getCurve().getSymbol().setHoverLocation(
                                AnnotationLocation.NORTH)
        self.getCurve().getSymbol().setHoverYShift(5)
        self.setPixelSize(self.getXChartSizeDecorated(),
                            self.getYChartSizeDecorated())



class GChartExample25 (FocusPanel):

    def __init__(self):
        FocusPanel.__init__(self)
        self.theChild = ChildGChart()
        self.theChild.update()
        self.setWidget(self.theChild)
        self.addKeyboardListener(self)
        self.addMouseListener(self)

    def onKeyDown(self, sender, keycode, modifiers):
        event = DOM.eventGetCurrentEvent()
        DOM.eventPreventDefault(event)
        # ignore mouse position when arrow-key pressing
        if self.theChild.getHoverTouchingEnabled():
            self.theChild.setHoverTouchingEnabled(False)

        p = self.theChild.getTouchedPoint()
        c = self.theChild.getCurve(); # only one curve on chart
        iPoint = 0
        if p is not None:
            iPoint = c.getPointIndex(p)
        if keycode == KeyboardListener.KEY_LEFT:
            iPoint = (iPoint + N_POINTS) % (N_POINTS+1)
        elif keycode == KeyboardListener.KEY_RIGHT:
            iPoint = (iPoint + 1) % (N_POINTS+1)

        self.theChild.touch(c.getPoint(iPoint))
        self.theChild.update()

    def onMouseMove(self, sender, x, y):
        event = DOM.eventGetCurrentEvent()
        DOM.eventPreventDefault(event)
        # mousing auto re-enables mouse-over hover feedback
        if not self.theChild.getHoverTouchingEnabled():
            self.theChild.setHoverTouchingEnabled(True)
            self.theChild.update()

    def setOptimizeForMemory(self, optimize):
        self.theChild.setOptimizeForMemory( optimize)
    def update(self):
        self.theChild.update()