fplot 1.7.1
A Fortran library providing a convenient interface for plotting with Gnuplot.
Loading...
Searching...
No Matches
fplot_plot_2d.f90
1! fplot_plot_2d.f90
2
3submodule(fplot_core) fplot_plot_2d
4contains
5! ------------------------------------------------------------------------------
6 module subroutine p2d_clean_up(this)
7 type(plot_2d), intent(inout) :: this
8 call this%free_resources()
9 if (associated(this%m_xAxis)) then
10 deallocate(this%m_xAxis)
11 nullify(this%m_xAxis)
12 end if
13 if (associated(this%m_yAxis)) then
14 deallocate(this%m_yAxis)
15 nullify(this%m_yAxis)
16 end if
17 if (associated(this%m_y2Axis)) then
18 deallocate(this%m_y2Axis)
19 nullify(this%m_y2Axis)
20 end if
21 end subroutine
22
23! ------------------------------------------------------------------------------
24 module subroutine p2d_init(this, term, fname, err)
25 ! Arguments
26 class(plot_2d), intent(inout) :: this
27 integer(int32), intent(in), optional :: term
28 character(len = *), intent(in), optional :: fname
29 class(errors), intent(inout), optional, target :: err
30
31 ! Local Variables
32 integer(int32) :: flag
33 class(errors), pointer :: errmgr
34 type(errors), target :: deferr
35
36 ! Initialization
37 if (present(err)) then
38 errmgr => err
39 else
40 errmgr => deferr
41 end if
42
43 ! Initialize the base class
44 call plt_init(this, term, fname, errmgr)
45 if (errmgr%has_error_occurred()) return
46
47 ! Process
48 flag = 0
49 if (.not.associated(this%m_xAxis)) then
50 allocate(this%m_xAxis, stat = flag)
51 end if
52 if (flag == 0 .and. .not.associated(this%m_yAxis)) then
53 allocate(this%m_yAxis, stat = flag)
54 end if
55 if (flag == 0 .and. .not.associated(this%m_y2Axis)) then
56 allocate(this%m_y2Axis, stat = flag)
57 end if
58
59 ! Error Checking
60 if (flag /= 0) then
61 call errmgr%report_error("p2d_init", &
62 "Insufficient memory available.", plot_out_of_memory_error)
63 return
64 end if
65 end subroutine
66
67! ------------------------------------------------------------------------------
68 module function p2d_get_cmd(this) result(x)
69 ! Arguments
70 class(plot_2d), intent(in) :: this
71 character(len = :), allocatable :: x
72
73 ! Local Variables
74 type(string_builder) :: str
75 integer(int32) :: i, n
76 class(plot_data), pointer :: ptr
77 class(plot_axis), pointer :: axis, xAxis, yAxis
78 type(legend), pointer :: leg
79 ! class(plot_label), pointer :: lbl
80
81 ! Initialization
82 call str%initialize()
83
84 ! Call the base routine
85 call str%append(this%plot%get_command_string())
86
87 ! Grid
88 if (this%get_show_gridlines()) then
89 call str%append(new_line('a'))
90 call str%append("set grid")
91 end if
92
93 ! Title
94 n = len_trim(this%get_title())
95 if (n > 0) then
96 call str%append(new_line('a'))
97 call str%append('set title "')
98 call str%append(this%get_title())
99 call str%append('"')
100 end if
101
102 ! Axes
103 call str%append(new_line('a'))
104 xaxis => this%get_x_axis()
105 if (associated(xaxis)) call str%append(xaxis%get_command_string())
106
107 call str%append(new_line('a'))
108 yaxis => this%get_y_axis()
109 if (associated(yaxis)) call str%append(yaxis%get_command_string())
110
111 ! Secondary Axes
112 if (this%get_use_y2_axis()) then
113 call str%append(new_line('a'))
114 axis => this%get_y2_axis()
115 if (associated(axis)) then
116 call str%append(axis%get_command_string())
117 call str%append(new_line('a'))
118 call str%append("set y2tics")
119 call str%append(new_line('a'))
120 call str%append("set ytics nomirror")
121 end if
122 end if
123
124 ! Tic Marks
125 if (.not.this%get_tics_inward()) then
126 call str%append(new_line('a'))
127 call str%append("set tics out")
128 end if
129 if (xaxis%get_zero_axis()) then
130 call str%append(new_line('a'))
131 call str%append("set xtics axis")
132 end if
133 if (yaxis%get_zero_axis()) then
134 call str%append(new_line('a'))
135 call str%append("set ytics axis")
136 end if
137
138
139 ! Border
140 call str%append(new_line('a'))
141 call str%append("set border back")
142
143 if (this%get_draw_border()) then
144 n = 31
145 else
146 n = 0
147 if (.not.xaxis%get_zero_axis()) n = n + 1
148 if (.not.yaxis%get_zero_axis()) n = n + 2
149
150 call str%append(new_line('a'))
151 call str%append("set xtics nomirror")
152 call str%append(new_line('a'))
153 call str%append("set ytics nomirror")
154
155 if (this%get_use_y2_axis()) then
156 n = n + 8
157 end if
158 end if
159
160 call str%append(new_line('a'))
161 if (n > 0) then
162 call str%append("set border ")
163 call str%append(to_string(n))
164 else
165 call str%append("unset border")
166 end if
167
168 ! Scaling
169 if (this%get_axis_equal()) then
170 call str%append(new_line('a'))
171 call str%append("set view equal xy")
172 end if
173
174 if (this%get_square_axes()) then
175 call str%append(new_line('a'))
176 call str%append("set size square")
177 end if
178
179 ! Legend
180 call str%append(new_line('a'))
181 leg => this%get_legend()
182 if (associated(leg)) call str%append(leg%get_command_string())
183
184 ! ! Labels
185 ! do i = 1, this%get_label_count()
186 ! lbl => this%get_label(i)
187 ! if (.not.associated(lbl)) cycle
188 ! call str%append(new_line('a'))
189 ! call str%append(lbl%get_command_string())
190 ! end do
191
192 ! Define the plot function and data formatting commands
193 n = this%get_count()
194 call str%append(new_line('a'))
195 call str%append("plot ")
196 do i = 1, n
197 ptr => this%get(i)
198 if (.not.associated(ptr)) cycle
199 call str%append(ptr%get_command_string())
200 if (i /= n) call str%append(", ")
201 end do
202
203 ! Define the data to plot
204 do i = 1, n
205 ptr => this%get(i)
206 if (.not.associated(ptr)) cycle
207 call str%append(new_line('a'))
208 call str%append(ptr%get_data_string())
209 call str%append("e")
210 ! if (i /= n) then
211 ! call str%append("e")
212 ! end if
213 end do
214
215 ! End
216 x = char(str%to_string())
217 end function
218
219! ------------------------------------------------------------------------------
220 module function p2d_get_x_axis(this) result(ptr)
221 class(plot_2d), intent(in) :: this
222 class(plot_axis), pointer :: ptr
223 ptr => this%m_xAxis
224 end function
225
226! ------------------------------------------------------------------------------
227 module function p2d_get_y_axis(this) result(ptr)
228 class(plot_2d), intent(in) :: this
229 class(plot_axis), pointer :: ptr
230 ptr => this%m_yAxis
231 end function
232
233! ------------------------------------------------------------------------------
234 module function p2d_get_y2_axis(this) result(ptr)
235 class(plot_2d), intent(in) :: this
236 class(plot_axis), pointer :: ptr
237 ptr => this%m_y2Axis
238 end function
239
240! ------------------------------------------------------------------------------
241 pure module function p2d_get_use_y2(this) result(x)
242 class(plot_2d), intent(in) :: this
243 logical :: x
244 x = this%m_useY2
245 end function
246
247! --------------------
248 module subroutine p2d_set_use_y2(this, x)
249 class(plot_2d), intent(inout) :: this
250 logical, intent(in) :: x
251 this%m_useY2 = x
252 end subroutine
253
254! ------------------------------------------------------------------------------
255 pure module function p2d_get_square_axes(this) result(rst)
256 class(plot_2d), intent(in) :: this
257 logical :: rst
258 rst = this%m_set2square
259 end function
260
261! --------------------
262 module subroutine p2d_set_square_axes(this, x)
263 class(plot_2d), intent(inout) :: this
264 logical, intent(in) :: x
265 this%m_set2square = x
266 end subroutine
267
268! ------------------------------------------------------------------------------
269end submodule
fplot_core