/ Plotting / src / utils.jl
utils.jl
  1  using .ect.Instruments: compactnum as cn
  2  
  3  makefig() = begin
  4      Figure(; size=(1900, 900))
  5  end
  6  
  7  @doc """ Deregisters interactions from an axis
  8  
  9  $(TYPEDSIGNATURES)
 10  
 11  The function iterates over all interactions of the given axis and deregisters them, except for those specified in the `except` parameter.
 12  The `except` parameter is a tuple containing the interactions to be preserved.
 13  By default, `:dragpan` and `:scrollzoom` interactions are preserved.
 14  """
 15  function deregister_interactions!(ax, except=(:dragpan, :scrollzoom))
 16      for i in keys(interactions(ax))
 17          i ∈ except && continue
 18          deregister_interaction!(ax, i)
 19      end
 20  end
 21  
 22  @doc """ Adjusts the position of a tooltip on a plot
 23  
 24  $(TYPEDSIGNATURES)
 25  
 26  The function calculates the position of a tooltip based on the index of a data point in a plot.
 27  It uses the `pos_func1` and `pos_func2` parameters to adjust the position of the tooltip.
 28  The `pos_func1` parameter is a function that calculates the position based on the data point's position.
 29  The `pos_func2` parameter is a function that adjusts the tooltip's position based on the calculated position and the projected position.
 30  """
 31  function tooltip_position!(
 32      inspector,
 33      plot,
 34      idx;
 35      vertices=4,
 36      pos_func1=((pos) -> ((pos[1] + pos[2]) / 2)),
 37      pos_func2=((a, b) -> b),
 38  )
 39      # Get the scene BarPlot lives in
 40      scene = parent_scene(plot)
 41      true_idx = div(idx - 1, vertices) + 1
 42      # fetch the position of the candle mesh
 43      pos = plot[1][][true_idx]
 44      proj_pos = shift_project(scene, plot, pos_func1(pos))
 45      update_tooltip_alignment!(inspector, pos_func2(pos, proj_pos))
 46      true_idx
 47  end
 48  
 49  @doc """ Sets the content and visibility of a tooltip
 50  
 51  $(TYPEDSIGNATURES)
 52  
 53  The function sets the text content of a tooltip and makes it visible.
 54  The `text` parameter is the content to be displayed in the tooltip.
 55  The `size` parameter sets the size of the tooltip's triangle, with a default value of 10.0.
 56  """
 57  function tooltip_text!(inspector, text; size=10.0)
 58      # Set the tooltip content
 59      tt = inspector.plot
 60      tt.text[] = text
 61      tt.triangle_size = size
 62      # Show the tooltip
 63      tt.visible[] = true
 64  end
 65  
 66  @doc """ Formats the timestamps for the X axis
 67  
 68  $(TYPEDSIGNATURES)
 69  
 70  The function takes a `start_date` and a time frame `tf` as parameters.
 71  It returns a function that, given a set of timestamps `t`, returns an array of formatted strings representing the timestamps.
 72  The formatting is done by adding the product of the time frame period and the rounded integer value of each timestamp to the start date.
 73  """
 74  function makexticks(start_date, tf)
 75      (t) -> [string(start_date + tf.period * round(Int, tt)) for tt in t]
 76  end
 77  
 78  @doc """ Formats the Y axis values
 79  
 80  $(TYPEDSIGNATURES)
 81  
 82  The function takes a set of values `t` and returns an array of compactly formatted strings representing these values.
 83  The formatting is done using the `compactnum` function from the `Instruments` module.
 84  """
 85  ytickscompact(t) = cn.(t)
 86  
 87  # @doc "Formats the Y axis values"
 88  # function makeyticks()
 89  #     (t) -> cn.(t)
 90  # end
 91  
 92  @doc """ Retrieves the price axis from a figure """
 93  _price_ax(fig::Figure) = fig.attributes[:price_ax][]
 94  @doc """ Creates a price axis in a figure
 95  
 96  $(TYPEDSIGNATURES)
 97  
 98  The function creates a new axis in the figure with the specified parameters.
 99  The `xticksargs` parameter is used to format the x-axis ticks.
100  The `title` parameter sets the title of the axis, defaulting to "OHLC".
101  The `ylabel` parameter sets the label of the y-axis, defaulting to "Price".
102  """
103  function makepriceax(fig; xticksargs, title="OHLC", ylabel="Price")
104      ax = Axis(
105          fig[1, 1];
106          xtickformat=makexticks(xticksargs...),
107          ytickformat=ytickscompact,
108          title,
109          xlabel="Time",
110          xaxisposition=:top,
111          ylabel,
112      )
113      fig.attributes[:price_ax] = ax
114  end
115  
116  @doc """ Creates an axis in a figure and applies default settings
117  
118  $(TYPEDSIGNATURES)
119  
120  The function creates an axis in the specified position of the figure, hides its spines and decorations, and deregisters its interactions.
121  The `idx` parameter specifies the position of the axis in the figure, defaulting to (1, 1).
122  """
123  function axis!(fig, idx=(1, 1))
124      ax = Axis(fig[idx...];)
125      hidespines!(ax)
126      hidedecorations!(ax)
127      deregister_interactions!(ax)
128      ax
129  end