Debian Codegraph @

src/legends.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2018 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! module containing routines for plotting legends in PGPLOT
25 ! subroutines:
26 ! legend : plots time on plots
27 ! legend_vec : plots legend with vector arrow
28 ! legend_markers : plots different particle types
29 ! legend_scale : plots a scale on co-ordinate plots
30 !-----------------------------------------------------------------
31 module legends
32 implicit none
33
34 public :: legend, legend_vec, legend_markers, legend_scale
35 public :: prompt_panelselect, ipanelselect
36 public :: in_legend
37
38 ! store bounding box of each legend in global variables
39 type box
40 real :: x1,x2,y1,y2
41 end type box
42
43 integer, parameter :: nlegend_types = 4
44 integer, parameter, public :: &
45 ilegend = 1, &
46 ilegend_vec = 2, &
47 ilegend_markers = 3, &
48 ilegend_scale = 4
49
50 type(box) :: bbox(nlegend_types)
51
52 private
53
54 contains
55
56 !-----------------------------------------------------------------
57 ! plots time on plot
58 ! arguments:
59 ! t : current time
60 ! hpos : horizontal position as fraction of viewport
61 ! vpos : vertical position in character heights from top
62 !-----------------------------------------------------------------
63
64 subroutine legend(legendtext,t,nvar,allvars,tags,unitslabel,hpos,vpos,fjust,usebox)
65 use plotlib, only:plot_annotate
66 use asciiutils, only:string_replace
67 use parsetext, only:parse_text,rn
68 use params, only:ltag
69 real, intent(in) :: t,hpos,vpos,fjust
70 integer, intent(in) :: nvar
71 real, intent(in), dimension(nvar) :: allvars
72 character(len=*), dimension(nvar), intent(in) :: tags
73 character(len=*), intent(in) :: legendtext,unitslabel
74 logical, intent(in) :: usebox
75 character(len=len(legendtext)+len(unitslabel)+20) :: label
76 real(kind=rn), dimension(nvar+1) :: vals
77 character(len=ltag), dimension(nvar+1) :: vars
78
79 label = trim(legendtext)
80 !
81 ! if string does not contain any formatting
82 ! append the time variable to it
83 !
84 if (index(label,'%') <= 0) then
85 if (t < 1.) then
86 label = trim(label)//'%t.2'
87 else
88 label = trim(label)//'%t.3'
89 endif
90 endif
91 !
92 ! parse string for functions of time and formatting
93 ! i.e. %t.5
94 !
95 vars(1) = 't'
96 vals(1) = real(t,kind=rn)
97 if (nvar > 1) then
98 vars(2:1+nvar) = tags(1:nvar)
99 vals(2:1+nvar) = allvars(1:nvar)
100 endif
101 call parse_text(label,vars,vals)
102
103 if (index(label,'%ut') > 0) then
104 call string_replace(label,'%ut',trim(unitslabel))
105 elseif (index(legendtext,'%') == 0) then
106 label = trim(label)//trim(unitslabel)
107 endif
108
109 if (usebox) call plot_box_around_text('T',trim(label),hpos,vpos,fjust)
110 call plot_annotate('T',-vpos,hpos,fjust,trim(label))
111
112 ! save bounding box of legend for later queries
113 call save_bbox('T',trim(label),hpos,vpos,fjust,ilegend)
114
115 end subroutine legend
116
117 !-----------------------------------------------------------------
118 ! utility routine for plotting translucent box in legends
119 !-----------------------------------------------------------------
120 subroutine plot_box_around_text(pos,string,hpos,vpos,fjust)
121 use plotlib, only:plot_qci,plot_sci,plot_sfs,plot_set_opacity,plot_rect
122 character(len=1), intent(in) :: pos
123 character(len=*), intent(in) :: string
124 real, intent(in) :: hpos,vpos,fjust
125 real :: x1,x2,y1,y2,ych
126 integer :: ic
127
128 call get_box_around_text(pos,string,hpos,vpos,fjust,x1,x2,y1,y2,ych)
129 !
130 !--draw box around the string
131 !
132 call plot_qci(ic) ! query colour index
133 call plot_sci(0) ! background colour
134 call plot_sfs(1) ! solid fill style
135 call plot_set_opacity(0.5)
136 call plot_rect(x1,x2,y1,y2,0.2*ych) ! draw a (rounded) rectangle
137 call plot_set_opacity(1.0)
138 call plot_sci(ic) ! restore colour index
139
140 end subroutine plot_box_around_text
141
142 !-----------------------------------------------------------------
143 ! helper routine to find the bounding box of a text string
144 !-----------------------------------------------------------------
145 subroutine get_box_around_text(pos,string,hpos,vpos,fjust,x1,x2,y1,y2,ych)
146 use plotlib, only:plot_qwin,plot_qcs,plot_qtxt
147 character(len=1), intent(in) :: pos
148 character(len=*), intent(in) :: string
149 real, intent(in) :: hpos,vpos,fjust
150 real, intent(out) :: x1,x2,y1,y2,ych
151 real :: xmin,xmax,ymin,ymax,xpos,ypos
152 real :: xbuf,ybuf,dx,dy,xch
153 real :: xbox(4),ybox(4)
154 !
155 !--convert hpos and vpos to x, y to plot arrow
156 !
157 call plot_qwin(xmin,xmax,ymin,ymax)
158 xpos = xmin + hpos*(xmax-xmin)
159 call plot_qcs(4,xch,ych)
160 select case(pos)
161 case('B') ! from bottom
162 ypos = ymin + (vpos + 1.)*ych
163 case default ! 'T' = from top
164 ypos = ymax - (vpos + 1.)*ych
165 end select
166 !
167 !--enquire bounding box of string
168 !
169 call plot_qtxt(xpos,ypos,0.0,0.0,trim(string),xbox,ybox)
170
171 xbuf = 0.25*xch
172 ybuf = 0.5*ych
173 dx = xbox(3) - xbox(1)
174 dy = ybox(3) - ybox(1) + 0.25*ych
175 x1 = xpos - fjust*dx - xbuf
176 x2 = x1 + dx + 2.*xbuf
177 y1 = ypos
178 y2 = y1 + dy + ybuf
179
180 end subroutine get_box_around_text
181
182 !-----------------------------------------------------------------
183 ! save the bounding box of a text string into the relevant
184 ! global variable
185 !-----------------------------------------------------------------
186 subroutine save_bbox(pos,string,hpos,vpos,fjust,id)
187 character(len=1), intent(in) :: pos
188 character(len=*), intent(in) :: string
189 real, intent(in) :: hpos,vpos,fjust
190 integer, intent(in) :: id
191 real :: ych
192
193 if (id > 0 .and. id <= nlegend_types) then
194 call get_box_around_text(pos,string,hpos,vpos,fjust,&
195 bbox(id)%x1,bbox(id)%x2,bbox(id)%y1,bbox(id)%y2,ych)
196 endif
197
198 end subroutine save_bbox
199
200 !-----------------------------------------------------------------
201 ! plots vector plot legend
202 ! arguments:
203 ! t : current time
204 ! hpos : horizontal position as fraction of viewport
205 ! vpos : vertical position in character heights from top
206 ! charheight : this is the text character height
207 ! (legend_vec is called directly after plotting the arrows,
208 ! which may use a different character size - so we plot
209 ! the arrow here in the same way, but then revert to
210 ! the text character height to write the text)
211 !-----------------------------------------------------------------
212
213 subroutine legend_vec(label,unitslabel,vecmax,dx,hpos,vpos,charheight)
214 use plotlib, only:plot_qwin,plot_qch,plot_sch,plot_qcs,plot_numb,plot_qtxt, &
215 plot_qci,plot_sci,plot_sfs,plot_rect,plot_sci,plot_text, &
216 plot_qvp,plot_svp,plot_swin,plot_arro,plot_set_opacity
217 real, intent(in) :: vecmax,dx,hpos,vpos,charheight
218 character(len=*), intent(in) :: label,unitslabel
219 real :: xmin,xmax,ymin,ymax
220 real :: xch,ych,charheightarrow,adjustlength,vecmaxnew
221 real :: xpos,ypos,xbox(4),ybox(4),dxlabel,dxstring
222 real :: dxbuffer,dybuffer,dxbox,dybox
223 real :: xminnew,xmaxnew,yminnew,ymaxnew,x1,x2,y1,y2
224 integer :: icolindex,mm,pp,nc,ndec
225 character(len=len(label)+20) :: string
226
227 !
228 !--convert hpos and vpos to x, y to plot arrow
229 !
230 call plot_qwin(xmin,xmax,ymin,ymax)
231 call plot_qch(charheightarrow)
232 call plot_sch(charheight)
233 xpos = xmin + hpos*(xmax-xmin)
234 call plot_qcs(4,xch,ych)
235 ypos = ymax - (vpos + 1.)*ych
236 !
237 !--format string containing numerical value
238 ! vecmax corresponds to arrow of length dx
239 ! we will draw an arrow of length sqrt(dx^2 + ych^2)
240 ! so adjust vecmax accordingly
241 !
242 adjustlength = sqrt(0.5*dx**2 + ych**2)/dx
243 vecmaxnew = adjustlength*vecmax
244 ndec = 2
245 if (vecmaxnew < tiny(vecmaxnew)) then
246 string = '0'
247 nc = 1
248 else
249 mm=int(vecmaxnew/10.**(int(log10(vecmaxnew)-ndec)))
250 pp=int(log10(vecmaxnew)-ndec)
251 call plot_numb(mm,pp,0,string,nc)
252 endif
253 string = '='//trim(string)
254 ! write(string,"('=',1pe7.1)") vecmax
255 !
256 !--enquire size of label
257 !
258 call plot_qtxt(xpos,ypos,0.0,0.0,trim(label),xbox,ybox)
259 dxlabel = xbox(3) - xbox(2) + 0.5*xch
260 !
261 !--enquire size of string + units label
262 !
263 call plot_qtxt(xpos,ypos,0.0,0.0,trim(string)//trim(unitslabel),xbox,ybox)
264 dxstring = xbox(3) - xbox(2)
265 !
266 !--set size of box in x direction
267 !
268 dxbuffer = 0.25*xch ! these are size of margins (x and y)
269 dybuffer = 0.25*ych
270 dxbox = dxlabel + dxstring + 1.1*dx/sqrt(2.) + dxbuffer
271 dybox = ych + 0.5*dybuffer
272 !
273 !--draw box around all of the legend
274 !
275 call plot_qci(icolindex)
276 ! draw a (rounded) rectangle in the background colour with solid fill style
277 call plot_sci(0)
278 call plot_sfs(1)
279 call plot_set_opacity(0.66)
280 x1 = xpos - dxbuffer
281 x2 = xpos + dxbox
282 y1 = ypos - dybuffer
283 y2 = ypos + dybox
284 call plot_rect(x1,x2,y1,y2,0.33*ych)
285 call plot_set_opacity(1.0)
286 ! change to foreground colour index
287 call plot_sci(1)
288 ! draw an outline around the box
289 ! call pgsfs(2)
290 ! call pgrect(xpos-dxbuffer,xpos+dxbox,ypos-dybuffer,ypos + dybox)
291 ! call pgsfs(1)
292 !
293 !--save bounding box
294 !
295 bbox(ilegend_vec)%x1 = x1
296 bbox(ilegend_vec)%x2 = x2
297 bbox(ilegend_vec)%y1 = y1
298 bbox(ilegend_vec)%y2 = y2
299 !
300 !--write label
301 !
302 call plot_text(xpos,ypos,trim(label))
303 xpos = xpos + dxlabel
304 !
305 !--Draw arrow. Here we have to perform tricks to get the arrow
306 ! to appear even if outside the usual plotting area
307 !
308 !--save viewport settings
309 call plot_qvp(0,x1,x2,y1,y2)
310 !--now allow the whole screen to be the viewport...
311 call plot_svp(0.0,1.0,0.0,1.0)
312 ! ...but correspondingly adjust window so that x and y positions
313 ! are the same as in the old viewport
314 xminnew = xmin - x1*(xmax-xmin)/(x2-x1)
315 xmaxnew = xmax + (1.-x2)*(xmax-xmin)/(x2-x1)
316 yminnew = ymin - y1*(ymax-ymin)/(y2-y1)
317 ymaxnew = ymax + (1.-y2)*(ymax-ymin)/(y2-y1)
318 call plot_swin(xminnew,xmaxnew,yminnew,ymaxnew)
319 !--use character height original arrows were drawn with
320 ! (this is to get the arrow head size right)
321 call plot_sch(charheightarrow)
322 !--draw arrow
323 call plot_arro(xpos,ypos,xpos + dx/sqrt(2.),ypos + ych)
324 !--restore viewport settings
325 call plot_svp(x1,x2,y1,y2)
326 call plot_swin(xmin,xmax,ymin,ymax)
327 xpos = xpos + 1.1*dx/sqrt(2.)
328 !
329 !--write numerical value and units label
330 !
331 call plot_sch(charheight)
332 !! call pgmtext('t',-vpos,hpos+0.02,0.0,trim(string))
333 call plot_text(xpos,ypos,trim(string)//trim(unitslabel))
334 !
335 !--restore colour index
336 call plot_sci(icolindex)
337
338 end subroutine legend_vec
339
340 !-------------------------------------------------------------------------
341 ! draw a legend for different line/marker styles
342 ! uses current line style and colour
343 ! plots this below the time legend
344 !-------------------------------------------------------------------------
345 subroutine legend_markers(icall,icolour,imarkerstyle,ilinestyle, &
346 iplotpts,iplotline,text,hposlegend,vposlegend,alphalegend)
347 use plotlib, only:plot_qwin,plot_qcs,plot_qci,plot_qls,plot_sci,plot_sls, &
348 plot_line,plot_pt,plot_text,plot_stbg,plot_slc,plot_qlc,plot_set_opacity
349 integer, intent(in) :: icall,icolour,imarkerstyle,ilinestyle
350 logical, intent(in) :: iplotpts,iplotline
351 character(len=*), intent(in) :: text
352 real, intent(in) :: hposlegend,vposlegend,alphalegend
353 integer :: icolourprev, ilinestyleprev,ilinecapprev
354 real, dimension(3) :: xline,yline
355 real :: xch, ych, xmin, xmax, ymin, ymax
356 real :: vspace, vpos
357 !
358 !--do not plot anything if string is blank
359 !
360 if (len_trim(text) <= 0) return
361 !call pgstbg(0) ! opaque text to overwrite previous
362 !
363 !--set horizontal and vertical position and spacing
364 ! in units of the character height
365 !
366 vspace = 1.5 ! (in units of character heights)
367 vpos = vposlegend + icall*vspace + 0.5 ! distance from top, in units of char height
368
369 call plot_qwin(xmin,xmax,ymin,ymax) ! query xmax, ymax
370 call plot_qcs(4,xch,ych) ! query character height in x and y units
371 call plot_qci(icolourprev) ! save current colour index
372 call plot_qls(ilinestyleprev) ! save current line style
373 call plot_qlc(ilinecapprev) ! save the current line cap
374
375
376 yline(:) = ymax - ((vpos - 0.5)*ych)
377 xline(1) = xmin + hposlegend*(xmax-xmin)
378 xline(2) = xline(1) + 1.5*xch
379 xline(3) = xline(1) + 3.*xch
380
381 call plot_sci(icolour)
382 call plot_set_opacity(alphalegend)
383 call plot_sls(ilinestyle)
384 !
385 !--set round caps
386 !
387 !call plot_slc(1)
388 !
389 !--draw a small line segment
390 !
391 if (iplotline) call plot_line(3,xline,yline)
392
393 call plot_slc(ilinecapprev)
394 call plot_sls(ilinestyleprev)
395 !
396 !--draw points, only two if line is also plotted so that you can see the line
397 ! three otherwise
398 !
399 if (iplotpts .and. iplotline) then
400 xline(2) = xline(3)
401 call plot_pt(2,xline(1:2),yline(1:2),imarkerstyle)
402 elseif (iplotpts) then
403 call plot_pt(3,xline,yline,imarkerstyle)
404 endif
405 !
406 !--add text
407 !
408 if (iplotline .or. iplotpts .and. len_trim(text) > 0) then
409 call plot_text(xline(3) + 0.75*xch,yline(1)-0.25*ych,trim(text))
410 endif
411
412 ! save (approximate) bounding box of legend for later queries
413 call save_bbox('T',trim(text),hposlegend,vpos,0.,ilegend_markers)
414 ! count the number of lines to give the extent in the y direction
415 bbox(ilegend_markers)%y2 = bbox(ilegend_markers)%y2 + icall*ych
416
417 call plot_sci(icolourprev) ! reset colour index
418 call plot_set_opacity(1.0)
419 call plot_stbg(-1) ! reset text background to transparent
420
421 end subroutine legend_markers
422
423 !-------------------------------------------------------------------
424 ! plots labelled scale (horizontal error bar of a given length)
425 ! can be used on co-ordinate plots to give a length scale
426 !
427 ! e.g. would produce something like:
428 !
429 ! |----|
430 ! 10 AU
431 !
432 ! arguments:
433 ! dxscale : length of scale in current x units
434 ! hpos : horizontal position as fraction of viewport
435 ! vpos : vertical position in character heights from top
436 ! text : label to print above scale
437 !-----------------------------------------------------------------
438 subroutine legend_scale(dxscale,hpos,vpos,text)
439 use plotlib, only:plot_qwin,plot_qcs,plot_err1,plot_annotate
440 real, intent(in) :: dxscale,hpos,vpos
441 character(len=*), intent(in) :: text
442 real :: xmin,xmax,ymin,ymax,xch,ych,xpos,ypos
443
444 call plot_qwin(xmin,xmax,ymin,ymax)
445 if (dxscale > (xmax-xmin)) then
446 print "(a)",'Error: scale size exceeds x dimensions: scale not plotted'
447 else
448 call plot_qcs(4,xch,ych)
449 !--draw horizontal "error bar" above text
450 ypos = ymin + (vpos+1.25)*ych
451 xpos = xmin + hpos*(xmax-xmin)
452 call plot_err1(5,xpos,ypos,0.5*dxscale,1.0)
453
454 !--write text at the position specified
455 call plot_annotate('B',-vpos,hpos,0.5,trim(text))
456
457 ! save (approximate) bounding box of legend for later queries
458 call save_bbox('B',trim(text),hpos,-vpos,0.5,ilegend_scale)
459 endif
460
461 end subroutine legend_scale
462
463 !-------------------------------------------------------------------
464 ! The following subroutines handle the plotting of annotation
465 ! and legends only on particular panels
466 !-------------------------------------------------------------------
467 subroutine prompt_panelselect(string,iselect)
468 use prompting, only:prompt
469 character(len=*), intent(in) :: string
470 integer, intent(inout) :: iselect
471
472 print "(4(/,a))", &
473 ' 0 : plot '//trim(string)//' on every panel ', &
474 ' n : plot '//trim(string)//' on nth panel only ', &
475 ' -1 : plot '//trim(string)//' on first row only ', &
476 ' -2 : plot '//trim(string)//' on first column only '
477 call prompt('Enter selection ',iselect,-2)
478
479 end subroutine prompt_panelselect
480
481 !-------------------------------------------------------------------
482 ! Function that evaluates the logic required to determine
483 ! whether the annotation should be plotted on the current panel
484 ! as per the prompts in prompt_panelselect
485 !-------------------------------------------------------------------
486 logical function ipanelselect(iselect,ipanel,irow,icolumn)
487 integer, intent(in) :: iselect,ipanel,irow,icolumn
488
489 ipanelselect = ((iselect > 0 .and. ipanel==iselect) &
490 .or.(iselect==-1 .and. irow==1) &
491 .or.(iselect==-2 .and. icolumn==1) &
492 .or.(iselect==0))
493
494 end function ipanelselect
495
496 !-------------------------------------------------------------------
497 ! Function to determine whether the mouse is positioned over
498 ! one of the legends
499 !-------------------------------------------------------------------
500 integer function in_legend(xpt,ypt)
501 real, intent(in) :: xpt,ypt
502 integer :: id
503
504 in_legend = 0
505 do id=1,nlegend_types
506 if (xpt >= bbox(id)%x1 .and. xpt <= bbox(id)%x2 .and. &
507 ypt >= bbox(id)%y1 .and. ypt <= bbox(id)%y2) then
508 in_legend = id
509 exit ! exit loop
510 endif
511 enddo
512
513 end function in_legend
514
515 end module legends