Skip to content

Commit a0aeba8

Browse files
Add to box and whisker
1 parent 4fdacc6 commit a0aeba8

2 files changed

Lines changed: 132 additions & 16 deletions

File tree

examples/box_whisker_example.f90

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ program example
55
implicit none
66

77
! Variables
8-
integer(int32), parameter :: n = 5
8+
integer(int32), parameter :: n = 3
99
integer(int32) :: i
1010
type(string) :: titles(n)
1111
real(real64) :: boxmin(n), boxmax(n), whiskermin(n), whiskermax(n)
@@ -14,22 +14,18 @@ program example
1414

1515
! Initialization
1616
do i = 1, n
17-
titles(i) = "Item " // to_string(i)
17+
titles(i) = "Item-" // to_string(i)
1818
end do
19-
call random_number(whiskermin)
20-
call random_number(whiskermax)
21-
call random_number(boxmin)
22-
call random_number(boxmax)
23-
24-
! Ensure proper order
25-
boxmin = boxmin + whiskermin
26-
boxmax = boxmin + boxmax
27-
whiskermax = whiskermax + boxmax
19+
boxmin = [4.5d0, 5.0d0, 6.0d0]
20+
boxmax = [7.5d0, 8.0d0, 9.0d0]
21+
whiskermin = [3.0d0, 4.0d0, 5.0d0]
22+
whiskermax = [9.0d0, 10.0d0, 11.0d0]
2823

2924
! Create the plot
3025
call plt%initialize()
3126

3227
call pd%define_data(titles, boxmin, boxmax, whiskermin, whiskermax)
28+
call pd%set_line_width(2.0)
3329
call plt%push(pd)
3430
call plt%draw()
3531
end program

src/fplot_plot_data_box_whisker.f90

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,28 @@ module fplot_plot_data_box_whisker
2323
!! The maximum y-values for each whisker.
2424
logical, private :: m_useY2 = .false.
2525
!! Plot against the secondary y-axis?
26+
logical, private :: m_whiskerbars = .true.
27+
!! Use horizontal whisker bar caps?
28+
real(real32), private :: m_whiskerWidth = 1.0
29+
!! On a scale of 0 -> 1, the whiskerwidth.
30+
real(real32), private :: m_lineWidth = 1.0
31+
!! The line width.
32+
real(real32), private :: m_boxWidth = 0.05
33+
!! The box width.
2634
contains
2735
procedure, public :: define_data => pdbw_define_data_xstring
2836
procedure, public :: get_command_string => pdbw_get_cmd
2937
procedure, public :: get_data_string => pdbw_get_data_cmd
3038
procedure, public :: get_draw_against_y2 => pdbw_get_use_y2
3139
procedure, public :: set_draw_against_y2 => pdbw_set_use_y2
40+
procedure, public :: get_use_whiskerbars => pdbw_get_use_whiskerbars
41+
procedure, public :: set_use_whiskerbars => pdbw_set_use_whiskerbars
42+
procedure, public :: get_whiskerbar_width => pdbw_get_whiskerbar_width
43+
procedure, public :: set_whiskerbar_width => pdbw_set_whiskerbar_width
44+
procedure, public :: get_line_width => pdbw_get_line_width
45+
procedure, public :: set_line_width => pdbw_set_line_width
46+
procedure, public :: get_box_width => pdbw_get_box_width
47+
procedure, public :: set_box_width => pdbw_set_box_width
3248
end type
3349

3450
contains
@@ -95,27 +111,38 @@ function pdbw_get_cmd(this) result(rst)
95111
integer(int32) :: n, nname
96112
type(color) :: clr
97113

114+
! Style
115+
! call str%append(' "-" using ($0+1):2:3:4:5:xtic(1) with candlesticks')
116+
call str%append(' "-" using ($0+1):2:3:4:5:(')
117+
call str%append(to_string(this%get_box_width()))
118+
call str%append("):xtic(1) with candlesticks")
119+
98120
! Title
99121
nname = len_trim(this%get_name())
100122
if (n > 0) then
101-
call str%append(' "-" title "')
123+
call str%append(' title "')
102124
call str%append(this%get_name())
103125
call str%append('"')
104126
else
105-
call str%append(' "-" notitle')
127+
call str%append(' notitle')
106128
end if
107129

108-
! Style
109-
call str%append(" using ($0+1):2:3:4:5:xtic(1) with candlesticks")
110-
111130
! Whisker bars
131+
if (this%get_use_whiskerbars()) then
132+
call str%append(" whiskerbars ")
133+
call str%append(to_string(this%get_whiskerbar_width()))
134+
end if
112135

113136
! Color
114137
clr = this%get_line_color()
115138
call str%append(' lc rgb "#')
116139
call str%append(clr%to_hex_string())
117140
call str%append('"')
118141

142+
! Line Width
143+
call str%append(" lw ")
144+
call str%append(to_string(this%get_line_width()))
145+
119146
! End
120147
rst = char(str%to_string())
121148
end function
@@ -198,8 +225,101 @@ subroutine pdbw_set_use_y2(this, x)
198225
end subroutine
199226

200227
! ------------------------------------------------------------------------------
228+
pure function pdbw_get_use_whiskerbars(this) result(rst)
229+
!! Gets a value determining if whiskerbars should be used.
230+
class(plot_data_box_whisker), intent(in) :: this
231+
!! The plot_data_box_whisker object.
232+
logical :: rst
233+
!! True if whiskerbars should be used; else, false.
234+
rst = this%m_whiskerbars
235+
end function
236+
237+
! --------------------
238+
subroutine pdbw_set_use_whiskerbars(this, x)
239+
!! Sets a value determining if whiskerbars should be used.
240+
class(plot_data_box_whisker), intent(inout) :: this
241+
!! The plot_data_box_whisker object.
242+
logical, intent(in) :: x
243+
!! Set to true if whiskerbars should be used; else, false.
244+
this%m_whiskerbars = x
245+
end subroutine
201246

202247
! ------------------------------------------------------------------------------
248+
pure function pdbw_get_whiskerbar_width(this) result(rst)
249+
!! Gets the width of whiskerbar.
250+
class(plot_data_box_whisker), intent(in) :: this
251+
!! The plot_data_box_whisker object.
252+
real(real32) :: rst
253+
!! The width of the whiskerbar on a scale of 0:1 with 1 being the full
254+
!! width.
255+
rst = this%m_whiskerWidth
256+
end function
257+
258+
! --------------------
259+
subroutine pdbw_set_whiskerbar_width(this, x)
260+
!! Sets the width of the whiskerbar.
261+
class(plot_data_box_whisker), intent(inout) :: this
262+
!! The plot_data_box_whisker object.
263+
real(real32), intent(in) :: x
264+
!! The width of the whiskerbar. This value is clamped to [0, 1] with
265+
!! 1 representing full width.
266+
267+
if (x < 0.0d0) then
268+
this%m_whiskerWidth = 0.0d0
269+
else if (x > 1.0d0) then
270+
this%m_whiskerWidth = 1.0d0
271+
else
272+
this%m_whiskerWidth = x
273+
end if
274+
end subroutine
275+
276+
! ------------------------------------------------------------------------------
277+
pure function pdbw_get_line_width(this) result(x)
278+
!! Gets the width of the line, in pixels.
279+
class(plot_data_box_whisker), intent(in) :: this
280+
!! The plot_data_box_whisker object.
281+
real(real32) :: x
282+
!! The line width.
283+
x = this%m_lineWidth
284+
end function
285+
286+
! --------------------
287+
subroutine pdbw_set_line_width(this, x)
288+
!! Sets the width of the line, in pixels.
289+
class(plot_data_box_whisker), intent(inout) :: this
290+
!! The plot_data_box_whisker object.
291+
real(real32), intent(in) :: x
292+
!! The line width.
293+
this%m_lineWidth = x
294+
end subroutine
295+
296+
! ------------------------------------------------------------------------------
297+
pure function pdbw_get_box_width(this) result(rst)
298+
!! Gets the box width.
299+
class(plot_data_box_whisker), intent(in) :: this
300+
!! The plot_data_box_whisker object.
301+
real(real32) :: rst
302+
!! The box width.
303+
rst = this%m_boxWidth
304+
end function
305+
306+
! --------------------
307+
subroutine pdbw_set_box_width(this, x)
308+
!! Sets the box width.
309+
class(plot_data_box_whisker), intent(inout) :: this
310+
!! The plot_data_box_whisker object.
311+
real(real32), intent(in) :: x
312+
!! The box width.
313+
this%m_boxWidth = x
314+
end subroutine
315+
316+
! ------------------------------------------------------------------------------
317+
318+
! --------------------
319+
320+
! ------------------------------------------------------------------------------
321+
322+
! --------------------
203323

204324
! ------------------------------------------------------------------------------
205325
end module

0 commit comments

Comments
 (0)