Skip to content

Instantly share code, notes, and snippets.

@awesomebytes
Last active December 9, 2015 10:54
Show Gist options
  • Select an option

  • Save awesomebytes/22a2d11a34b7482727e2 to your computer and use it in GitHub Desktop.

Select an option

Save awesomebytes/22a2d11a34b7482727e2 to your computer and use it in GitHub Desktop.
"""
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import remi.gui as gui
from remi import start, App
from threading import Timer
import random
import time
class SvgPlot(gui.Svg):
def __init__(self, width, height):
super(SvgPlot, self).__init__(width, height)
self.width = width
self.height = height
self.polyList = []
self.font_size = 15
self.plot_inner_border = self.font_size
self.textYMin = gui.SvgText(0,self.height + self.font_size,"min")
self.textYMax = gui.SvgText(0,0,"max")
self.textYMin.style['font-size'] = gui.to_pix(self.font_size)
self.textYMax.style['font-size'] = gui.to_pix(self.font_size)
self.append( str(id(self.textYMin)), self.textYMin )
self.append( str(id(self.textYMax)), self.textYMax )
def append_poly(self, poly):
self.append(str(id(poly)), poly)
self.polyList.append(poly)
poly.textXMin = gui.SvgText(0,0,"actualValue")
poly.textXMax = gui.SvgText(0,0,"actualValue")
poly.textYVal = gui.SvgText(0,0,"actualValue")
poly.textYVal.style['font-size'] = gui.to_pix(self.font_size)
poly.lineYValIndicator = gui.SvgLine(0,0,0,0)
poly.lineXMinIndicator = gui.SvgLine(0,0,0,0)
poly.lineXMaxIndicator = gui.SvgLine(0,0,0,0)
self.append( str(id(poly.textXMin)), poly.textXMin )
self.append( str(id(poly.textXMax)), poly.textXMax )
self.append( str(id(poly.textYVal)), poly.textYVal )
self.append( str(id(poly.lineYValIndicator)), poly.lineYValIndicator )
self.append( str(id(poly.lineXMinIndicator)), poly.lineXMinIndicator )
self.append( str(id(poly.lineXMaxIndicator)), poly.lineXMaxIndicator )
def remove_poly(self, poly):
self.remove(poly)
self.polyList.remove(poly)
self.remove(poly.textXMin)
self.remove(poly.textXMax)
self.remove(poly.textYVal)
def render(self):
self.set_viewbox(-self.plot_inner_border, -self.plot_inner_border, self.width + self.plot_inner_border*2, self.height + self.plot_inner_border*2)
if len(self.polyList)<1:
return
minX = min(self.polyList[0].coordsX)
maxX = max(self.polyList[0].coordsX)
minY = min(self.polyList[0].coordsY)
maxY = max(self.polyList[0].coordsY)
for poly in self.polyList:
minX = min(minX, min(poly.coordsX))
maxX = max(maxX, max(poly.coordsX))
minY = min(minY, min(poly.coordsY))
maxY = max(maxY, max(poly.coordsY))
self.textYMin.set_text( "min:%s"%minY )
self.textYMax.set_text( "max:%s"%maxY )
scaleWidth = 1.0
scaleHeight = 1.0
if (maxX>minX):
scaleWidth = self.width/float(abs(maxX-minX))
if (maxY>minY):
scaleHeight = self.height/float(abs(maxY-minY))
i = 1
for poly in self.polyList:
scaledTranslatedYpos = (-poly.coordsY[-1]+maxY)*scaleHeight
textXpos = self.height/(len(self.polyList)+1)*i
poly.textXMin.set_text( str(min(poly.coordsX)) )
poly.textXMin.set_fill( poly.style['stroke'])
#poly.textXMin.set_position( -scaledTranslatedYpos, (min(poly.coordsX)-minX)*scaleWidth )
poly.textXMin.set_position( -textXpos, (min(poly.coordsX)-minX)*scaleWidth )
poly.textXMin.attributes['transform']="rotate(%s)"%(-90)
poly.textXMax.set_text( str(max(poly.coordsX)) )
poly.textXMax.set_fill( poly.style['stroke'])
poly.textXMax.set_position( -textXpos, (max(poly.coordsX)-minX)*scaleWidth )
#poly.textXMax.set_position( -scaledTranslatedYpos, (maxX-minX)*scaleWidth )
poly.textXMax.attributes['transform']="rotate(%s)"%(-90)
poly.textYVal.set_text( str(poly.coordsY[-1]) )
poly.textYVal.set_fill( poly.style['stroke'])
poly.textYVal.set_position( 0, scaledTranslatedYpos )
#poly.textYVal.set_position( (maxX-minX)*scaleWidth/2.0, scaledTranslatedYpos )
poly.lineYValIndicator.set_stroke(1,poly.style['stroke'])
poly.lineXMinIndicator.set_stroke(1,poly.style['stroke'])
poly.lineXMaxIndicator.set_stroke(1,poly.style['stroke'])
poly.lineYValIndicator.set_coords(0,scaledTranslatedYpos,self.width,scaledTranslatedYpos)
poly.lineXMinIndicator.set_coords((min(poly.coordsX)-minX)*scaleWidth,0,(min(poly.coordsX)-minX)*scaleWidth,self.height)
poly.lineXMaxIndicator.set_coords((max(poly.coordsX)-minX)*scaleWidth,0,(max(poly.coordsX)-minX)*scaleWidth,self.height)
poly.attributes['transform']=('translate(%s,%s)'%(-minX*scaleWidth,(maxY*scaleHeight)) + ' scale(%s,%s)'%((scaleWidth),-(scaleHeight)))
i = i + 1
class MyApp(App):
def __init__(self, *args):
super(MyApp, self).__init__(*args)
def main(self, name='world'):
self.wid = gui.Widget(1200, 1200, False, 10)
self.init_time = time.time()
# Hz to generate_data
default_generation = 50
self.timer_data_gen = 0.1
self.hor_widget0 = gui.Widget(600, 40, True, 10)
self.label_generation = gui.Label(200, 30, "Data generation freq (Hz)")
self.slider_generation = gui.Slider(200, 30, defaultValue=default_generation, min=1, max=100, step=1)
self.slider_generation.set_on_change_listener(self, 'on_change_generation_slider')
self.label_generation_qtty = gui.Label(50, 30, str(default_generation))
self.hor_widget0.append('label', self.label_generation)
self.hor_widget0.append('slider', self.slider_generation)
self.hor_widget0.append('qtty', self.label_generation_qtty)
# Max points in the plot
default_max_points = 100
self.hor_widget2 = gui.Widget(600, 40, True, 10)
self.label_len = gui.Label(200, 30, "Max points length")
self.slider_max_len = gui.Slider(200, 30, defaultValue=default_max_points, min=1, max=1000, step=1)
self.slider_max_len.set_on_change_listener(self, 'on_change_max_len_slider')
self.label_max_qtty = gui.Label(50, 30, str(default_max_points))
self.hor_widget2.append('label', self.label_len)
self.hor_widget2.append('slider', self.slider_max_len)
self.hor_widget2.append('qtty', self.label_max_qtty)
# Append the horizontal slider stuff
self.wid.append('data_gen_slider', self.hor_widget0)
self.wid.append('max_slider', self.hor_widget2)
# Create the plot
self.svgplot = SvgPlot(900, 900)
self.plotData1 = gui.SvgPolyline(default_max_points)
self.plotData1.set_stroke(2.0, 'rgba(255,0,0,0.8)')
self.svgplot.append_poly(self.plotData1)
self.wid.append('plot', self.svgplot)
self.data_gen_timer = Timer(self.timer_data_gen, self.data_generation).start()
# returning the root widget
return self.wid
def on_change_max_len_slider(self, value):
self.label_max_qtty.set_text(str(value))
self.svgplot = SvgPlot(900, 900)
self.plotData1 = gui.SvgPolyline(value)
self.plotData1.set_stroke(2.0, 'rgba(255,0,0,0.8)')
self.svgplot.append_poly(self.plotData1)
self.wid.append('plot', self.svgplot)
def on_change_generation_slider(self, value):
self.timer_data_gen = 1.0 / float(value)
self.label_generation_qtty.set_text(str(value))
def data_generation(self):
x = time.time() - self.init_time
y = random.randrange(-1000, 1000, 1) * 0.1
print x, y
self.plotData1.add_coord(x, y)
# To behave correctly we should render every time we add data
self.svgplot.render()
self.data_gen_timer = Timer(self.timer_data_gen, self.data_generation).start()
if __name__ == "__main__":
start(MyApp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment