File: BarPlot.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (149 lines) | stat: -rw-r--r-- 4,146 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python

import wx
## import the installed version
from wx.lib.floatcanvas import NavCanvas, FloatCanvas

## import a local version
#import sys
#sys.path.append("../")
#from floatcanvas import NavCanvas, FloatCanvas

import numpy as np
from numpy import random as random

NumChannels = 200
MaxValue = 2000
#MaxValue = 2**24


def YScaleFun(center):
    """
    Function that returns a scaling vector to scale y data to same range as x data

    This is used by FloatCanvas as a "projection function", so that you can have
    a different scale for X and Y. With the default projection, X and Y are the same scale.

    """

    # center gets ignored in this case
    return np.array((1, float(NumChannels)/MaxValue), float)

def ScaleWorldToPixel(self, Lengths):
    """
        This is a new version of a function that will get passed to the
        drawing functions of the objects, to Change a length from world to
        pixel coordinates.

        This version uses the "ceil" function, so that fractional pixel get
        rounded up, rather than down.

        Lengths should be a NX2 array of (x,y) coordinates, or
        a 2-tuple, or sequence of 2-tuples.
    """
    return  np.ceil(( (np.asarray(Lengths, float)*self.TransformVector) )).astype('i')


class DrawFrame(wx.Frame):

    """
    A frame used for the FloatCanvas Demo

    """

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.CreateStatusBar()

        # Add the Canvas
        FloatCanvas.FloatCanvas.ScaleWorldToPixel = ScaleWorldToPixel
        NC = NavCanvas.NavCanvas(self,-1,
                                     size = (500,500),
                                     BackgroundColor = "DARK SLATE BLUE",
                                     ProjectionFun = YScaleFun,
                                     )

        self.Canvas = Canvas = NC.Canvas
        #self.Canvas.ScaleWorldToPixel = ScaleWorldToPixel

        self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove)

        self.Values = random.randint(0, MaxValue, (NumChannels,))

        self.Bars = []
        self.BarWidth = 0.75
        # add an X axis
        Canvas.AddLine(((0,0), (NumChannels, 0 )),)
        for x in np.linspace(1, NumChannels, 11):
            Canvas.AddText("%i"%x, (x-1+self.BarWidth/2,0), Position="tc")

        for i, Value in enumerate(self.Values):
            bar = Canvas.AddRectangle(XY=(i, 0),
                                      WH=(self.BarWidth, Value),
                                      LineColor = None,
                                      LineStyle = "Solid",
                                      LineWidth    = 1,
                                      FillColor    = "Red",
                                      FillStyle    = "Solid",
                                      )
            self.Bars.append(bar)

        # Add a couple a button the Toolbar

        tb = NC.ToolBar
        tb.AddSeparator()

        ResetButton = wx.Button(tb, label="Reset")
        tb.AddControl(ResetButton)
        ResetButton.Bind(wx.EVT_BUTTON, self.ResetData)

#        PlayButton = wx.Button(tb, wx.ID_ANY, "Run")
#        tb.AddControl(PlayButton)
#        PlayButton.Bind(wx.EVT_BUTTON, self.RunTest)
        tb.Realize()

        self.Show()
        Canvas.ZoomToBB()
        Canvas.Draw(True)


    def OnMove(self, event):
        """
        Updates the status bar with the world coordinates

        """
        channel, value = event.Coords
        if 0 < channel < NumChannels  :
            channel = "%i,"%(channel+1)
        else:
            channel = ""

        if value >=0:
            value = "%3g"%value
        else:
            value = ""
        self.SetStatusText("Channel: %s  Value: %s"%(channel, value))

    def ResetData(self, event):
        self.Values = random.randint(0, MaxValue, (NumChannels,))
        for i, bar in enumerate(self.Bars):
            bar.SetShape(bar.XY, (self.BarWidth, self.Values[i]))
        self.Canvas.Draw(Force=True)

app = wx.App(False)
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700,700) )
app.MainLoop()