Skip to content

Commit 9d3170e

Browse files
Merge pull request #15 from jchristopherson/legend_updates
Added additional functionality to the legend type
2 parents b074da1 + ceffe94 commit 9d3170e

4 files changed

Lines changed: 172 additions & 2 deletions

File tree

examples/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,8 @@ target_link_libraries(fplot_fill_2 fplot)
120120

121121
# Example 31
122122
add_executable(fplot_histogram_1 fplot_histogram_1.f90)
123-
target_link_libraries(fplot_histogram_1 fplot)
123+
target_link_libraries(fplot_histogram_1 fplot)
124+
125+
# Example 32
126+
add_executable(fplot_legend_1 fplot_legend_1.f90)
127+
target_link_libraries(fplot_legend_1 fplot)

examples/fplot_legend_1.f90

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
program example
2+
use fplot_core
3+
use iso_fortran_env
4+
implicit none
5+
6+
! Local Variables & Parameters
7+
integer(int32), parameter :: npts = 1000
8+
real(real64), dimension(npts) :: x, y
9+
type(plot_2d) :: plt
10+
type(plot_data_2d) :: dataset
11+
class(plot_axis), pointer :: xAxis, yAxis
12+
type(legend), pointer :: leg
13+
14+
! Build a data set to plot
15+
x = linspace(0.0d0, 10.0d0, npts)
16+
y = exp(-0.5d0 * x) * sin(10.0d0 * x - 0.5d0)
17+
18+
call dataset%define_data(x, y)
19+
call dataset%set_name("Example")
20+
21+
! Set up the plot
22+
call plt%initialize()
23+
call plt%set_title("Example Plot")
24+
25+
xAxis => plt%get_x_axis()
26+
call xAxis%set_title("X Axis")
27+
28+
yAxis => plt%get_y_axis()
29+
call yAxis%set_title("Y Axis")
30+
31+
! Show the legend
32+
leg => plt%get_legend()
33+
call leg%set_is_visible(.true.)
34+
call leg%set_is_opaque(.false.)
35+
call leg%set_draw_border(.false.)
36+
call leg%set_horizontal_position(LEGEND_LEFT)
37+
call leg%set_vertical_position(LEGEND_CENTER)
38+
39+
! Add the data to the plot
40+
call plt%push(dataset)
41+
42+
! Draw the plot
43+
call plt%draw()
44+
end program

src/fplot_core.f90

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ module fplot_core
7070
public :: LEGEND_RIGHT
7171
public :: LEGEND_TOP
7272
public :: LEGEND_BOTTOM
73+
public :: LEGEND_ARRANGE_VERTICALLY
74+
public :: LEGEND_ARRANGE_HORIZONTALLY
7375
public :: POLAR_THETA_BOTTOM
7476
public :: POLAR_THETA_LEFT
7577
public :: POLAR_THETA_RIGHT
@@ -207,6 +209,12 @@ module fplot_core
207209
character(len = *), parameter :: LEGEND_RIGHT = "right"
208210
!> @brief Defines the legend should be placed at the bottom of the plot.
209211
character(len = *), parameter :: LEGEND_BOTTOM = "bottom"
212+
!> @brief Defines the legend should be arranged such that the column count
213+
!! is minimized.
214+
character(len = *), parameter :: LEGEND_ARRANGE_VERTICALLY = "vertical"
215+
!> @brief Defines the legend should be arranged such that the row count
216+
!! is minimized.
217+
character(len = *), parameter :: LEGEND_ARRANGE_HORIZONTALLY = "horizontal"
210218

211219
! ******************************************************************************
212220
! POLAR PLOT CONSTANTS
@@ -2224,6 +2232,10 @@ module subroutine pa_set_tic_label_fmt(this, x)
22242232
character(len = 20) :: m_vertPosition = LEGEND_TOP
22252233
!> Determines if the legend is visible.
22262234
logical :: m_show = .false.
2235+
!> Determines the legend layout.
2236+
character(len = 20) :: m_layout = LEGEND_ARRANGE_VERTICALLY
2237+
!> Opaque background?
2238+
logical :: m_opaque = .true.
22272239
contains
22282240
!> @brief Gets a value determining if the legend should be drawn inside
22292241
!! the axes border (true), or outside the axes border (false).
@@ -2559,6 +2571,48 @@ module subroutine pa_set_tic_label_fmt(this, x)
25592571
!! @param[in] this The legend object.
25602572
!! @return The GNUPLOT command string.
25612573
procedure, public :: get_command_string => leg_get_command_txt
2574+
!> @brief Gets the layout of the legend.
2575+
!!
2576+
!! @par Syntax
2577+
!! @code{.f90}
2578+
!! character(len = :) pure function get_layout(class(legend) this)
2579+
!! @endcode
2580+
!!
2581+
!! @param[in] this The legend object.
2582+
!! @return The layout type, either @ref LEGEND_ARRANGE_VERTICALLY
2583+
!! or @ref LEGEND_ARRANGE_HORIZONTALLY.
2584+
procedure, public :: get_layout => leg_get_layout
2585+
!> @brief Sets the layout of the legend.
2586+
!!
2587+
!! @par Syntax
2588+
!! @code{.f90}
2589+
!! subroutine set_layout(class(legend) this, character(len = *) x)
2590+
!! @endcode
2591+
!!
2592+
!! @param[in,out] this The legend object.
2593+
!! @param[in] x The layout type, either @ref LEGEND_ARRANGE_VERTICALLY
2594+
!! or @ref LEGEND_ARRANGE_HORIZONTALLY.
2595+
procedure, public :: set_layout => leg_set_layout
2596+
!> @brief Gets a value determining if the legend is to be opaque.
2597+
!!
2598+
!! @par Syntax
2599+
!! @code{.f90}
2600+
!! logical pure function get_is_opaque(class(legend) this)
2601+
!! @endcode
2602+
!!
2603+
!! @param[in] this The legend object.
2604+
!! @return True if the legend is to be opaque; else, false.
2605+
procedure, public :: get_is_opaque => leg_get_opaque
2606+
!> @brief Sets a value determining if the legend is to be opaque.
2607+
!!
2608+
!! @par Syntax
2609+
!! @code{.f90}
2610+
!! subroutine set_is_opaque(class(legend) this)
2611+
!! @endcode
2612+
!!
2613+
!! @param[in,out] this The legend object.
2614+
!! @param[in] x True if the legend is to be opaque; else, false.
2615+
procedure, public :: set_is_opaque => leg_set_opaque
25622616
end type
25632617

25642618
! ------------------------------------------------------------------------------
@@ -2617,6 +2671,26 @@ module function leg_get_command_txt(this) result(txt)
26172671
class(legend), intent(in) :: this
26182672
character(len = :), allocatable :: txt
26192673
end function
2674+
2675+
pure module function leg_get_layout(this) result(rst)
2676+
class(legend), intent(in) :: this
2677+
character(len = :), allocatable :: rst
2678+
end function
2679+
2680+
module subroutine leg_set_layout(this, x)
2681+
class(legend), intent(inout) :: this
2682+
character(len = *), intent(in) :: x
2683+
end subroutine
2684+
2685+
pure module function leg_get_opaque(this) result(rst)
2686+
class(legend), intent(in) :: this
2687+
logical :: rst
2688+
end function
2689+
2690+
module subroutine leg_set_opaque(this, x)
2691+
class(legend), intent(inout) :: this
2692+
logical :: x
2693+
end subroutine
26202694
end interface
26212695

26222696

src/fplot_legend.f90

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,61 @@ module function leg_get_command_txt(this) result(txt)
114114
! Border
115115
call str%append(new_line('a'))
116116
if (this%get_draw_border()) then
117-
call str%append("set key box opaque")
117+
! call str%append("set key box opaque")
118+
call str%append("set key box")
118119
else
119120
call str%append("set key nobox")
120121
end if
121122

123+
! Layout
124+
call str%append(new_line('a'))
125+
call str%append("set key ")
126+
call str%append(this%get_layout())
127+
128+
! Opaque
129+
call str%append(new_line('a'))
130+
call str%append("set key ")
131+
if (this%get_is_opaque()) then
132+
call str%append("opaque")
133+
else
134+
call str%append("noopaque")
135+
end if
136+
122137
! End
123138
txt = str%to_string()
124139
end function
125140

141+
! ------------------------------------------------------------------------------
142+
pure module function leg_get_layout(this) result(rst)
143+
class(legend), intent(in) :: this
144+
character(len = :), allocatable :: rst
145+
rst = trim(this%m_layout)
146+
end function
147+
148+
! ---------------------
149+
module subroutine leg_set_layout(this, x)
150+
class(legend), intent(inout) :: this
151+
character(len = *), intent(in) :: x
152+
if (x == LEGEND_ARRANGE_HORIZONTALLY .or. &
153+
x == LEGEND_ARRANGE_VERTICALLY) &
154+
then
155+
this%m_layout = x
156+
end if
157+
end subroutine
158+
159+
! ------------------------------------------------------------------------------
160+
pure module function leg_get_opaque(this) result(rst)
161+
class(legend), intent(in) :: this
162+
logical :: rst
163+
rst = this%m_opaque
164+
end function
165+
166+
! ---------------------
167+
module subroutine leg_set_opaque(this, x)
168+
class(legend), intent(inout) :: this
169+
logical :: x
170+
this%m_opaque = x
171+
end subroutine
172+
173+
! ------------------------------------------------------------------------------
126174
end submodule

0 commit comments

Comments
 (0)