2012-07-09 12:38:30 +00:00
# -*- coding: utf-8 -*-
2013-02-25 04:09:03 +00:00
"""
This example demonstrates the creation of a plot with a customized
AxisItem and ViewBox .
"""
2012-07-09 12:38:30 +00:00
import initExample ## Add path to library (just for examples; you do not need this)
import pyqtgraph as pg
from pyqtgraph . Qt import QtCore , QtGui
import numpy as np
import time
class DateAxis ( pg . AxisItem ) :
def tickStrings ( self , values , scale , spacing ) :
2012-10-11 04:57:24 +00:00
strns = [ ]
2012-11-10 16:22:56 +00:00
rng = max ( values ) - min ( values )
2012-11-27 19:54:49 +00:00
#if rng < 120:
# return pg.AxisItem.tickStrings(self, values, scale, spacing)
if rng < 3600 * 24 :
2012-11-10 16:22:56 +00:00
string = ' % H: % M: % S '
2012-11-27 19:54:49 +00:00
label1 = ' % b %d - '
label2 = ' % b %d , % Y '
2012-11-10 16:22:56 +00:00
elif rng > = 3600 * 24 and rng < 3600 * 24 * 30 :
string = ' %d '
2012-11-27 19:54:49 +00:00
label1 = ' % b - '
label2 = ' % b, % Y '
elif rng > = 3600 * 24 * 30 and rng < 3600 * 24 * 30 * 24 :
string = ' % b '
label1 = ' % Y - '
label2 = ' % Y '
elif rng > = 3600 * 24 * 30 * 24 :
string = ' % Y '
label1 = ' '
label2 = ' '
2012-10-11 04:57:24 +00:00
for x in values :
try :
2012-11-10 16:22:56 +00:00
strns . append ( time . strftime ( string , time . localtime ( x ) ) )
2012-10-11 04:57:24 +00:00
except ValueError : ## Windows can't handle dates before 1970
strns . append ( ' ' )
2012-11-27 19:54:49 +00:00
try :
label = time . strftime ( label1 , time . localtime ( min ( values ) ) ) + time . strftime ( label2 , time . localtime ( max ( values ) ) )
except ValueError :
label = ' '
#self.setLabel(text=label)
2012-10-11 04:57:24 +00:00
return strns
2012-07-09 12:38:30 +00:00
class CustomViewBox ( pg . ViewBox ) :
def __init__ ( self , * args , * * kwds ) :
pg . ViewBox . __init__ ( self , * args , * * kwds )
self . setMouseMode ( self . RectMode )
## reimplement right-click to zoom out
def mouseClickEvent ( self , ev ) :
if ev . button ( ) == QtCore . Qt . RightButton :
self . autoRange ( )
def mouseDragEvent ( self , ev ) :
if ev . button ( ) == QtCore . Qt . RightButton :
ev . ignore ( )
else :
pg . ViewBox . mouseDragEvent ( self , ev )
app = pg . mkQApp ( )
axis = DateAxis ( orientation = ' bottom ' )
vb = CustomViewBox ( )
pw = pg . PlotWidget ( viewBox = vb , axisItems = { ' bottom ' : axis } , enableMenu = False , title = " PlotItem with custom axis and ViewBox<br>Menu disabled, mouse behavior changed: left-drag to zoom, right-click to reset zoom " )
dates = np . arange ( 8 ) * ( 3600 * 24 * 356 )
pw . plot ( x = dates , y = [ 1 , 6 , 2 , 4 , 3 , 5 , 6 , 8 ] , symbol = ' o ' )
pw . show ( )
2013-02-25 04:09:03 +00:00
pw . setWindowTitle ( ' pyqtgraph example: customPlot ' )
2012-07-09 12:38:30 +00:00
r = pg . PolyLineROI ( [ ( 0 , 0 ) , ( 10 , 10 ) ] )
pw . addItem ( r )
## Start Qt event loop unless running in interactive mode or using pyside.
2012-12-05 05:25:45 +00:00
if __name__ == ' __main__ ' :
import sys
if ( sys . flags . interactive != 1 ) or not hasattr ( QtCore , ' PYQT_VERSION ' ) :
QtGui . QApplication . instance ( ) . exec_ ( )