Skip to content

Commit

Permalink
Merge pull request #15 from jchristopherson/legend_updates
Browse files Browse the repository at this point in the history
Added additional functionality to the legend type
  • Loading branch information
jchristopherson authored Jun 30, 2023
2 parents b074da1 + ceffe94 commit 9d3170e
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
6 changes: 5 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ target_link_libraries(fplot_fill_2 fplot)

# Example 31
add_executable(fplot_histogram_1 fplot_histogram_1.f90)
target_link_libraries(fplot_histogram_1 fplot)
target_link_libraries(fplot_histogram_1 fplot)

# Example 32
add_executable(fplot_legend_1 fplot_legend_1.f90)
target_link_libraries(fplot_legend_1 fplot)
44 changes: 44 additions & 0 deletions examples/fplot_legend_1.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
program example
use fplot_core
use iso_fortran_env
implicit none

! Local Variables & Parameters
integer(int32), parameter :: npts = 1000
real(real64), dimension(npts) :: x, y
type(plot_2d) :: plt
type(plot_data_2d) :: dataset
class(plot_axis), pointer :: xAxis, yAxis
type(legend), pointer :: leg

! Build a data set to plot
x = linspace(0.0d0, 10.0d0, npts)
y = exp(-0.5d0 * x) * sin(10.0d0 * x - 0.5d0)

call dataset%define_data(x, y)
call dataset%set_name("Example")

! Set up the plot
call plt%initialize()
call plt%set_title("Example Plot")

xAxis => plt%get_x_axis()
call xAxis%set_title("X Axis")

yAxis => plt%get_y_axis()
call yAxis%set_title("Y Axis")

! Show the legend
leg => plt%get_legend()
call leg%set_is_visible(.true.)
call leg%set_is_opaque(.false.)
call leg%set_draw_border(.false.)
call leg%set_horizontal_position(LEGEND_LEFT)
call leg%set_vertical_position(LEGEND_CENTER)

! Add the data to the plot
call plt%push(dataset)

! Draw the plot
call plt%draw()
end program
74 changes: 74 additions & 0 deletions src/fplot_core.f90
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ module fplot_core
public :: LEGEND_RIGHT
public :: LEGEND_TOP
public :: LEGEND_BOTTOM
public :: LEGEND_ARRANGE_VERTICALLY
public :: LEGEND_ARRANGE_HORIZONTALLY
public :: POLAR_THETA_BOTTOM
public :: POLAR_THETA_LEFT
public :: POLAR_THETA_RIGHT
Expand Down Expand Up @@ -207,6 +209,12 @@ module fplot_core
character(len = *), parameter :: LEGEND_RIGHT = "right"
!> @brief Defines the legend should be placed at the bottom of the plot.
character(len = *), parameter :: LEGEND_BOTTOM = "bottom"
!> @brief Defines the legend should be arranged such that the column count
!! is minimized.
character(len = *), parameter :: LEGEND_ARRANGE_VERTICALLY = "vertical"
!> @brief Defines the legend should be arranged such that the row count
!! is minimized.
character(len = *), parameter :: LEGEND_ARRANGE_HORIZONTALLY = "horizontal"

! ******************************************************************************
! POLAR PLOT CONSTANTS
Expand Down Expand Up @@ -2224,6 +2232,10 @@ module subroutine pa_set_tic_label_fmt(this, x)
character(len = 20) :: m_vertPosition = LEGEND_TOP
!> Determines if the legend is visible.
logical :: m_show = .false.
!> Determines the legend layout.
character(len = 20) :: m_layout = LEGEND_ARRANGE_VERTICALLY
!> Opaque background?
logical :: m_opaque = .true.
contains
!> @brief Gets a value determining if the legend should be drawn inside
!! the axes border (true), or outside the axes border (false).
Expand Down Expand Up @@ -2559,6 +2571,48 @@ module subroutine pa_set_tic_label_fmt(this, x)
!! @param[in] this The legend object.
!! @return The GNUPLOT command string.
procedure, public :: get_command_string => leg_get_command_txt
!> @brief Gets the layout of the legend.
!!
!! @par Syntax
!! @code{.f90}
!! character(len = :) pure function get_layout(class(legend) this)
!! @endcode
!!
!! @param[in] this The legend object.
!! @return The layout type, either @ref LEGEND_ARRANGE_VERTICALLY
!! or @ref LEGEND_ARRANGE_HORIZONTALLY.
procedure, public :: get_layout => leg_get_layout
!> @brief Sets the layout of the legend.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine set_layout(class(legend) this, character(len = *) x)
!! @endcode
!!
!! @param[in,out] this The legend object.
!! @param[in] x The layout type, either @ref LEGEND_ARRANGE_VERTICALLY
!! or @ref LEGEND_ARRANGE_HORIZONTALLY.
procedure, public :: set_layout => leg_set_layout
!> @brief Gets a value determining if the legend is to be opaque.
!!
!! @par Syntax
!! @code{.f90}
!! logical pure function get_is_opaque(class(legend) this)
!! @endcode
!!
!! @param[in] this The legend object.
!! @return True if the legend is to be opaque; else, false.
procedure, public :: get_is_opaque => leg_get_opaque
!> @brief Sets a value determining if the legend is to be opaque.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine set_is_opaque(class(legend) this)
!! @endcode
!!
!! @param[in,out] this The legend object.
!! @param[in] x True if the legend is to be opaque; else, false.
procedure, public :: set_is_opaque => leg_set_opaque
end type

! ------------------------------------------------------------------------------
Expand Down Expand Up @@ -2617,6 +2671,26 @@ module function leg_get_command_txt(this) result(txt)
class(legend), intent(in) :: this
character(len = :), allocatable :: txt
end function

pure module function leg_get_layout(this) result(rst)
class(legend), intent(in) :: this
character(len = :), allocatable :: rst
end function

module subroutine leg_set_layout(this, x)
class(legend), intent(inout) :: this
character(len = *), intent(in) :: x
end subroutine

pure module function leg_get_opaque(this) result(rst)
class(legend), intent(in) :: this
logical :: rst
end function

module subroutine leg_set_opaque(this, x)
class(legend), intent(inout) :: this
logical :: x
end subroutine
end interface


Expand Down
50 changes: 49 additions & 1 deletion src/fplot_legend.f90
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,61 @@ module function leg_get_command_txt(this) result(txt)
! Border
call str%append(new_line('a'))
if (this%get_draw_border()) then
call str%append("set key box opaque")
! call str%append("set key box opaque")
call str%append("set key box")
else
call str%append("set key nobox")
end if

! Layout
call str%append(new_line('a'))
call str%append("set key ")
call str%append(this%get_layout())

! Opaque
call str%append(new_line('a'))
call str%append("set key ")
if (this%get_is_opaque()) then
call str%append("opaque")
else
call str%append("noopaque")
end if

! End
txt = str%to_string()
end function

! ------------------------------------------------------------------------------
pure module function leg_get_layout(this) result(rst)
class(legend), intent(in) :: this
character(len = :), allocatable :: rst
rst = trim(this%m_layout)
end function

! ---------------------
module subroutine leg_set_layout(this, x)
class(legend), intent(inout) :: this
character(len = *), intent(in) :: x
if (x == LEGEND_ARRANGE_HORIZONTALLY .or. &
x == LEGEND_ARRANGE_VERTICALLY) &
then
this%m_layout = x
end if
end subroutine

! ------------------------------------------------------------------------------
pure module function leg_get_opaque(this) result(rst)
class(legend), intent(in) :: this
logical :: rst
rst = this%m_opaque
end function

! ---------------------
module subroutine leg_set_opaque(this, x)
class(legend), intent(inout) :: this
logical :: x
this%m_opaque = x
end subroutine

! ------------------------------------------------------------------------------
end submodule

0 comments on commit 9d3170e

Please sign in to comment.