-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotinteractive1d.pro
368 lines (350 loc) · 16.3 KB
/
plotinteractive1d.pro
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
; ***************************************************************************
; Routines to create a 1D interactive plot (y vs. index)
;
; AUTHOR:
; S. Merkel, CNRS - Universite Lille 1, http://merkel.ZoneO.net/
;
; CALLING SEQUENCE:
; calplotinteractive1D, base, xdata, ydata, title = title, xlabel = xlabel, ylabel = ylabel, legend = legend
;
; INPUTS:
; base: parent frame
; xdata: 1-D array with x-data
; ydata: array of numbers, data to plot
; title: optional parameter to set a plot title
; xlabel: optional parameter to set a label for the x-axis
; ylabel: optional parameter to set a label for the y-axis
; legend: optional parameter to set a legend label for the datasets
;
; If you want to plot multiple datasets, set ydata with an array of numbers as ydata(i,j)
; where i is the set number
; Legends should be an array of string with legend[i] = legend for dataset i
; HISTORY
; 11/2006: Creation of the framework
; ***************************************************************************
; ***************************************************************************
; export and print
; ***************************************************************************
; export to GIF function
pro plotinteractive1D_exportgif, event
common files, extension, datadirectory, outputdirectory, defaultdirectory, jcpdsdirectory
widget_control, event.top, get_uvalue=pstate
; fix the extension
filters = [['*.gif'], ['GIF']]
; pick a filename
filename = DIALOG_PICKFILE(dialog_parent = (*pstate).tlb , TITLE='Save graphics as...', $
path=defaultdirectory, get_path = newdefaultdir, filter=filters, default_extension='.gif', /write, /OVERWRITE_PROMPT);
; if OK was pressed
if (filename ne '') then begin
; export the content of the active window to gif
write_gif, filename, TVRD()
; set the nez default path
defaultdirectory = newdefaultdir
endif
end
; export to JPEG unction
pro plotinteractive1D_exportjpg, event
common files, extension, datadirectory, outputdirectory, defaultdirectory, jcpdsdirectory
widget_control, event.top, get_uvalue=pstate
; fix the extension
filters = [['*.jpg;*.jpeg'], ['JPEG']]
; pick a filename
filename = DIALOG_PICKFILE(dialog_parent = (*pstate).tlb, TITLE='Save graphics as...', path=defaultdirectory, $
get_path = newdefaultdir, filter=filters, default_extension='.jpg', /write, /OVERWRITE_PROMPT);
; if OK was pressed
if (filename ne '') then begin
; export the content of the active window to jpeg
write_jpeg, filename, TVRD()
; set the new default path
defaultdirectory = newdefaultdir
endif
end
; export to postscript
pro plotinteractive1D_exportps, event
common files, extension, datadirectory, outputdirectory, defaultdirectory, jcpdsdirectory
widget_control, event.top, get_uvalue=pstate
; get the data in the window
widget_control, event.top, get_uvalue=pstate
; fix the extension
filters = [['*.ps'], ['PS']]
; pick a filename
filename = DIALOG_PICKFILE(dialog_parent = (*pstate).tlb, TITLE='Save graphics as...', path=defaultdirectory, $
get_path = newdefaultdir, filter=filters, default_extension='.ps', /write, /OVERWRITE_PROMPT);
; if OK was pressed
if (filename ne '') then begin
; save current devide, set the device to postscript
mydevice = !D.NAME
set_plot, 'PS'
device, filename = filename, /PORTRAIT, xsize = 15, ysize = 10, xoffset = 2.5, yoffset = 10, /color, bits_per_pixel=24
; replot the data in the postscript device
plotinteractive1D_doplot, pstate, postscript=1
; close postscript devide, return to the old one
device, /CLOSE
set_plot, mydevice
; set the new default path
defaultdirectory = newdefaultdir
endif
end
; export to in chiplot unction
pro plotinteractive1D_exportchi, event
common files, extension, datadirectory, outputdirectory, defaultdirectory, jcpdsdirectory
widget_control, event.top, get_uvalue=pstate
; state = {xdata: xdata, ydata:ydata, ncolumns: ncolumns, tlb: tlb, w_id:w_id, draw:draw, status:status, xlabel:xlabel, ylabel:ylabel, title: title, xmin:xmin, xmax:xmax, ymin:ymin, ymax:ymax, sc_xmin:0.0, sc_xmax:0.0, sc_ymin:0.0, sc_ymax:0.0, scaling:0, plotlegend: plotlegend, legend: legend}
if ((*pstate).ncolumns gt 1) then Result = DIALOG_MESSAGE( "You have several datasets. Only the first will be saved.", DIALOG_PARENT=(*pstate).tlb, /INFORMATION, TITLE="Multifit warning")
; fix the extension
filters = [['*.chi'], ['CHI']]
; pick a filename
filename = DIALOG_PICKFILE(dialog_parent = (*pstate).tlb, filter=filters, TITLE='Save dataset as...', $
path=defaultdir, get_path = newdefaultdir, filter=filters, default_extension='.chi', /write, /OVERWRITE_PROMPT);
; if OK was pressed
if (filename ne '') then begin
; export the content of the active window to chiplot
openw, lun, filename, /get_lun
printf, lun, "Chi file created by Multifit"
printf, lun, (*pstate).xlabel
printf, lun, (*pstate).ylabel
printf, lun, n_elements((*pstate).xdata)
for i=0,n_elements((*pstate).xdata)-1 do printf, lun, (*pstate).xdata[i], (*pstate).ydata[0,i]
free_lun, lun
; set the new default path
defaultdir = newdefaultdir
endif
end
; ***************************************************************************
; handling of mouse events
; ***************************************************************************
; handle mouse events in the plot window
pro plotinteractive1D_draw, event
widget_control, event.top, get_uvalue=pstate
if (event.type eq 0) then begin ; mouse is pressed
if (event.clicks eq 2) then begin
; double-click: we autoscale the data
(*pstate).scaling = 0
(*pstate).xmin = min((*pstate).xdata)
(*pstate).xmax = max((*pstate).xdata)
(*pstate).ymin = min((*pstate).ydata)
(*pstate).ymax = max((*pstate).ydata)
plotinteractive1D_doplot, pstate
endif else begin
; single click, scaling according to mouse, setting starting value and activating rectagle draw
datac = convert_coord(event.x, event.y, /device, /to_data, /double)
(*pstate).sc_xmin = datac[0]
(*pstate).sc_ymin = datac[1]
(*pstate).scaling = 1
endelse
endif else if (event.type eq 1) then begin ; mouse is released
; if we were scaling, end of scaling according to mouse
if ((*pstate).scaling eq 1) then begin
datac = convert_coord(event.x, event.y, /device, /to_data, /double)
(*pstate).xmin = (*pstate).sc_xmin
(*pstate).ymin = (*pstate).sc_ymin
(*pstate).xmax = datac[0]
(*pstate).ymax = datac[1]
(*pstate).scaling = 0
plotinteractive1D_doplot, pstate
endif
endif else if (event.type eq 2) then begin ; mouse is moved
if ((*pstate).scaling eq 1) then begin
; if scaling according to mouse, drawing the rectangle
datac = convert_coord(event.x, event.y, /device, /to_data, /double)
(*pstate).sc_xmax = datac[0]
(*pstate).sc_ymax = datac[1]
plotinteractive1D_doplot, pstate
endif else begin
; otherwise convert the data to a string and set it in the status variable of the widget
datac = convert_coord(event.x, event.y, /device, /to_data, /double)
statusstr = strtrim(datac[0],2) + ',' + strtrim(datac[1],2)
widget_control, (*pstate).status, set_value=statusstr
endelse
endif
end
; ***************************************************************************
; replot the data with or without scaling rectangle
; ***************************************************************************
; plot the data, send 'pstate', postscript = 1 if you are saving to a postscript file
pro plotinteractive1D_doplot, pstate, postscript=postscript
IF N_Elements(postscript) EQ 0 THEN postscript = 0
; find and fix plotting range
xmin = min([(*pstate).xmin,(*pstate).xmax])
xmax = max([(*pstate).xmin,(*pstate).xmax])
ymin = min([(*pstate).ymin,(*pstate).ymax])
ymax = max([(*pstate).ymin,(*pstate).ymax])
if (postscript eq 0) then begin ; plotting to screen
; ensure that data are being plotted in the draw window
; print, "Getting ready to plot"
wset, (*pstate).w_id
; plot data
if ((*pstate).nav eq 0) then begin ; we plot everything
plot, (*pstate).xdata, (*pstate).ydata[0,*], xrange = [xmin,xmax], yrange=[ymin,ymax], background=255, color=0, xtitle=(*pstate).xlabel, ytitle=(*pstate).ylabel, title=(*pstate).title, ystyle = 1, xstyle=1
if ((*pstate).ncolumns gt 1) then for i=1, (*pstate).ncolumns-1 do oplot, (*pstate).xdata, (*pstate).ydata[i,*], color=10*i
endif else begin ; we only plot one dataset (the "current" one)
plot, (*pstate).xdata, (*pstate).ydata[(*pstate).current,*], xrange = [xmin,xmax], yrange=[ymin,ymax], background=255, color=0, xtitle=(*pstate).xlabel, ytitle=(*pstate).ylabel, title=(*pstate).title, ystyle = 1, xstyle=1
endelse
; if we are scaling, plot a red rectangle
if ((*pstate).scaling eq 1) then begin
oplot, [(*pstate).sc_xmin,(*pstate).sc_xmax,(*pstate).sc_xmax,(*pstate).sc_xmin,(*pstate).sc_xmin], [(*pstate).sc_ymin,(*pstate).sc_ymin,(*pstate).sc_ymax,(*pstate).sc_ymax,(*pstate).sc_ymin], color=10
endif
; add the legend, if necessary
if ((*pstate).plotlegend) then begin
x1 = xmax-0.3*(xmax-xmin)
x2 = xmax-0.25*(xmax-xmin)
x3 = xmax-0.3*(xmax-xmin)
y1 = ymax-0.05*(ymax-ymin)
oplot, [x1,x2], [y1,y1], color=0
if ((*pstate).nav eq 0) then begin ; we add all legends
xyouts, x3, y1, (*pstate).legend[0], color=0
if ((*pstate).ncolumns gt 1) then begin
for i=1, (*pstate).ncolumns-1 do begin
y1 = y1 - 0.05*(ymax-ymin)
oplot, [x1,x2], [y1,y1], color=10*i
xyouts, x3, y1, (*pstate).legend[i], color=10*i
endfor
endif
endif else begin ; we only add the right legend
xyouts, x3, y1, (*pstate).legend[(*pstate).current], color=0
endelse
endif
endif else begin ; postscript, simply plot the data
plot, (*pstate).xdata, (*pstate).ydata[0,*], xrange = [xmin,xmax], yrange=[ymin,ymax], background=255, color=0, xtitle=(*pstate).xlabel, ytitle=(*pstate).ylabel, title=(*pstate).title, xthick=2, ythick=2, thick=2, ystyle = 1, xstyle=1
if ((*pstate).ncolumns gt 1) then for i=1, (*pstate).ncolumns-1 do oplot, (*pstate).xdata, (*pstate).ydata[i,*], color=10*i
; add the legend, if necessary
if ((*pstate).plotlegend) then begin
x1 = xmax-0.2*(xmax-xmin)
x2 = xmax-0.15*(xmax-xmin)
x3 = xmax-0.13*(xmax-xmin)
y1 = ymax-0.05*(ymax-ymin)
oplot, [x1,x2], [y1,y1], color=0
xyouts, x3, y1, (*pstate).legend[0], color=0
if ((*pstate).ncolumns gt 1) then for i=1, (*pstate).ncolumns-1 do begin
y1 = y1 - 0.05*(ymax-ymin)
oplot, [x1,x2], [y1,y1], color=10*i
xyouts, x3, y1, (*pstate).legend[i], color=10*i
endfor
endif
endelse
end
; ***************************************************************************
; resize event
; ***************************************************************************
; handle resize of window events (resize the plot)
pro plotinteractive1D_event, event
; get the pstate pointer
widget_control, event.top, get_uvalue=pstate
WIDGET_CONTROL, event.ID, GET_UVALUE=uval
if (TYPENAME(uval) eq 'STRING') then begin
; print, TYPENAME(uval)
CASE uval OF
'PREVIOUSDATASET': BEGIN
;print, "Previous"
if ((*pstate).current gt 0) then (*pstate).current -= 1
plotinteractive1D_doplot, pstate
;movebackActiveSet, stash.log, stash.listsets
;updateRangeLabels, stash
;plotDataContour, ev.TOP, plotmin, plotmax, thetamin, thetamax, azmin, azmax
END
'NEXTDATASET': BEGIN
;print, "Next", (*pstate).current, (*pstate).ncolumns
if ((*pstate).current lt ((*pstate).ncolumns-2)) then (*pstate).current += 1
plotinteractive1D_doplot, pstate
;advanceActiveSet, stash.log, stash.listsets
;updateRangeLabels, stash
;plotDataContour, ev.TOP, plotmin, plotmax, thetamin, thetamax, azmin, azmax
END
ELSE:
ENDCASE
endif else begin ; it is a resize event
; getting size available for the plot
statusg = widget_info((*pstate).status, /geometry)
tlbg = widget_info(event.top, /geometry)
newx = event.x - 2*tlbg.xpad
newy = event.y - statusg.scr_ysize - 2*tlbg.ypad - 2*tlbg.space
; setting a new size
widget_control, (*pstate).draw, xsize=newx, ysize=newy
; replot
plotinteractive1D_doplot, pstate
endelse
END
; ***************************************************************************
; cleaning up
; ***************************************************************************
; if we come from an event (from the menu)
pro plotinteractive1D_cleanupmenu,event
widget_control, event.top, get_uvalue=pstate
IF Widget_Info((*pstate).tlb, /Valid_ID) THEN Widget_Control, (*pstate).tlb, /Destroy
ptr_free, pstate
end
; if we come from a window (window has been shut, by the user or the application)
pro plotinteractive1D_cleanup, tlb
widget_control, tlb, get_uvalue = pstate
IF Widget_Info((*pstate).tlb, /Valid_ID) THEN Widget_Control, (*pstate).tlb, /Destroy
ptr_free, pstate
end
; ***************************************************************************
; setting up
; ***************************************************************************
pro plotinteractive1D, base, xdata, sendydata, title = title, xlabel = xlabel, ylabel = ylabel, legend = legend, range = range, nav = nav
IF N_Elements(title) EQ 0 THEN title = "Plot"
IF N_Elements(xlabel) EQ 0 THEN xlabel = ""
IF N_Elements(ylabel) EQ 0 THEN ylabel = ""
IF N_Elements(nav) EQ 0 THEN nav = 0
IF N_Elements(legend) EQ 0 THEN BEGIN
plotlegend = 0
legend = ''
endif else plotlegend = 1
IF N_Elements(range) EQ 0 THEN begin
xmin = min(xdata)
xmax = max(xdata)
endif else begin
xmin = range[0]
xmax = range[1]
endelse
; converting ydata to 2D array if it is not already
if (N_ELEMENTS(SIZE(sendydata, /DIMENSION)) eq 1) then begin
ydata = fltarr(1,N_ELEMENTS(sendydata))
ydata[0,*] = sendydata
ncolumns = 1
endif else begin
ydata = sendydata
ncolumns = (SIZE(ydata, /DIMENSION))[0]
endelse
; main window
tlb = widget_base(title = title, /column, /tlb_size_events, MBAR=bar, GROUP_LEADER=base)
; menu bar
file_menu = WIDGET_BUTTON(bar, VALUE='File', /MENU)
file_bttn1 = WIDGET_BUTTON(file_menu, VALUE='Export plot to GIF', event_pro = 'plotinteractive1D_exportgif' )
file_bttn2 = WIDGET_BUTTON(file_menu, VALUE='Export plot to JPEG', event_pro = 'plotinteractive1D_exportjpg' )
file_bttn3 = WIDGET_BUTTON(file_menu, VALUE='Export plot to PS', event_pro = 'plotinteractive1D_exportps' )
file_bttn4 = WIDGET_BUTTON(file_menu, VALUE='Export data in chi format', event_pro ='plotinteractive1D_exportchi', /SEPARATOR)
file_bttn4 = WIDGET_BUTTON(file_menu, VALUE='Close window', event_pro ='plotinteractive1D_cleanupmenu', /SEPARATOR)
; navigaton between sets
if (nav eq 1) then begin
buttons2 = WIDGET_BASE(tlb,/ALIGN_CENTER, /ROW, /GRID_LAYOUT)
previous = WIDGET_BUTTON(buttons2, VALUE='Previous set', UVALUE='PREVIOUSDATASET')
next = WIDGET_BUTTON(buttons2, VALUE='Next set', UVALUE='NEXTDATASET')
endif
; other
status = widget_label(tlb, value=' ', /dynamic_resize)
draw = widget_draw(tlb, xsize=500, ysize=300, /motion_events, /button_events, event_pro='plotinteractive1D_draw')
; build the UI
Widget_Control, tlb, /Realize
; get important information to communicate in the application
Widget_Control, draw, get_value=w_id
ymin = min(ydata,/NAN)
ymax = max(ydata,/NAN)
current = 0
state = {xdata: xdata, ydata:ydata, ncolumns: ncolumns, tlb: tlb, w_id:w_id, draw:draw, status:status, xlabel:xlabel, ylabel:ylabel, title: title, xmin:xmin, xmax:xmax, ymin:ymin, ymax:ymax, sc_xmin:0.0, sc_xmax:0.0, sc_ymin:0.0, sc_ymax:0.0, scaling:0, plotlegend: plotlegend, legend: legend, nav:nav, current:current}
; create a pointer to the state structure and put that pointer
; into the user value of the top-level base
pstate = ptr_new(state,/no_copy)
widget_control, tlb, set_uvalue=pstate
widget_control, draw, set_uvalue=pstate
widget_control, file_bttn1, set_uvalue=pstate
widget_control, file_bttn2, set_uvalue=pstate
widget_control, file_bttn3, set_uvalue=pstate
widget_control, file_bttn4, set_uvalue=pstate
; plot the data
plotinteractive1D_doplot, pstate
; Register with XMANAGER so you can receive events.
Widget_Control, tlb, Kill_Notify='plotinteractive1D_cleanup'
xmanager, 'plotinteractive1D', tlb, event_handler='plotinteractive1D_event', cleanup='plotinteractive1D_cleanup'
end