I am trying to plot a candlestick chart which can show tooltips with the date and open, high, low, close values but I am getting just x,y coordinates. I tried tooltip but didn’t get it to work. Tried datainspector which works but only shows x,y coordinates. Help is appreciated!
using GLMakie
using Dates
using Colors
using MarketData
using Statistics
# Theme and Color Setup
set_theme!(theme_light())
bullish_color = colorant"#009E73"
bearish_color = colorant"#D55E00"
# Load and Prepare Market Data
ohlc_data = ohlc
timestamps = timestamp(ohlc_data)
N = length(timestamps)
dates = collect(timestamps)
opens = values(ohlc_data.Open)
highs = values(ohlc_data.High)
lows = values(ohlc_data.Low)
closes = values(ohlc_data.Close)
# Candle body info
body_heights = abs.(closes .- opens)
body_offsets = min.(opens, closes)
colors = [c > o ? bullish_color : bearish_color for (o, c) in zip(opens, closes)]
# Plot Layout
fig = Figure(size = (1000, 500))
ax = Axis(fig[1, 1], title = "Candlestick Chart", ylabel = "Price")
for i in 1:length(timestamps)
lines!(ax, [timestamps[i], timestamps[i]], [lows[i], highs[i]]; color = :black, linewidth = 1.0)
end
barplot!(ax, timestamps, body_heights; offset = body_offsets, color = colors, width = 0.6, strokecolor = :black, strokewidth = 0.5)
DataInspector(fig)
fig
The displayed text can be adjusted by setting plot.inspector_label to a function (plot, index, position) -> "my_label" returning a label
So I would try adding such a function to barplot and lines, I would think the index is already corrected such that you can reference your data with it, but not sure, might also be a lower-level index from GLMakie in which case a transformation on that would be necessary.
You simply need to tell the DataInspector what to do, via the argument inspector_label to barplot!.
Since I don’t have access to your ohlc and am not really sure how to randomly generate this, here’s an example of three named points, where hovering over them also shows the names. There are also line segments connecting the points, and hovering over those shows the relative weights of the endpoints.
using GLMakie
point_names = ["Amy", "Bob", "Carl"]
nb_points = length(point_names)
point_positions = rand(2, nb_points)
function point_inspector_label(plt, index, position)
return "x: $(position[1])\ny: $(position[2])\nname: $(point_names[index])"
end
function line_inspector_label(plt, index, position)
start_point_index = mod1(index - 1, nb_points)
end_point_index = mod1(index, nb_points)
fraction = first((position .- point_positions[:, start_point_index]) ./ (point_positions[:, end_point_index] - point_positions[:, start_point_index])) # Both components should be valid, unless we run into numerical problems
return "$(fraction * 100)% $(point_names[end_point_index]), $((1 - fraction) * 100)% $(point_names[start_point_index])"
end
fig, ax, plt = scatter(point_positions, inspector_label = point_inspector_label)
lines!(ax, [point_positions point_positions[:, 1]], inspector_label = line_inspector_label)
DataInspector(fig)
display(fig)