| 1 |
|
| 2 |
try: |
| 3 |
import Tkinter |
| 4 |
import tkSimpleDialog |
| 5 |
import tkFileDialog |
| 6 |
except ImportError: |
| 7 |
import tkinter as Tkinter |
| 8 |
from tkinter import simpledialog as tkSimpleDialog |
| 9 |
from tkinter import filedialog as tkFileDialog |
| 10 |
from auto.graphics import Pmw |
| 11 |
from auto import AUTOutil |
| 12 |
from auto.graphics import optionHandler |
| 13 |
import math |
| 14 |
import sys |
| 15 |
import string |
| 16 |
|
| 17 |
GrapherError="GrapherError" |
| 18 |
Axes3D=None |
| 19 |
|
| 20 |
class BasicGrapher(optionHandler.OptionHandler,Tkinter.Canvas): |
| 21 |
"""Documentation string for Basic Grapher |
| 22 |
|
| 23 |
A simple graphing widget |
| 24 |
By Randy P.""" |
| 25 |
def __init__(self,parent=None,callback=None,**kw): |
| 26 |
self.data = [] |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
optionDefaults={} |
| 31 |
optionDefaults["minx"] = (0,callback) |
| 32 |
optionDefaults["maxx"] = (0,callback) |
| 33 |
optionDefaults["miny"] = (0,callback) |
| 34 |
optionDefaults["maxy"] = (0,callback) |
| 35 |
optionDefaults["minz"] = (0,callback) |
| 36 |
optionDefaults["maxz"] = (0,callback) |
| 37 |
optionDefaults["azimuth"] = (None,callback) |
| 38 |
optionDefaults["elevation"] = (None,callback) |
| 39 |
optionDefaults["left_margin"] = (80,callback) |
| 40 |
optionDefaults["right_margin"] = (40,callback) |
| 41 |
optionDefaults["top_margin"] = (40,callback) |
| 42 |
optionDefaults["bottom_margin"] = (40,callback) |
| 43 |
optionDefaults["decorations"] = (True,callback) |
| 44 |
optionDefaults["xlabel"] = (None,callback) |
| 45 |
optionDefaults["xlabel_fontsize"] = (None,callback) |
| 46 |
optionDefaults["ylabel"] = (None,callback) |
| 47 |
optionDefaults["ylabel_fontsize"] = (None,callback) |
| 48 |
optionDefaults["zlabel"] = (None,callback) |
| 49 |
optionDefaults["zlabel_fontsize"] = (None,callback) |
| 50 |
optionDefaults["xticks"] = (5,callback) |
| 51 |
optionDefaults["yticks"] = (5,callback) |
| 52 |
optionDefaults["zticks"] = (5,callback) |
| 53 |
optionDefaults["grid"] = (True,callback) |
| 54 |
optionDefaults["tick_label_template"] = ("%.2e",callback) |
| 55 |
optionDefaults["tick_length"] = (0.2,callback) |
| 56 |
optionDefaults["odd_tick_length"] = (0.4,callback) |
| 57 |
optionDefaults["even_tick_length"] = (0.2,callback) |
| 58 |
|
| 59 |
optionDefaults["foreground"] = ("black",callback) |
| 60 |
optionDefaults["color_list"] = ("black red green blue",callback) |
| 61 |
optionDefaults["symbol_font"] = ("-misc-fixed-*-*-*-*-*-*-*-*-*-*-*-*",callback) |
| 62 |
optionDefaults["symbol_color"] = ("red",callback) |
| 63 |
optionDefaults["smart_label"] = (True,callback) |
| 64 |
optionDefaults["line_width"] = (2,callback) |
| 65 |
optionDefaults["realwidth"] = (1,callback) |
| 66 |
optionDefaults["realheight"] = (1,callback) |
| 67 |
optionDefaults["use_labels"] = (True,callback) |
| 68 |
optionDefaults["use_symbols"] = (True,callback) |
| 69 |
optionDefaults["top_title"] = ("",callback) |
| 70 |
optionDefaults["top_title_fontsize"] = (None,callback) |
| 71 |
optionDefaults["dashes"] = ((6.0,6.0),callback) |
| 72 |
optionDefaults["width"] = (600,callback) |
| 73 |
optionDefaults["height"] = (480,callback) |
| 74 |
|
| 75 |
optionAliases = {} |
| 76 |
optionAliases["fg"] = "foreground" |
| 77 |
|
| 78 |
|
| 79 |
if kw.get("hide") and 'graphics.grapher_mpl' in sys.modules: |
| 80 |
optionHandler.OptionHandler.__init__(self) |
| 81 |
else: |
| 82 |
try: |
| 83 |
Tkinter.Canvas.__init__(self,parent) |
| 84 |
optionHandler.OptionHandler.__init__(self,Tkinter.Canvas) |
| 85 |
except Tkinter.TclError: |
| 86 |
optionHandler.OptionHandler.__init__(self) |
| 87 |
|
| 88 |
for key in list(kw): |
| 89 |
if key not in optionDefaults: |
| 90 |
del kw[key] |
| 91 |
self.addOptions(**optionDefaults) |
| 92 |
self.addAliases(**optionAliases) |
| 93 |
BasicGrapher._configNoDraw(self,**kw) |
| 94 |
|
| 95 |
def __len__(self): |
| 96 |
return len(self.data) |
| 97 |
|
| 98 |
def config(self,cnf=None,**kw): |
| 99 |
rval = self._configNoDraw(cnf,**kw) |
| 100 |
if isinstance(cnf, str) or (cnf is None and not kw): |
| 101 |
return rval |
| 102 |
self.clear() |
| 103 |
self.draw() |
| 104 |
configure=config |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
def _configNoDraw(self,cnf=None,**kw): |
| 110 |
|
| 111 |
if (cnf is not None or kw) and not isinstance(cnf, str): |
| 112 |
dct = (cnf or {}).copy() |
| 113 |
dct.update(kw) |
| 114 |
for coord in ["x", "y", "z"]: |
| 115 |
minc = "min" + coord |
| 116 |
maxc = "max" + coord |
| 117 |
ticks = coord + "ticks" |
| 118 |
if (minc in dct or maxc in dct) and ticks not in dct: |
| 119 |
dct[ticks] = None |
| 120 |
return optionHandler.OptionHandler.config(self,**dct) |
| 121 |
return optionHandler.OptionHandler.config(self,cnf,**kw) |
| 122 |
_configureNoDraw = _configNoDraw |
| 123 |
|
| 124 |
def _addData(self,data,newsect=None,color=None,stable=None): |
| 125 |
for array in data: |
| 126 |
if len(array[0]) != len(array[1]): |
| 127 |
raise GrapherError("Array lengths must match") |
| 128 |
new_array={} |
| 129 |
new_array["x"]=array[0] |
| 130 |
new_array["y"]=array[1] |
| 131 |
if len(array) > 2: |
| 132 |
new_array["z"]=array[2] |
| 133 |
new_array["stable"]=stable |
| 134 |
new_array["newsect"]=newsect |
| 135 |
new_array["color"]=color |
| 136 |
if len(array[0]) > 0: |
| 137 |
new_array["minx"]=min(array[0]) |
| 138 |
new_array["maxx"]=max(array[0]) |
| 139 |
if len(array[1]) > 0: |
| 140 |
new_array["miny"]=min(array[1]) |
| 141 |
new_array["maxy"]=max(array[1]) |
| 142 |
if "z" in new_array and len(array[2]) > 0: |
| 143 |
new_array["minz"]=min(array[2]) |
| 144 |
new_array["maxz"]=max(array[2]) |
| 145 |
self.data.append(new_array) |
| 146 |
|
| 147 |
def addData(self,data): |
| 148 |
self._addData(data) |
| 149 |
self.computeXRange() |
| 150 |
self.computeYRange() |
| 151 |
self.computeZRange() |
| 152 |
self.draw() |
| 153 |
|
| 154 |
def addArray(self,array): |
| 155 |
self._addData((array,)) |
| 156 |
self.computeXRange() |
| 157 |
self.computeYRange() |
| 158 |
self.computeZRange() |
| 159 |
self.draw() |
| 160 |
|
| 161 |
def addDataNoDraw(self,data): |
| 162 |
self._addData(data) |
| 163 |
|
| 164 |
def addArrayNoDraw(self,array,newsect=None,color=None,stable=None): |
| 165 |
self._addData((array,),newsect,color,stable) |
| 166 |
|
| 167 |
def _delAllData(self): |
| 168 |
self.data=[] |
| 169 |
|
| 170 |
try: |
| 171 |
zcolumn = self.cget(self.cget("type")+"_z") |
| 172 |
except Tkinter.TclError: |
| 173 |
return |
| 174 |
if zcolumn is not None: |
| 175 |
self._configNoDraw({self.cget("type")+"_z":None}) |
| 176 |
print("\nWithout matplotlib 3D plots are not supported.") |
| 177 |
print("Plotting only the first two coordinates.") |
| 178 |
|
| 179 |
def delAllData(self): |
| 180 |
self._delAllData() |
| 181 |
self.clear() |
| 182 |
|
| 183 |
def _delData(self,index): |
| 184 |
del self.data[index] |
| 185 |
|
| 186 |
def delData(self,index): |
| 187 |
self._delData(index) |
| 188 |
self.clear() |
| 189 |
self.draw() |
| 190 |
|
| 191 |
def _round(self,val,increment): |
| 192 |
"This function returns the closest integer multiple to increment" |
| 193 |
quotient = val/increment |
| 194 |
remainder = quotient-math.floor(quotient) |
| 195 |
if remainder < 0.5: |
| 196 |
return math.floor(quotient)*increment |
| 197 |
else: |
| 198 |
return (math.floor(quotient)+1)*increment |
| 199 |
|
| 200 |
def _computeNiceRanges(self,minimum,maximum): |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
therange = maximum - minimum |
| 207 |
inc = math.pow(10,math.ceil(math.log10(therange) - 1.0)) |
| 208 |
if therange / inc <= 2: |
| 209 |
inc = inc / 4 |
| 210 |
elif therange / inc <= 4: |
| 211 |
inc = inc / 2 |
| 212 |
minimumr = self._round(minimum,inc) |
| 213 |
if minimumr > minimum: |
| 214 |
minimum = minimumr - inc |
| 215 |
else: |
| 216 |
minimum = minimumr |
| 217 |
maximumr = self._round(maximum,inc) |
| 218 |
if maximumr < maximum: |
| 219 |
maximum = maximumr + inc |
| 220 |
else: |
| 221 |
maximum = maximumr ; |
| 222 |
num = int(round(( maximum - minimum ) / inc)) |
| 223 |
return {"min": minimum, "max": maximum, "divisions": num + 1} |
| 224 |
|
| 225 |
def computeRange(self,coordinate,guess_minimum=None,guess_maximum=None): |
| 226 |
minc = "min"+coordinate |
| 227 |
maxc = "max"+coordinate |
| 228 |
if len(self.data) > 0 and minc not in self.data[0]: |
| 229 |
return |
| 230 |
if guess_minimum is None and len(self.data) > 0: |
| 231 |
guess_minimum = min([entry[minc] for entry in self.data]) |
| 232 |
|
| 233 |
if guess_maximum is None and len(self.data) > 0: |
| 234 |
guess_maximum = max([entry[maxc] for entry in self.data]) |
| 235 |
|
| 236 |
if guess_minimum != guess_maximum: |
| 237 |
d = self._computeNiceRanges(guess_minimum,guess_maximum) |
| 238 |
self._configNoDraw(**{minc:d["min"],maxc:d["max"], |
| 239 |
coordinate+'ticks':d["divisions"]}) |
| 240 |
elif guess_maximum != None: |
| 241 |
self._configNoDraw(**{minc:guess_minimum-1,maxc:guess_maximum+1}) |
| 242 |
|
| 243 |
def computeXRange(self,guess_minimum=None,guess_maximum=None): |
| 244 |
self.computeRange("x",guess_minimum,guess_maximum) |
| 245 |
|
| 246 |
def computeYRange(self,guess_minimum=None,guess_maximum=None): |
| 247 |
self.computeRange("y",guess_minimum,guess_maximum) |
| 248 |
|
| 249 |
def computeZRange(self,guess_minimum=None,guess_maximum=None): |
| 250 |
self.computeRange("z",guess_minimum,guess_maximum) |
| 251 |
|
| 252 |
def getXRange(self): |
| 253 |
return [self.cget("minx"),self.cget("maxx")] |
| 254 |
|
| 255 |
def getYRange(self): |
| 256 |
return [self.cget("miny"),self.cget("maxy")] |
| 257 |
|
| 258 |
def getZRange(self): |
| 259 |
return [self.cget("minz"),self.cget("maxz")] |
| 260 |
|
| 261 |
def clear(self): |
| 262 |
if hasattr(self, "tk"): |
| 263 |
for x in self.find_all(): |
| 264 |
self.delete(x) |
| 265 |
|
| 266 |
def plot(self): |
| 267 |
pass |
| 268 |
|
| 269 |
def draw(self): |
| 270 |
color_list = self.cget("color_list").split() |
| 271 |
minx=self.cget("minx") |
| 272 |
maxx=self.cget("maxx") |
| 273 |
miny=self.cget("miny") |
| 274 |
maxy=self.cget("maxy") |
| 275 |
top_margin = self.cget("top_margin") |
| 276 |
bottom_margin = self.cget("bottom_margin") |
| 277 |
left_margin = self.cget("left_margin") |
| 278 |
right_margin = self.cget("right_margin") |
| 279 |
width = int(self.cget("realwidth")) |
| 280 |
height = int(self.cget("realheight")) |
| 281 |
|
| 282 |
if self.cget("decorations"): |
| 283 |
|
| 284 |
self.create_polygon(left_margin,top_margin, |
| 285 |
int(width)-right_margin,top_margin, |
| 286 |
int(width)-right_margin,int(height)-bottom_margin, |
| 287 |
left_margin,int(height)-bottom_margin,fill="",outline=self.cget("foreground")) |
| 288 |
|
| 289 |
line_width=self.cget("line_width") |
| 290 |
adjwidth = width - (left_margin + right_margin) |
| 291 |
adjheight = height - (top_margin + bottom_margin) |
| 292 |
xscale = (float(maxx) - minx) / adjwidth |
| 293 |
yscale = (float(maxy) - miny) / adjheight |
| 294 |
i=-1 |
| 295 |
for d in self.data: |
| 296 |
if d["newsect"] is None or d["newsect"]: |
| 297 |
i = i+1 |
| 298 |
if d["color"] is None: |
| 299 |
color = i |
| 300 |
else: |
| 301 |
color = d["color"] |
| 302 |
fill=color_list[color%len(color_list)] |
| 303 |
curve="curve:%d"%(i,) |
| 304 |
n=len(d["x"]) |
| 305 |
[x,y]=self.__valueToCanvasFast([d["x"][0],d["y"][0]],minx,maxx,miny,maxy, |
| 306 |
width,height,left_margin,right_margin,top_margin,bottom_margin) |
| 307 |
|
| 308 |
if n == 1: |
| 309 |
self.create_oval(x-3,y-3,x+3,y+3, |
| 310 |
tags=("data_point:%d"%(0,),"curve:%d"%(i,),"data"), |
| 311 |
fill=fill) |
| 312 |
else: |
| 313 |
line = [x, y] |
| 314 |
xs = d["x"] |
| 315 |
ys = d["y"] |
| 316 |
stable = d["stable"] |
| 317 |
for j in range(1, n): |
| 318 |
line.append((xs[j]-minx) / xscale + left_margin) |
| 319 |
line.append((adjheight - (ys[j]-miny) / yscale + top_margin)) |
| 320 |
if stable is None or stable: |
| 321 |
self.create_line(line,width=line_width,tags=(curve,"data"),fill=fill) |
| 322 |
else: |
| 323 |
self.create_line(line,width=line_width,tags=(curve,"data"),fill=fill,dash=(10,10)) |
| 324 |
|
| 325 |
|
| 326 |
if self.cget("decorations"): |
| 327 |
|
| 328 |
self.create_polygon(0,0, |
| 329 |
int(width),0, |
| 330 |
int(width),top_margin-1, |
| 331 |
0,top_margin-1, |
| 332 |
fill=self["background"]) |
| 333 |
self.create_polygon(0,0, |
| 334 |
0,int(height), |
| 335 |
left_margin-1,int(height), |
| 336 |
left_margin-1,0, |
| 337 |
fill=self["background"]) |
| 338 |
self.create_polygon(int(width),int(height), |
| 339 |
int(width),0, |
| 340 |
int(width)-right_margin+1,0, |
| 341 |
int(width)-right_margin+1,int(height), |
| 342 |
fill=self["background"]) |
| 343 |
self.create_polygon(int(width),int(height), |
| 344 |
0,int(height), |
| 345 |
0,int(height)-bottom_margin+1, |
| 346 |
int(width),int(height)-bottom_margin+1, |
| 347 |
fill=self["background"]) |
| 348 |
|
| 349 |
|
| 350 |
xw=float(width) - (float(left_margin) + float(right_margin)) |
| 351 |
yw=float(height) - (float(top_margin) + float(bottom_margin)) |
| 352 |
|
| 353 |
tick_label_template=self.cget("tick_label_template") |
| 354 |
tick_length=self.cget("tick_length") |
| 355 |
odd_tick_length=self.cget("odd_tick_length") |
| 356 |
even_tick_length=self.cget("even_tick_length") |
| 357 |
|
| 358 |
xticks = self.cget("xticks") |
| 359 |
if xticks is None: |
| 360 |
xticks = self.config("xticks")[3] |
| 361 |
else: |
| 362 |
xticks=int(xticks) |
| 363 |
tick_start_y=yw+bottom_margin |
| 364 |
for i in range(xticks): |
| 365 |
|
| 366 |
if i%2==0: |
| 367 |
tick_end_y=yw+bottom_margin*(1+even_tick_length) |
| 368 |
else: |
| 369 |
tick_end_y=yw+bottom_margin*(1+odd_tick_length) |
| 370 |
|
| 371 |
tick_x=left_margin + float(i)*xw/float(xticks-1) |
| 372 |
self.create_line(tick_x,tick_start_y,tick_x,tick_end_y,fill=self.cget("foreground")) |
| 373 |
val = self.canvasToValue((tick_x,tick_start_y)) |
| 374 |
self.create_text(tick_x,tick_end_y,text=tick_label_template%(val[0],),anchor="n",fill=self.cget("foreground")) |
| 375 |
if i != 0 and i != xticks - 1 and self.cget("grid") in ["yes",True]: |
| 376 |
self.create_line(tick_x,tick_start_y,tick_x,tick_start_y-yw, |
| 377 |
fill=self.cget("foreground"),stipple="gray50") |
| 378 |
yticks = self.cget("yticks") |
| 379 |
if yticks is None: |
| 380 |
yticks = self.config("yticks")[3] |
| 381 |
else: |
| 382 |
yticks=int(yticks) |
| 383 |
tick_start_x=left_margin |
| 384 |
tick_end_x=left_margin*(1-tick_length) |
| 385 |
for i in range(yticks): |
| 386 |
tick_y=bottom_margin + float(i)*yw/float(yticks-1) |
| 387 |
self.create_line(tick_start_x,tick_y,tick_end_x,tick_y,fill=self.cget("foreground")) |
| 388 |
val = self.canvasToValue((tick_start_x,tick_y)) |
| 389 |
self.create_text(tick_end_x,tick_y,text=tick_label_template%(val[1],),anchor="e",fill=self.cget("foreground")) |
| 390 |
if i != 0 and i != yticks - 1 and self.cget("grid") in ["yes",True]: |
| 391 |
self.create_line(tick_start_x,tick_y,tick_start_x + xw,tick_y, |
| 392 |
fill=self.cget("foreground"),stipple="gray50") |
| 393 |
|
| 394 |
|
| 395 |
self.create_text(left_margin*0.3,bottom_margin*0.3, |
| 396 |
text=self.cget("ylabel"),anchor="nw",fill=self.cget("foreground")) |
| 397 |
self.create_text(int(width)-left_margin*0.3,int(height)-bottom_margin*0.1, |
| 398 |
text=self.cget("xlabel"),anchor="se",fill=self.cget("foreground")) |
| 399 |
|
| 400 |
self.create_text((left_margin-right_margin+int(width))//2, |
| 401 |
top_margin*0.1,text=self.cget("top_title"),anchor="n", |
| 402 |
fill=self.cget("foreground")) |
| 403 |
|
| 404 |
|
| 405 |
def valueToCanvas(self,val): |
| 406 |
if len(val) != 2: |
| 407 |
raise GrapherError("Illegal value choosen for coordinate transformation. Must be a tuple with 2 elements.") |
| 408 |
|
| 409 |
minx=self.cget("minx") |
| 410 |
maxx=self.cget("maxx") |
| 411 |
miny=self.cget("miny") |
| 412 |
maxy=self.cget("maxy") |
| 413 |
width = int(self.cget("realwidth")) |
| 414 |
height = int(self.cget("realheight")) |
| 415 |
left_margin = self.cget("left_margin") |
| 416 |
right_margin = self.cget("right_margin") |
| 417 |
top_margin = self.cget("top_margin") |
| 418 |
bottom_margin = self.cget("bottom_margin") |
| 419 |
return self.__valueToCanvasFast(val,minx,maxx,miny,maxy, |
| 420 |
width,height,left_margin,right_margin,top_margin,bottom_margin) |
| 421 |
|
| 422 |
def __valueToCanvasFast(self,val,minx,maxx,miny,maxy, |
| 423 |
width,height,left_margin,right_margin,top_margin,bottom_margin): |
| 424 |
x = val[0] |
| 425 |
y = val[1] |
| 426 |
width = width - (left_margin + right_margin) |
| 427 |
height = height - (top_margin + bottom_margin) |
| 428 |
return [((x-minx)/(maxx-minx))*width + left_margin, |
| 429 |
height - ((y-miny)/(maxy-miny))*height + top_margin] |
| 430 |
|
| 431 |
def transform(self,val): |
| 432 |
[x,y] = self.valueToCanvas(val) |
| 433 |
return [x,self.cget("realheight") - y] |
| 434 |
|
| 435 |
def transform_seq(self,seqs): |
| 436 |
minx=self.cget("minx") |
| 437 |
maxx=self.cget("maxx") |
| 438 |
miny=self.cget("miny") |
| 439 |
maxy=self.cget("maxy") |
| 440 |
width = int(self.cget("realwidth")) |
| 441 |
height = int(self.cget("realheight")) |
| 442 |
left_margin = self.cget("left_margin") |
| 443 |
right_margin = self.cget("right_margin") |
| 444 |
top_margin = self.cget("top_margin") |
| 445 |
bottom_margin = self.cget("bottom_margin") |
| 446 |
valuetocanvasfast = self.__valueToCanvasFast |
| 447 |
sp2 = 5 |
| 448 |
sp4 = 5 |
| 449 |
l = [] |
| 450 |
for i in range(len(seqs[0])): |
| 451 |
val = [seqs[0][i],seqs[1][i]] |
| 452 |
[x,y] = valuetocanvasfast(val,minx,maxx,miny,maxy, |
| 453 |
width,height,left_margin,right_margin,top_margin,bottom_margin) |
| 454 |
l.append( |
| 455 |
[(x - left_margin) / sp2,((height - y) - bottom_margin) / sp4]) |
| 456 |
return l |
| 457 |
|
| 458 |
def canvasToValue(self,val): |
| 459 |
if len(val) != 2: |
| 460 |
raise GrapherError("Illegal value choosen for coordinate transformation. Must be a tuple with 2 elements.") |
| 461 |
x = val[0] |
| 462 |
if x < self.cget("left_margin"): |
| 463 |
x = self.cget("left_margin") |
| 464 |
if x > int(self.cget("realwidth")) - self.cget("right_margin"): |
| 465 |
x = int(self.cget("realwidth")) - self.cget("right_margin") |
| 466 |
y = val[1] |
| 467 |
if y < self.cget("top_margin"): |
| 468 |
y = self.cget("top_margin") |
| 469 |
if y > int(self.cget("realheight")) - self.cget("bottom_margin"): |
| 470 |
y = int(self.cget("realheight")) - self.cget("bottom_margin") |
| 471 |
|
| 472 |
x = x - self.cget("left_margin") |
| 473 |
y = y - self.cget("bottom_margin") |
| 474 |
minx=self.cget("minx") |
| 475 |
maxx=self.cget("maxx") |
| 476 |
miny=self.cget("miny") |
| 477 |
maxy=self.cget("maxy") |
| 478 |
return [minx + (maxx-minx)*float(x)/(float(self.cget("realwidth"))-(self.cget("left_margin")+self.cget("right_margin"))), |
| 479 |
maxy - (maxy-miny)*float(y)/(float(self.cget("realheight"))-(self.cget("bottom_margin")+self.cget("top_margin")))] |
| 480 |
|
| 481 |
class LabeledGrapher(BasicGrapher): |
| 482 |
def __init__(self,parent=None,**kw): |
| 483 |
self.labels=[] |
| 484 |
BasicGrapher.__init__(self,parent,**kw) |
| 485 |
|
| 486 |
def addLabel(self,i,j,input_text,symbol=None): |
| 487 |
new_label={} |
| 488 |
new_label["j"]=j |
| 489 |
xy = [self.data[i]["x"][j], self.data[i]["y"][j]] |
| 490 |
if "z" in self.data[i]: |
| 491 |
xy.append(self.data[i]["z"][j]) |
| 492 |
new_label["xy"]=xy |
| 493 |
new_label["text"]=input_text |
| 494 |
new_label["symbol"]=symbol |
| 495 |
self.labels[i].append(new_label) |
| 496 |
|
| 497 |
def _delData(self,i): |
| 498 |
del self.labels[i] |
| 499 |
BasicGrapher._delData(self,i) |
| 500 |
|
| 501 |
def _delAllData(self): |
| 502 |
self.labels=[] |
| 503 |
BasicGrapher._delAllData(self) |
| 504 |
|
| 505 |
def _addData(self,data,newsect=None,color=None,stable=None): |
| 506 |
self.labels.append([]) |
| 507 |
BasicGrapher._addData(self,data,newsect,color,stable) |
| 508 |
|
| 509 |
def plotlabels(self): |
| 510 |
if self.cget("realwidth") == 1 or self.cget("realheight") == 1: |
| 511 |
return |
| 512 |
|
| 513 |
if not self.cget("use_labels") or len(self.labels) == 0: |
| 514 |
return |
| 515 |
|
| 516 |
trans = self.transform_seq |
| 517 |
if self.cget("smart_label"): |
| 518 |
mp = self.inarrs() |
| 519 |
for d in self.data: |
| 520 |
self.map_curve(mp,trans([d["x"],d["y"]])) |
| 521 |
for i, labels in enumerate(self.labels): |
| 522 |
for label in labels: |
| 523 |
if len(label["text"]) == 0: |
| 524 |
continue |
| 525 |
[x,y] = label["xy"][:2] |
| 526 |
if (x < self["minx"] or x > self["maxx"] or |
| 527 |
y < self["miny"] or y > self["maxy"]): |
| 528 |
continue |
| 529 |
data = self.transform([x,y]) |
| 530 |
if not(data is None): |
| 531 |
[x,y] = data |
| 532 |
if self.cget("smart_label"): |
| 533 |
[xoffd1,yoffd1,xoffd2,yoffd2, |
| 534 |
xofft,yofft,pos] = self.findsp(x,y,mp) |
| 535 |
else: |
| 536 |
[xoffd1,yoffd1,xoffd2,yoffd2, |
| 537 |
xofft,yofft,pos] = self.dumblabel(i,label["j"],x,y) |
| 538 |
anchor = self.getpos(pos) |
| 539 |
y = self.cget("realheight") - y |
| 540 |
self.create_line(x+xoffd1,y-yoffd1,x+xoffd2,y-yoffd2, |
| 541 |
fill=self.cget("foreground")) |
| 542 |
self.create_text(x+xofft,y-yofft,text=label["text"], |
| 543 |
anchor=anchor,fill=self.cget("foreground")) |
| 544 |
|
| 545 |
|
| 546 |
def dumblabel(self,i,j,x,y): |
| 547 |
|
| 548 |
if j < len(self.data[i]["x"]) - 1: |
| 549 |
first = j |
| 550 |
second = j+1 |
| 551 |
else: |
| 552 |
first = j-1 |
| 553 |
second = j |
| 554 |
realwidth=self.cget("realwidth") |
| 555 |
realheight=self.cget("realheight") |
| 556 |
left_margin=self.cget("left_margin") |
| 557 |
top_margin=self.cget("top_margin") |
| 558 |
|
| 559 |
ys = self.data[i]["y"] |
| 560 |
if ys[second] > ys[first]: |
| 561 |
if (x < int(realwidth)-(20+left_margin) and |
| 562 |
y < int(realheight)-(20+top_margin)): |
| 563 |
xoffset = 10 |
| 564 |
yoffset = 10 |
| 565 |
pos=0 |
| 566 |
else: |
| 567 |
xoffset = -10 |
| 568 |
yoffset = -10 |
| 569 |
pos=4 |
| 570 |
else: |
| 571 |
if x > 20+left_margin and y < int(realheight)-(20+top_margin): |
| 572 |
xoffset = -10 |
| 573 |
yoffset = 10 |
| 574 |
pos=2 |
| 575 |
else: |
| 576 |
xoffset = 10 |
| 577 |
yoffset = -10 |
| 578 |
pos=6 |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
return [xoffset//10,yoffset//10,xoffset,yoffset,xoffset,yoffset,pos] |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
def findsp(self,x1,y1,mp): |
| 597 |
sp1 = self.cget("left_margin") |
| 598 |
minsx = sp1 |
| 599 |
maxsx = self.cget("realwidth") - self.cget("right_margin") |
| 600 |
sp2 = 5 |
| 601 |
sp3 = self.cget("bottom_margin") |
| 602 |
minsy = sp3 |
| 603 |
maxsy = self.cget("realheight") - self.cget("top_margin") |
| 604 |
sp4 = 5 |
| 605 |
|
| 606 |
xi = (x1 - sp1) / sp2 |
| 607 |
yi = (y1 - sp3) / sp4 |
| 608 |
pi2 = math.pi * 2.0 |
| 609 |
start = pi2 / 12.0 |
| 610 |
npoint = 16 |
| 611 |
|
| 612 |
hx = len(mp) - 5 |
| 613 |
hy = len(mp[0]) - 5 |
| 614 |
found = False |
| 615 |
for radius in range(3,71): |
| 616 |
npoint = npoint + 8 |
| 617 |
|
| 618 |
for st in [start + (float(i)/npoint)*pi2 for i in range(npoint)]: |
| 619 |
xd = int(xi + radius * math.cos(st)) |
| 620 |
yd = int(yi + radius * math.sin(st)) |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
if xd <= hx and xd >= 3 and yd <= hy and yd >= 3: |
| 626 |
found = True |
| 627 |
for col in mp[xd-2:xd+3]: |
| 628 |
if max(col[yd-2:yd+3]) == 1: |
| 629 |
found = False |
| 630 |
break |
| 631 |
if found: |
| 632 |
r1 = (radius + 3) * sp2 |
| 633 |
ix = x1 + r1 * math.cos(st) |
| 634 |
iy = y1 + r1 * math.sin(st) |
| 635 |
if ix>=minsx and ix<=maxsx and iy>=minsy and iy<=maxsy: |
| 636 |
break |
| 637 |
found = False |
| 638 |
if found: |
| 639 |
break |
| 640 |
|
| 641 |
if not found: |
| 642 |
return [1,1,10,10,11,11,4] |
| 643 |
|
| 644 |
for col in mp[xd-2:xd+3]: |
| 645 |
for i2 in range(yd-1,yd+2): |
| 646 |
col[i2] = 1 |
| 647 |
|
| 648 |
pos = int(st/pi2*8.0 - 0.5) |
| 649 |
radius = radius * sp2 |
| 650 |
cosst = math.cos(st) |
| 651 |
sinst = math.sin(st) |
| 652 |
d1dist = self.cget("line_width") + 1 |
| 653 |
xoffd1 = d1dist * cosst |
| 654 |
yoffd1 = d1dist * sinst |
| 655 |
xoffd2 = radius * cosst |
| 656 |
yoffd2 = radius * sinst |
| 657 |
xofft = (radius + 2) * cosst |
| 658 |
yofft = (radius + 2) * sinst |
| 659 |
return [xoffd1,yoffd1,xoffd2,yoffd2,xofft,yofft,pos] |
| 660 |
|
| 661 |
def getpos(self,pos): |
| 662 |
anchor = [ "sw", "s", "se", "e", "ne", "n", "nw", "w", "sw" ] |
| 663 |
return anchor[pos] |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
def inarrs(self): |
| 670 |
sp1 = self.cget("left_margin") |
| 671 |
sp2 = 5 |
| 672 |
sp3 = self.cget("bottom_margin") |
| 673 |
sp4 = 5 |
| 674 |
nx = int(self.cget("realwidth")-sp1-self.cget("right_margin"))//sp2 |
| 675 |
ny = int(self.cget("realheight")-sp3-self.cget("top_margin"))//sp4 |
| 676 |
r = ny*[0] |
| 677 |
return [r[:] for i in range(nx)] |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
def map_curve(self,mp,xys): |
| 683 |
ixmax = len(mp) |
| 684 |
iymax = len(mp[0]) |
| 685 |
[x2,y2] = xys[0] |
| 686 |
ix2,iy2 = int(x2),int(y2) |
| 687 |
for xy in xys[1:]: |
| 688 |
x1,y1 = x2,y2 |
| 689 |
ix1,iy1 = ix2,iy2 |
| 690 |
[x2,y2] = xy |
| 691 |
ix2,iy2 = int(x2),int(y2) |
| 692 |
if ix1 == ix2 and iy1 == iy2: |
| 693 |
|
| 694 |
if ix1 >= 0 and ix1 < ixmax and iy1 >= 0 and iy1 < iymax: |
| 695 |
mp[ix1][iy1] = 1 |
| 696 |
continue |
| 697 |
|
| 698 |
|
| 699 |
if ((ix1 < 0 and ix2 < 0) or (iy1 < 0 and iy2 < 0) or |
| 700 |
(ix1 >= ixmax and ix2 >= ixmax) or |
| 701 |
(iy1 >= iymax and iy2 >= iymax)): |
| 702 |
continue |
| 703 |
dx = x2 - x1 |
| 704 |
dy = y2 - y1 |
| 705 |
index = int(max(abs(dx),abs(dy))) + 1 |
| 706 |
ilow = 0 |
| 707 |
ihigh = index |
| 708 |
if dx != 0: |
| 709 |
xlim1 = -x1*index/dx |
| 710 |
xlim2 = (ixmax-x1)*index/dx+1 |
| 711 |
ilow = max(ilow,min(xlim1,xlim2)) |
| 712 |
ihigh = min(ihigh,max(xlim1,xlim2)) |
| 713 |
if dy != 0: |
| 714 |
ylim1 = -y1*index/dy |
| 715 |
ylim2 = (iymax-y1)*index/dy+1 |
| 716 |
ilow = max(ilow,min(ylim1,ylim2)) |
| 717 |
ihigh = min(ihigh,max(ylim1,ylim2)) |
| 718 |
for i in range(int(ilow),int(ihigh)): |
| 719 |
f = float(i)/index |
| 720 |
ix = int(x1 + f * dx) |
| 721 |
iy = int(y1 + f * dy) |
| 722 |
try: |
| 723 |
mp[ix][iy] = 1 |
| 724 |
except IndexError: |
| 725 |
pass |
| 726 |
|
| 727 |
def draw(self): |
| 728 |
self.plotlabels() |
| 729 |
BasicGrapher.draw(self) |
| 730 |
self.plotsymbols() |
| 731 |
|
| 732 |
def plotsymbols(self): |
| 733 |
if not self.cget("use_symbols"): |
| 734 |
return |
| 735 |
|
| 736 |
for i, labels in enumerate(self.labels): |
| 737 |
for label in labels: |
| 738 |
l = label["symbol"] |
| 739 |
if l is None: |
| 740 |
continue |
| 741 |
data = self.valueToCanvas(label["xy"][:2]) |
| 742 |
if data is None: |
| 743 |
continue |
| 744 |
[x,y] = data |
| 745 |
if (x < self["minx"] or x > self["maxx"] or |
| 746 |
y < self["miny"] or y > self["maxy"]): |
| 747 |
continue |
| 748 |
c = self.cget("symbol_color") |
| 749 |
if len(l) <= 3: |
| 750 |
self.create_text(x,y,font=self.cget("symbol_font"), |
| 751 |
fill=c,text=l) |
| 752 |
continue |
| 753 |
ms = 3 |
| 754 |
for i, ch in enumerate(l): |
| 755 |
if ch in string.digits: |
| 756 |
ms = int(l[i:]) |
| 757 |
l = l[:i] |
| 758 |
break |
| 759 |
if l == "fillcircle": |
| 760 |
self.create_oval(x-ms,y-ms,x+ms,y+ms,fill=c,outline=c) |
| 761 |
elif l == "circle": |
| 762 |
self.create_oval(x-ms,y-ms,x+ms,y+ms,outline=c) |
| 763 |
elif l == "square": |
| 764 |
self.create_rectangle(x-ms,y-ms,x+ms,y+ms,outline=c) |
| 765 |
elif l == "crosssquare": |
| 766 |
self.create_rectangle(x-ms,y-ms,x+ms,y+ms,outline=c) |
| 767 |
self.create_line(x-ms,y-ms,x+ms,y+ms,fill=c) |
| 768 |
self.create_line(x-ms,y+ms,x+ms,y-ms,fill=c) |
| 769 |
elif l == "fillsquare": |
| 770 |
self.create_rectangle(x-ms,y-ms,x+ms,y+ms,fill=c,outline=c) |
| 771 |
|
| 772 |
|
| 773 |
class InteractiveGrapher(LabeledGrapher): |
| 774 |
def __init__(self,parent=None,**kw): |
| 775 |
LabeledGrapher.__init__(self,parent,**kw) |
| 776 |
|
| 777 |
def unzoom(self): |
| 778 |
self.computeXRange() |
| 779 |
self.computeYRange() |
| 780 |
self.clear() |
| 781 |
self.draw() |
| 782 |
|
| 783 |
def labelPointWrapper(self,e): |
| 784 |
|
| 785 |
points = self.find_closest(e.x,e.y) |
| 786 |
|
| 787 |
point=None |
| 788 |
curve=None |
| 789 |
for point in points: |
| 790 |
|
| 791 |
point_tags = self.gettags(point) |
| 792 |
for tag in point_tags: |
| 793 |
tag=tag.split(":") |
| 794 |
if tag[0] == "data_point": |
| 795 |
point=int(tag[1]) |
| 796 |
if tag[0] == "curve": |
| 797 |
curve=int(tag[1]) |
| 798 |
if not((point is None) or (curve is None)): |
| 799 |
label = tkSimpleDialog.askstring("Label","Enter here") |
| 800 |
self.addLabel(curve,point,label) |
| 801 |
|
| 802 |
def printTagWrapper(self,e): |
| 803 |
theid=self.find("closest",e.x,e.y) |
| 804 |
print(self.gettags(theid[0])) |
| 805 |
|
| 806 |
def printValueWrapper(self,e): |
| 807 |
self.__printValue((e.x,e.y)) |
| 808 |
|
| 809 |
def __printValue(self,val): |
| 810 |
print(self.canvasToValue(val)) |
| 811 |
|
| 812 |
def zoomRubberBand(self,e): |
| 813 |
self.delete("rubber_band") |
| 814 |
self.create_polygon(e.x,e.y,e.x,self.zoom_starty, |
| 815 |
self.zoom_startx,self.zoom_starty, |
| 816 |
self.zoom_startx,e.y, |
| 817 |
tags=("rubber_band",),fill="",outline="grey") |
| 818 |
|
| 819 |
def zoomWrapperStart(self,e): |
| 820 |
self.zoom_startx = e.x |
| 821 |
self.zoom_starty = e.y |
| 822 |
|
| 823 |
def zoomWrapperEnd(self,e): |
| 824 |
tolerance = 5 |
| 825 |
|
| 826 |
cx=[self.zoom_startx,e.x] |
| 827 |
cy=[self.zoom_starty,e.y] |
| 828 |
cx.sort() |
| 829 |
cy.sort() |
| 830 |
|
| 831 |
if cx[1] - cx[0] > tolerance and cy[1] - cy[0] > tolerance: |
| 832 |
vx=[0,0] |
| 833 |
vy=[0,0] |
| 834 |
|
| 835 |
vx[0],vy[0] = self.canvasToValue((cx[0],cy[1])) |
| 836 |
vx[1],vy[1] = self.canvasToValue((cx[1],cy[0])) |
| 837 |
|
| 838 |
self.computeXRange(vx[0],vx[1]) |
| 839 |
self.computeYRange(vy[0],vy[1]) |
| 840 |
self.clear() |
| 841 |
self.draw() |
| 842 |
|
| 843 |
|
| 844 |
def drawWrapper(self,e): |
| 845 |
|
| 846 |
|
| 847 |
self._configNoDraw(realwidth=e.width,realheight=e.height) |
| 848 |
self.configure() |
| 849 |
self.clear() |
| 850 |
self.draw() |
| 851 |
|
| 852 |
class GUIGrapher(InteractiveGrapher): |
| 853 |
def __init__(self,parent=None,**kw): |
| 854 |
InteractiveGrapher.__init__(self,parent,**kw) |
| 855 |
if not hasattr(self, "tk"): |
| 856 |
return |
| 857 |
self.bind("<ButtonPress-3>",self.popupMenuWrapper) |
| 858 |
self.menu=Tkinter.Menu() |
| 859 |
self.menu.add_radiobutton(label="print value",command=self.printValueBindings) |
| 860 |
|
| 861 |
|
| 862 |
self.menu.add_radiobutton(label="zoom",command=self.zoomBindings) |
| 863 |
self.menu.invoke('zoom') |
| 864 |
self.menu.add_command(label="Unzoom",command=self.unzoom) |
| 865 |
self.menu.add_command(label="Postscript",command=self.generatePostscript) |
| 866 |
self.menu.add_command(label="Configure...",command=self.__interactiveConfigureDialog) |
| 867 |
self.bind("<Configure>",self.drawWrapper) |
| 868 |
|
| 869 |
def __interactiveConfigureDialog(self): |
| 870 |
diag = Pmw.Dialog(self,buttons=("Ok","Cancel")) |
| 871 |
options = sorted([self.configure(key)[0] for key in self.configure() |
| 872 |
if self._isInternalOption(key)]) |
| 873 |
|
| 874 |
self.optionList = Pmw.ScrolledListBox(diag.interior(), |
| 875 |
items=options, |
| 876 |
dblclickcommand=self.__updateInteractiveConfigureDialog) |
| 877 |
self.optionList.pack(side="left") |
| 878 |
|
| 879 |
frame = Tkinter.Frame(diag.interior()) |
| 880 |
frame.pack(side="right") |
| 881 |
self.optionLabel = Pmw.EntryField(frame, |
| 882 |
labelpos="w", |
| 883 |
label_text="Option Name", |
| 884 |
entry_state=Tkinter.DISABLED) |
| 885 |
self.optionLabel.pack(side="top") |
| 886 |
|
| 887 |
self.valueLabel = Pmw.EntryField(frame, |
| 888 |
labelpos="w", |
| 889 |
label_text="Old Value", |
| 890 |
entry_state=Tkinter.DISABLED) |
| 891 |
self.valueLabel.pack(side="top") |
| 892 |
|
| 893 |
self.valueEntry = Pmw.EntryField(frame, |
| 894 |
labelpos="w", |
| 895 |
label_text="New Value", |
| 896 |
command=self.__modifyOption) |
| 897 |
self.valueEntry.pack(side="top") |
| 898 |
|
| 899 |
def __modifyOption(self): |
| 900 |
key = self.optionList.getcurselection()[0] |
| 901 |
if isinstance(self.cget(key), int): |
| 902 |
self[key] = int(self.valueEntry.get()) |
| 903 |
elif isinstance(self.cget(key), float): |
| 904 |
self[key] = float(self.valueEntry.get()) |
| 905 |
elif isinstance(self.cget(key), str): |
| 906 |
self[key] = self.valueEntry.get() |
| 907 |
self.valueLabel.setentry(self.cget(key)) |
| 908 |
|
| 909 |
def __updateInteractiveConfigureDialog(self): |
| 910 |
key = self.optionList.getcurselection()[0] |
| 911 |
self.optionLabel.setentry(key) |
| 912 |
self.valueLabel.setentry(self.cget(key)) |
| 913 |
self.valueEntry.clear() |
| 914 |
if isinstance(self.cget(key), int): |
| 915 |
self.valueEntry.configure(validate={"validator":"integer"}) |
| 916 |
elif isinstance(self.cget(key), float): |
| 917 |
self.valueEntry.configure(validate={"validator":"real"}) |
| 918 |
elif isinstance(self.cget(key), str): |
| 919 |
self.valueEntry.configure(validate={"validator":"alphanumeric"}) |
| 920 |
|
| 921 |
def generatePostscript(self,filename=None,pscolormode=None): |
| 922 |
""" |
| 923 |
Save the plot as postscript. |
| 924 |
""" |
| 925 |
if pscolormode is None: |
| 926 |
pscolormode=self.cget("ps_colormode") |
| 927 |
if filename is None: |
| 928 |
filename = tkFileDialog.asksaveasfilename(defaultextension=".eps",title="Save as Postscript File") |
| 929 |
self.update() |
| 930 |
self.postscript(file=filename,colormode=pscolormode) |
| 931 |
|
| 932 |
savefig = generatePostscript |
| 933 |
|
| 934 |
def printValueBindings(self): |
| 935 |
self.bind("<ButtonPress-1>",self.printValueWrapper) |
| 936 |
self.unbind("<B1-Motion>") |
| 937 |
self.unbind("<ButtonRelease-1>") |
| 938 |
|
| 939 |
def printTagBindings(self): |
| 940 |
self.bind("<ButtonPress-1>",self.printTagWrapper) |
| 941 |
self.unbind("<B1-Motion>") |
| 942 |
self.unbind("<ButtonRelease-1>") |
| 943 |
|
| 944 |
def labelPointBindings(self): |
| 945 |
self.bind("<ButtonPress-1>",self.labelPointWrapper) |
| 946 |
self.unbind("<B1-Motion>") |
| 947 |
self.unbind("<ButtonRelease-1>") |
| 948 |
|
| 949 |
def zoomBindings(self): |
| 950 |
self.bind("<ButtonPress-1>",self.zoomWrapperStart) |
| 951 |
self.bind("<B1-Motion>",self.zoomRubberBand) |
| 952 |
self.bind("<ButtonRelease-1>",self.zoomWrapperEnd) |
| 953 |
|
| 954 |
def popupMenuWrapper(self,e): |
| 955 |
self.menu.tk_popup(e.x+self.winfo_rootx(),e.y+self.winfo_rooty()) |
| 956 |
|
| 957 |
|
| 958 |
def test(grapher=None): |
| 959 |
if grapher is None: |
| 960 |
grapher = GUIGrapher() |
| 961 |
data = [float(i)*0.1 for i in range(62)] |
| 962 |
|
| 963 |
grapher.addArray((data,list(map(math.sin,data)))) |
| 964 |
grapher.addArray((data,list(map(math.cos,data)))) |
| 965 |
grapher.addLabel(0,10,"hello") |
| 966 |
grapher.addLabel(0,30,"world") |
| 967 |
grapher.pack() |
| 968 |
grapher.plot() |
| 969 |
|
| 970 |
button = Tkinter.Button(text="Quit",command=grapher.quit) |
| 971 |
button.pack() |
| 972 |
button.update() |
| 973 |
print("Press <return> to continue") |
| 974 |
raw_input() |
| 975 |
|
| 976 |
grapher.delAllData() |
| 977 |
grapher.addArray((data,list(map(math.cos,data)))) |
| 978 |
grapher.plot() |
| 979 |
print("Press <return> to continue") |
| 980 |
raw_input() |
| 981 |
|
| 982 |
if __name__=='__main__': |
| 983 |
test() |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|