fplot 1.7.1
A Fortran library providing a convenient interface for plotting with Gnuplot.
Loading...
Searching...
No Matches
fplot_plot.f90
1! fplot_plot.f90
2
3submodule(fplot_core) fplot_plot
4contains
5! ------------------------------------------------------------------------------
6 module subroutine plt_clean_up(this)
7 class(plot), intent(inout) :: this
8 if (associated(this%m_terminal)) then
9 deallocate(this%m_terminal)
10 nullify(this%m_terminal)
11 end if
12 if (associated(this%m_legend)) then
13 deallocate(this%m_legend)
14 nullify(this%m_legend)
15 end if
16 if (associated(this%m_colormap)) then
17 deallocate(this%m_colormap)
18 nullify(this%m_colormap)
19 end if
20 end subroutine
21
22! ------------------------------------------------------------------------------
23 module subroutine plt_init(this, term, fname, err)
24 ! Arguments
25 class(plot), intent(inout) :: this
26 integer(int32), intent(in), optional :: term
27 character(len = *), intent(in), optional :: fname
28 class(errors), intent(inout), optional, target :: err
29
30 ! Local Variables
31 integer(int32) :: flag, t
32 class(errors), pointer :: errmgr
33 type(errors), target :: deferr
34 type(wxt_terminal), pointer :: wxt
35 type(windows_terminal), pointer :: win
36 type(qt_terminal), pointer :: qt
37 type(png_terminal), pointer :: png
38 type(latex_terminal), pointer :: latex
39
40 ! Initialization
41 if (present(err)) then
42 errmgr => err
43 else
44 errmgr => deferr
45 end if
46 if (present(term)) then
47 t = term
48 else
49 t = gnuplot_terminal_wxt
50 end if
51
52 ! Process
53 flag = 0
54 if (associated(this%m_terminal)) deallocate(this%m_terminal)
55 select case (t)
56 case (gnuplot_terminal_png)
57 allocate(png, stat = flag)
58 if (present(fname)) call png%set_filename(fname)
59 this%m_terminal => png
60 case (gnuplot_terminal_qt)
61 allocate(qt, stat = flag)
62 this%m_terminal => qt
63 case (gnuplot_terminal_win32)
64 allocate(win, stat = flag)
65 this%m_terminal => win
66 case (gnuplot_terminal_latex)
67 allocate(latex, stat = flag)
68 if (present(fname)) call latex%set_filename(fname)
69 this%m_terminal => latex
70 case default ! WXT is the default
71 allocate(wxt, stat = flag)
72 this%m_terminal => wxt
73 end select
74
75 ! Establish the colormap
76 nullify(this%m_colormap)
77
78 if (flag == 0 .and. .not.associated(this%m_legend)) then
79 allocate(this%m_legend, stat = flag)
80 end if
81
82 ! Error Checking
83 if (flag /= 0) then
84 call errmgr%report_error("plt_init", &
85 "Insufficient memory available.", plot_out_of_memory_error)
86 return
87 end if
88 end subroutine
89
90! ------------------------------------------------------------------------------
91 module function plt_get_title(this) result(txt)
92 class(plot), intent(in) :: this
93 character(len = :), allocatable :: txt
94 integer(int32) :: n
95 n = len_trim(this%m_title)
96 allocate(character(len = n) :: txt)
97 txt = trim(this%m_title)
98 end function
99
100! --------------------
101 module subroutine plt_set_title(this, txt)
102 class(plot), intent(inout) :: this
103 character(len = *), intent(in) :: txt
104 integer :: n
105 n = min(len_trim(txt), plotdata_max_name_length)
106 this%m_title = ""
107 if (n /= 0) then
108 this%m_title(1:n) = txt(1:n)
109 this%m_hasTitle = .true.
110 else
111 this%m_hasTitle = .false.
112 end if
113 end subroutine
114
115! ------------------------------------------------------------------------------
116 pure module function plt_has_title(this) result(x)
117 class(plot), intent(in) :: this
118 logical :: x
119 x = this%m_hasTitle
120 end function
121
122! ------------------------------------------------------------------------------
123 module function plt_get_legend(this) result(x)
124 class(plot), intent(in) :: this
125 type(legend), pointer :: x
126 x => this%m_legend
127 end function
128
129! ------------------------------------------------------------------------------
130 pure module function plt_get_count(this) result(x)
131 class(plot), intent(in) :: this
132 integer(int32) :: x
133 x = this%m_data%count()
134 end function
135
136! ------------------------------------------------------------------------------
137 module subroutine plt_push_data(this, x, err)
138 ! Arguments
139 class(plot), intent(inout) :: this
140 class(plot_data), intent(inout) :: x
141 class(errors), intent(inout), optional, target :: err
142 class(legend), pointer :: lgnd
143
144 ! Index the color tracking index if the type is of plot_data_colored
145 select type (x)
146 class is (plot_data_colored)
147 call x%set_color_index(this%m_colorIndex)
148 if (this%m_colorIndex == size(color_list)) then
149 this%m_colorIndex = 1
150 else
151 this%m_colorIndex = this%m_colorIndex + 1
152 end if
153 end select
154
155 ! Store the object
156 call this%m_data%push(x, err = err)
157 end subroutine
158
159! ------------------------------------------------------------------------------
160 module subroutine plt_pop_data(this)
161 ! Arguments
162 class(plot), intent(inout) :: this
163 class(legend), pointer :: lgnd
164
165 ! Process
166 call this%m_data%pop()
167 if (this%m_data%count() < 2) then
168 lgnd => this%get_legend()
169 call lgnd%set_is_visible(.false.)
170 end if
171 end subroutine
172
173! ------------------------------------------------------------------------------
174 module subroutine plt_clear_all(this)
175 ! Arguments
176 class(plot), intent(inout) :: this
177
178 ! Process
179 this%m_colorIndex = 1
180 call this%m_data%clear()
181 end subroutine
182
183! ------------------------------------------------------------------------------
184 module function plt_get(this, i) result(x)
185 ! Arguments
186 class(plot), intent(in) :: this
187 integer(int32), intent(in) :: i
188 class(plot_data), pointer :: x
189
190 ! Local Variables
191 class(*), pointer :: item
192
193 ! Process
194 item => this%m_data%get(i)
195 select type (item)
196 class is (plot_data)
197 x => item
198 class default
199 nullify(x)
200 end select
201 end function
202
203
204! --------------------
205 module subroutine plt_set(this, i, x)
206 class(plot), intent(inout) :: this
207 integer(int32), intent(in) :: i
208 class(plot_data), intent(in) :: x
209 call this%m_data%set(i, x)
210 end subroutine
211
212! ------------------------------------------------------------------------------
213 module function plt_get_term(this) result(x)
214 class(plot), intent(in) :: this
215 class(terminal), pointer :: x
216 x => this%m_terminal
217 end function
218
219! ------------------------------------------------------------------------------
220 pure module function plt_get_show_grid(this) result(x)
221 class(plot), intent(in) :: this
222 logical :: x
223 x = this%m_showGrid
224 end function
225
226! --------------------
227 module subroutine plt_set_show_grid(this, x)
228 class(plot), intent(inout) :: this
229 logical, intent(in) :: x
230 this%m_showGrid = x
231 end subroutine
232
233! ------------------------------------------------------------------------------
234 module subroutine plt_draw(this, persist, err)
235 ! Arguments
236 class(plot), intent(in) :: this
237 logical, intent(in), optional :: persist
238 class(errors), intent(inout), optional, target :: err
239
240 ! Parameters
241 character(len = *), parameter :: fname = "temp_gnuplot_file.plt"
242
243 ! Local Variables
244 logical :: p
245 integer(int32) :: fid, flag
246 class(errors), pointer :: errmgr
247 type(errors), target :: deferr
248 character(len = 256) :: errmsg
249 class(terminal), pointer :: term
250
251 ! Initialization
252 if (present(persist)) then
253 p = persist
254 else
255 p = .true.
256 end if
257 if (present(err)) then
258 errmgr => err
259 else
260 errmgr => deferr
261 end if
262 term => this%get_terminal()
263
264 ! Open the file for writing, and write the contents to file
265 open(newunit = fid, file = fname, iostat = flag)
266 if (flag > 0) then
267 write(errmsg, 100) &
268 "The file could not be opened/created. Error code ", flag, &
269 " was encountered."
270 call errmgr%report_error("plt_draw", trim(errmsg), &
271 plot_gnuplot_file_error)
272 return
273 end if
274 write(fid, '(A)') term%get_command_string()
275 write(fid, '(A)') new_line('a')
276 write(fid, '(A)') this%get_command_string()
277 close(fid)
278
279 ! Launch GNUPLOT
280 if (p) then
281 call execute_command_line("gnuplot --persist " // fname)
282 else
283 call execute_command_line("gnuplot " // fname)
284 end if
285
286 ! Clean up by deleting the file
287 open(newunit = fid, file = fname)
288 close(fid, status = "delete")
289
290100 format(a, i0, a)
291 end subroutine
292
293! ------------------------------------------------------------------------------
294 module subroutine plt_save(this, fname, err)
295 ! Arguments
296 class(plot), intent(in) :: this
297 character(len = *), intent(in) :: fname
298 class(errors), intent(inout), optional, target :: err
299
300 ! Local Variables
301 integer(int32) :: fid, flag
302 class(errors), pointer :: errmgr
303 type(errors), target :: deferr
304 character(len = 256) :: errmsg
305 class(terminal), pointer :: term
306
307 ! Initialization
308 if (present(err)) then
309 errmgr => err
310 else
311 errmgr => deferr
312 end if
313 term => this%get_terminal()
314
315 ! Open the file for writing, and write the contents to file
316 open(newunit = fid, file = fname, iostat = flag)
317 if (flag > 0) then
318 write(errmsg, 100) &
319 "The file could not be opened/created. Error code ", flag, &
320 " was encountered."
321 call errmgr%report_error("plt_save", trim(errmsg), &
322 plot_gnuplot_file_error)
323 return
324 end if
325 write(fid, '(A)') term%get_command_string()
326 write(fid, '(A)') new_line('a')
327 write(fid, '(A)') this%get_command_string()
328 close(fid)
329
330100 format(a, i0, a)
331 end subroutine
332
333! ------------------------------------------------------------------------------
334 module function plt_get_font(this) result(x)
335 class(plot), intent(in) :: this
336 character(len = :), allocatable :: x
337 class(terminal), pointer :: term
338 term => this%get_terminal()
339 x = term%get_font_name()
340 end function
341
342! --------------------
343 module subroutine plt_set_font(this, x)
344 class(plot), intent(inout) :: this
345 character(len = *), intent(in) :: x
346 class(terminal), pointer :: term
347 term => this%get_terminal()
348 call term%set_font_name(x)
349 end subroutine
350
351! ------------------------------------------------------------------------------
352 module function plt_get_font_size(this) result(x)
353 class(plot), intent(in) :: this
354 integer(int32) :: x
355 class(terminal), pointer :: term
356 term => this%get_terminal()
357 x = term%get_font_size()
358 end function
359
360! --------------------
361 module subroutine plt_set_font_size(this, x)
362 class(plot), intent(inout) :: this
363 integer(int32), intent(in) :: x
364 class(terminal), pointer :: term
365 term => this%get_terminal()
366 call term%set_font_size(x)
367 end subroutine
368
369! ------------------------------------------------------------------------------
370 pure module function plt_get_tics_in(this) result(x)
371 class(plot), intent(in) :: this
372 logical :: x
373 x = this%m_ticsIn
374 end function
375
376! --------------------
377 module subroutine plt_set_tics_in(this, x)
378 class(plot), intent(inout) :: this
379 logical, intent(in) :: x
380 this%m_ticsIn = x
381 end subroutine
382
383! ------------------------------------------------------------------------------
384 pure module function plt_get_draw_border(this) result(x)
385 class(plot), intent(in) :: this
386 logical :: x
387 x = this%m_drawBorder
388 end function
389
390! --------------------
391 module subroutine plt_set_draw_border(this, x)
392 class(plot), intent(inout) :: this
393 logical, intent(in) :: x
394 this%m_drawBorder = x
395 end subroutine
396
397! ******************************************************************************
398! ADDED: JUNE 22, 2018 - JAC
399! ------------------------------------------------------------------------------
400 module subroutine plt_push_label(this, lbl, err)
401 ! Arguments
402 class(plot), intent(inout) :: this
403 class(plot_label), intent(in) :: lbl
404 class(errors), intent(inout), optional, target :: err
405
406 ! Process
407 call this%m_labels%push(lbl, err = err)
408 end subroutine
409
410! ------------------------------------------------------------------------------
411 module subroutine plt_pop_label(this)
412 class(plot), intent(inout) :: this
413 call this%m_labels%pop()
414 end subroutine
415
416! ------------------------------------------------------------------------------
417 module function plt_get_label(this, i) result(x)
418 ! Arguments
419 class(plot), intent(in) :: this
420 integer(int32), intent(in) :: i
421 class(plot_label), pointer :: x
422
423 ! Local Variables
424 class(*), pointer :: item
425
426 ! Process
427 item => this%m_labels%get(i)
428 select type (item)
429 class is (plot_label)
430 x => item
431 class default
432 nullify(x)
433 end select
434 end function
435
436! --------------------
437 module subroutine plt_set_label(this, i, x)
438 class(plot), intent(inout) :: this
439 integer(int32), intent(in) :: i
440 class(plot_label), intent(in) :: x
441 call this%m_labels%set(i, x)
442 end subroutine
443
444! ------------------------------------------------------------------------------
445 pure module function plt_get_label_count(this) result(x)
446 class(plot), intent(in) :: this
447 integer(int32) :: x
448 x = this%m_labels%count()
449 end function
450
451! ------------------------------------------------------------------------------
452 module subroutine plt_clear_labels(this)
453 class(plot), intent(inout) :: this
454 call this%m_labels%clear()
455 end subroutine
456
457! ******************************************************************************
458! ADDED: SEPT. 25, 2020 - JAC
459! ------------------------------------------------------------------------------
460 pure module function plt_get_axis_equal(this) result(rst)
461 class(plot), intent(in) :: this
462 logical :: rst
463 rst = this%m_axisEqual
464 end function
465
466! --------------------
467 module subroutine plt_set_axis_equal(this, x)
468 class(plot), intent(inout) :: this
469 logical, intent(in) :: x
470 this%m_axisEqual = x
471 end subroutine
472
473! ******************************************************************************
474! ADDED: OCT. 8, 2020 - JAC
475! ------------------------------------------------------------------------------
476 module function plt_get_colormap(this) result(x)
477 class(plot), intent(in) :: this
478 class(colormap), pointer :: x
479 x => this%m_colormap
480 end function
481
482! --------------------
483 module subroutine plt_set_colormap(this, x, err)
484 ! Arguments
485 class(plot), intent(inout) :: this
486 class(colormap), intent(in) :: x
487 class(errors), intent(inout), optional, target :: err
488
489 ! Local Variables
490 integer(int32) :: flag
491 class(errors), pointer :: errmgr
492 type(errors), target :: deferr
493
494 ! Initialization
495 if (present(err)) then
496 errmgr => err
497 else
498 errmgr => deferr
499 end if
500
501 ! Process
502 if (associated(this%m_colormap)) deallocate(this%m_colormap)
503 allocate(this%m_colormap, stat = flag, source = x)
504 if (flag /= 0) then
505 call errmgr%report_error("surf_set_colormap", &
506 "Insufficient memory available.", plot_out_of_memory_error)
507 return
508 end if
509 end subroutine
510
511! ------------------------------------------------------------------------------
512 pure module function plt_get_show_colorbar(this) result(x)
513 class(plot), intent(in) :: this
514 logical :: x
515 x = this%m_showColorbar
516 end function
517
518! --------------------
519 module subroutine plt_set_show_colorbar(this, x)
520 class(plot), intent(inout) :: this
521 logical, intent(in) :: x
522 this%m_showColorbar = x
523 end subroutine
524
525! ------------------------------------------------------------------------------
526 module function plt_get_cmd(this) result(x)
527 ! Arguments
528 class(plot), intent(in) :: this
529 character(len = :), allocatable :: x
530
531 ! Local Variables
532 integer(int32) :: i
533 type(string_builder) :: str
534 class(colormap), pointer :: clr
535 class(plot_arrow), pointer :: arrow
536 class(plot_label), pointer :: lbl
537
538 ! Initialization
539 call str%initialize()
540
541 ! Define the colormap
542 clr => this%get_colormap()
543 if (associated(clr)) then
544 call str%append(new_line('a'))
545 call str%append(clr%get_command_string())
546 end if
547
548 ! Show the colorbar
549 if (.not.this%get_show_colorbar()) then
550 call str%append(new_line('a'))
551 call str%append("unset colorbox")
552 end if
553
554 ! Arrows
555 do i = 1, this%get_arrow_count()
556 arrow => this%get_arrow(i)
557 if (.not.associated(arrow)) cycle
558 call str%append(new_line('a'))
559 call str%append(arrow%get_command_string())
560 end do
561
562 ! Labels
563 do i = 1, this%get_label_count()
564 lbl => this%get_label(i)
565 if (.not.associated(lbl)) cycle
566 call str%append(new_line('a'))
567 call str%append(lbl%get_command_string())
568 end do
569
570 ! End
571 x = char(str%to_string())
572 end function
573
574! ******************************************************************************
575! ADDED: 1/3/2024 - JAC
576! ------------------------------------------------------------------------------
577 module subroutine plt_push_arrow(this, x, err)
578 class(plot), intent(inout) :: this
579 class(plot_arrow), intent(in) :: x
580 class(errors), intent(inout), optional, target :: err
581 call this%m_arrows%push(x, manage = .true., err = err)
582 end subroutine
583
584! ------------------------------------------------------------------------------
585 module subroutine plt_pop_arrow(this)
586 class(plot), intent(inout) :: this
587 call this%m_arrows%pop()
588 end subroutine
589
590! ------------------------------------------------------------------------------
591 module function plt_get_arrow(this, i) result(rst)
592 class(plot), intent(in) :: this
593 integer(int32), intent(in) :: i
594 class(plot_arrow), pointer :: rst
595
596 class(*), pointer :: ptr
597 ptr => this%m_arrows%get(i)
598 select type (ptr)
599 class is (plot_arrow)
600 rst => ptr
601 class default
602 nullify(rst)
603 end select
604 end function
605
606! ------------------------------------------------------------------------------
607 module subroutine plt_set_arrow(this, i, x)
608 class(plot), intent(inout) :: this
609 integer(int32), intent(in) :: i
610 class(plot_arrow), intent(in) :: x
611 call this%m_arrows%set(i, x)
612 end subroutine
613
614! ------------------------------------------------------------------------------
615 pure module function plt_get_arrow_count(this) result(rst)
616 class(plot), intent(in) :: this
617 integer(int32) :: rst
618 rst = this%m_arrows%count()
619 end function
620
621! ------------------------------------------------------------------------------
622 module subroutine plt_clear_arrows(this)
623 class(plot), intent(inout) :: this
624 call this%m_arrows%clear()
625 end subroutine
626
627! ------------------------------------------------------------------------------
628end submodule
fplot_core