fplot 1.7.1
A Fortran library providing a convenient interface for plotting with Gnuplot.
Loading...
Searching...
No Matches
fplot_multiplot.f90
1! fplot_multiplot.f90
2
3submodule(fplot_core) fplot_multiplot
4contains
5! ------------------------------------------------------------------------------
6 module function mp_get_command(this) result(x)
7 ! Arguments
8 class(multiplot), intent(in) :: this
9 character(len = :), allocatable :: x
10
11 ! Local Variables
12 type(string_builder) :: str
13 integer(int32) :: i, j, m, n
14 class(plot), pointer :: ptr
15
16 ! Initialization
17 call str%initialize()
18 m = this%get_row_count()
19 n = this%get_column_count()
20
21 ! Set up the multiplot
22 call str%append("set multiplot layout ")
23 call str%append(to_string(m))
24 call str%append(",")
25 call str%append(to_string(n))
26 call str%append(" columnsfirst")
27 if (this%is_title_defined()) then
28 call str%append(" title ")
29 call str%append('"')
30 call str%append(this%get_title())
31 call str%append('"')
32 end if
33 call str%append(new_line('a'))
34
35 ! Write commands for each plot object
36 do j = 1, n
37 do i = 1, m
38 ptr => this%get(i, j)
39 call str%append(new_line('a'))
40 call str%append(ptr%get_command_string())
41 end do
42 end do
43
44 ! Close out the multiplot
45 call str%append(new_line('a'))
46 call str%append("unset multiplot")
47
48 ! Get the string
49 x = char(str%to_string())
50 end function
51
52! ------------------------------------------------------------------------------
53 module subroutine mp_init(this, m, n, term, err)
54 ! Arguments
55 class(multiplot), intent(inout) :: this
56 integer(int32), intent(in) :: m, n
57 integer(int32), intent(in), optional :: term
58 class(errors), intent(inout), optional, target :: err
59
60 ! Local Variables
61 integer(int32) :: flag, t, i
62 class(errors), pointer :: errmgr
63 type(errors), target :: deferr
64 type(wxt_terminal), pointer :: wxt
65 type(windows_terminal), pointer :: win
66 type(qt_terminal), pointer :: qt
67 type(png_terminal), pointer :: png
68 type(latex_terminal), pointer :: latex
69
70 ! Initialization
71 if (present(err)) then
72 errmgr => err
73 else
74 errmgr => deferr
75 end if
76 if (present(term)) then
77 t = term
78 else
79 t = gnuplot_terminal_wxt
80 end if
81
82 ! Process
83 call this%m_plots%clear()
84 this%m_rows = m
85 this%m_cols = n
86 flag = 0
87
88 ! Populate the list with a dummy variable at the outset. This allows
89 ! the list to be appropriately sized so the user may use the "set"
90 ! subroutine appropriately
91 do i = 1, m * n
92 call this%m_plots%push(i)
93 end do
94
95 ! Define the terminal
96 if (associated(this%m_terminal)) deallocate(this%m_terminal)
97 select case (t)
98 case (gnuplot_terminal_png)
99 allocate(png, stat = flag)
100 this%m_terminal => png
101 case (gnuplot_terminal_qt)
102 allocate(qt, stat = flag)
103 this%m_terminal => qt
104 case (gnuplot_terminal_win32)
105 allocate(win, stat = flag)
106 this%m_terminal => win
107 case (gnuplot_terminal_latex)
108 allocate(latex, stat = flag)
109 this%m_terminal => latex
110 case default ! WXT is the default
111 allocate(wxt, stat = flag)
112 this%m_terminal => wxt
113 end select
114
115 ! Error Checking
116 if (flag /= 0) then
117 call errmgr%report_error("mp_init", &
118 "Insufficient memory available.", plot_out_of_memory_error)
119 return
120 end if
121 end subroutine
122
123! ------------------------------------------------------------------------------
124 module subroutine mp_clean(this)
125 type(multiplot), intent(inout) :: this
126 if (associated(this%m_terminal)) deallocate(this%m_terminal)
127 nullify(this%m_terminal)
128 end subroutine
129
130! ------------------------------------------------------------------------------
131 pure module function mp_get_rows(this) result(x)
132 class(multiplot), intent(in) :: this
133 integer(int32) :: x
134 x = this%m_rows
135 end function
136
137! --------------------
138 pure module function mp_get_cols(this) result(x)
139 class(multiplot), intent(in) :: this
140 integer(int32) :: x
141 x = this%m_cols
142 end function
143
144! --------------------
145 pure module function mp_get_count(this) result(x)
146 class(multiplot), intent(in) :: this
147 integer(int32) :: x
148 x = this%m_plots%count()
149 end function
150
151! ------------------------------------------------------------------------------
152 module function mp_get_title(this) result(x)
153 class(multiplot), intent(in) :: this
154 character(len = :), allocatable :: x
155 x = this%m_title
156 end function
157
158! --------------------
159 module subroutine mp_set_title(this, x)
160 ! Arguments
161 class(multiplot), intent(inout) :: this
162 character(len = *), intent(in) :: x
163
164 ! Local Variables
165 integer(int32) :: n
166
167 ! Process
168 n = min(len(x), plotdata_max_name_length)
169 this%m_title = ""
170 if (n /= 0) then
171 this%m_title(1:n) = x(1:n)
172 this%m_hasTitle = .true.
173 else
174 this%m_hasTitle = .false.
175 end if
176 end subroutine
177
178! ------------------------------------------------------------------------------
179 module subroutine mp_draw(this, persist, err)
180 ! Arguments
181 class(multiplot), intent(in) :: this
182 logical, intent(in), optional :: persist
183 class(errors), intent(inout), optional, target :: err
184
185 ! Parameters
186 character(len = *), parameter :: fname = "temp_gnuplot_file.plt"
187
188 ! Local Variables
189 logical :: p
190 integer(int32) :: fid, flag
191 class(errors), pointer :: errmgr
192 type(errors), target :: deferr
193 character(len = 256) :: errmsg
194 class(terminal), pointer :: term
195
196 ! Initialization
197 if (present(persist)) then
198 p = persist
199 else
200 p = .true.
201 end if
202 if (present(err)) then
203 errmgr => err
204 else
205 errmgr => deferr
206 end if
207 term => this%get_terminal()
208
209 ! Open the file for writing, and write the contents to file
210 open(newunit = fid, file = fname, iostat = flag)
211 if (flag > 0) then
212 write(errmsg, 100) &
213 "The file could not be opened/created. Error code ", flag, &
214 " was encountered."
215 call errmgr%report_error("mp_draw", trim(errmsg), &
216 plot_gnuplot_file_error)
217 return
218 end if
219 write(fid, '(A)') term%get_command_string()
220 write(fid, '(A)') new_line('a')
221 write(fid, '(A)') this%get_command_string()
222 close(fid)
223
224 ! Launch GNUPLOT
225 if (p) then
226 call execute_command_line("gnuplot --persist " // fname)
227 else
228 call execute_command_line("gnuplot " // fname)
229 end if
230
231 ! Clean up by deleting the file
232 open(newunit = fid, file = fname)
233 close(fid, status = "delete")
234
235100 format(a, i0, a)
236 end subroutine
237
238! ------------------------------------------------------------------------------
239 module function mp_get(this, i, j) result(x)
240 ! Arguments
241 class(multiplot), intent(in) :: this
242 integer(int32), intent(in) :: i, j
243 class(plot), pointer :: x
244
245 ! Local Variables
246 class(*), pointer :: item
247 integer(int32) :: ind
248
249 ! Process
250 ind = this%m_rows * (j - 1) + i
251 item => this%m_plots%get(ind)
252 select type (item)
253 class is (plot)
254 x => item
255 class default
256 nullify(x)
257 end select
258 end function
259
260! --------------------
261 module subroutine mp_set(this, i, j, x)
262 ! Arguments
263 class(multiplot), intent(inout) :: this
264 integer(int32), intent(in) :: i, j
265 class(plot), intent(in) :: x
266
267 ! Local Variables
268 integer(int32) :: ind
269
270 ! Process
271 ind = this%m_rows * (j - 1) + i
272 call this%m_plots%set(ind, x)
273 end subroutine
274
275! ------------------------------------------------------------------------------
276 pure module function mp_has_title(this) result(x)
277 class(multiplot), intent(in) :: this
278 logical :: x
279 x = this%m_hasTitle
280 end function
281
282! ------------------------------------------------------------------------------
283 module function mp_get_term(this) result(x)
284 class(multiplot), intent(in) :: this
285 class(terminal), pointer :: x
286 x => this%m_terminal
287 end function
288
289! ------------------------------------------------------------------------------
290 module subroutine mp_save(this, fname, err)
291 ! Arguments
292 class(multiplot), intent(in) :: this
293 character(len = *), intent(in) :: fname
294 class(errors), intent(inout), optional, target :: err
295
296 ! Local Variables
297 integer(int32) :: fid, flag
298 class(errors), pointer :: errmgr
299 type(errors), target :: deferr
300 character(len = 256) :: errmsg
301 class(terminal), pointer :: term
302
303 ! Initialization
304 if (present(err)) then
305 errmgr => err
306 else
307 errmgr => deferr
308 end if
309 term => this%get_terminal()
310
311 ! Open the file for writing, and write the contents to file
312 open(newunit = fid, file = fname, iostat = flag)
313 if (flag > 0) then
314 write(errmsg, 100) &
315 "The file could not be opened/created. Error code ", flag, &
316 " was encountered."
317 call errmgr%report_error("mp_save", trim(errmsg), &
318 plot_gnuplot_file_error)
319 return
320 end if
321 write(fid, '(A)') term%get_command_string()
322 write(fid, '(A)') new_line('a')
323 write(fid, '(A)') this%get_command_string()
324 close(fid)
325
326100 format(a, i0, a)
327 end subroutine
328
329! ------------------------------------------------------------------------------
330 module function mp_get_font(this) result(x)
331 class(multiplot), intent(in) :: this
332 character(len = :), allocatable :: x
333 class(terminal), pointer :: term
334 term => this%get_terminal()
335 x = term%get_font_name()
336 end function
337
338! --------------------
339 module subroutine mp_set_font(this, x)
340 class(multiplot), intent(inout) :: this
341 character(len = *), intent(in) :: x
342 class(terminal), pointer :: term
343 term => this%get_terminal()
344 call term%set_font_name(x)
345 end subroutine
346
347! ------------------------------------------------------------------------------
348 module function mp_get_font_size(this) result(x)
349 class(multiplot), intent(in) :: this
350 integer(int32) :: x
351 class(terminal), pointer :: term
352 term => this%get_terminal()
353 x = term%get_font_size()
354 end function
355
356! --------------------
357 module subroutine mp_set_font_size(this, x)
358 class(multiplot), intent(inout) :: this
359 integer(int32), intent(in) :: x
360 class(terminal), pointer :: term
361 term => this%get_terminal()
362 call term%set_font_size(x)
363 end subroutine
364
365! ------------------------------------------------------------------------------
366end submodule
fplot_core