我正在从 Julia 的角度尝试使用 Dash 来创建仪表板。我有一个包含以下代码的工作图。
using DataFrames, XLSX, Dates, Dash, DashHtmlComponents, DashCoreComponents, StatsPlots, Indicators
df = DataFrame(XLSX.readtable("file.xlsx", "Price", infer_eltypes=true)...)
filter!(row -> row.Date >= Date(2018,12,1), df)
sort!(df)
app = dash()
app.layout = html_div() do
html_h1("Price graph"),
dcc_graph(
id = "example-graph-1",
figure = (
data = [
(x = df.Date, y = df.Closeprice, type = "line", name = "Closeprice"),
(x = df.Date, y = sma(df.Closeprice, n=50), type = "line", name = "SMA50"),
(x = df.Date, y = sma(df.Closeprice, n=200), type = "line", name = "SMA200")
],
layout = (title = "Simple Moving Average", barmode="group")
)
)
end
run_server(app, "0.0.0.0", debug=true)
现在我想在该图的 y 轴上的某些点处创建一些水平线。我尝试查看文档,但没有找到任何关于如何创建水平线的好示例。 当我使用 Statsplot 库时,我可以使用 hline 和 vline 创建不同的线,但这似乎在 Dash 中不起作用。任何指导将不胜感激。
最佳答案
您可以向布局添加形状。
简化的可重现示例:
using DataFrames, Dash, DashHtmlComponents, DashCoreComponents
df = DataFrame(Date = [1, 2, 3, 4], Closeprice = [1, 3, 5, 8])
app = dash()
app.layout = html_div() do
html_h1("Price graph"),
dcc_graph(
id = "example-graph-1",
figure = (
data = [(x = df.Date, y = df.Closeprice, type = "line", name = "Closeprice")],
layout = (
title = "Simple Moving Average",
barmode = "group",
shapes = [(
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 4,
y1 = 4,
yref = "y",
line = (color = "Red", width = 2)
)],
),
),
)
end
run_server(app, "0.0.0.0", debug = true)
结果:
使用xref = "paper"
以便线条跨越整个图表。
By default, text annotations have xref and yref set to "x" and "y", respectively, meaning that their x/y coordinates are with respect to the axes of the plot. This means that panning the plot will cause the annotations to move. Setting xref and/or yref to "paper" will cause the x and y attributes to be interpreted in paper coordinates.
https://plotly.com/python/text-and-annotations/
以上引用来自 Python 文档,但同样适用于此处。
关于plotly - 在 Dash for Julia 中添加水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68314851/