[Feature Request] Add custom label anchor for Sankey graphs
未关闭
还没有人认领这个 Issue。
feature
P3
- 主要语言
- JavaScript
- 星标
- 18.3k
- 派生
- 2k
- 平均合并
- 2 天 12 小时
- 30 天内合并 PR
- 28
描述
Add the possibility to custom change the anchor of the node text
Current:
After:
Minimum example code:
import dash
from dash import dcc, html, Input, Output
import plotly.graph_objects as go
# Inicializa o app Dash
app = dash.Dash(__name__)
app.title = "Sankey Colorido"
# ----------- Sankey básico -------------
labels = ["A1", "A2", "B1", "B2", "C1", "C2"]
sources = [0, 1, 0, 2, 3, 3]
targets = [2, 3, 3, 4, 4, 5]
values = [8, 4, 2, 8, 4, 2]
fig = go.Figure(go.Sankey(
arrangement="snap",
node=dict(
pad=15,
thickness=20,
line=dict(color="black", width=0.5),
label=labels,
color="blue"
),
link=dict(
source=sources,
target=targets,
value=values
)
))
fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
# ----------- Layout Dash -------------
app.layout = html.Div([
dcc.Graph(
id="sankey-graph",
figure=fig,
style={"width":"100%", "height":"100vh"}
)
])
# ----------- Client-side callback para alinhar labels -------------
# desloca horizontalmente e alinha à direita
app.clientside_callback(
"""
function(figure) {
setTimeout(() => {
const labels = document.querySelectorAll('.sankey .node-label');
labels.forEach(label => {
const transform = label.getAttribute('transform');
const match = /translate\\(([^,]+),([^)]+)\\)/.exec(transform);
if(match){
const y = parseFloat(match[2]);
const x = -15; // deslocamento horizontal
label.setAttribute('transform', `translate(${x},${y})`);
label.setAttribute('text-anchor','end'); // alinhamento à direita
}
});
}, 500); // espera renderização
return figure;
}
""",
Output('sankey-graph', 'figure'),
Input('sankey-graph', 'figure')
)
if __name__ == "__main__":
app.run(debug=True)
The clientside_callback is replaced after user interaction with the graph
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
issue 中没有指出任何仓库文件或测试。首先跟踪 plotly.js 中 Sankey 节点标签的渲染,并将其与使用 clientside callback 的变通方案进行比较;当受支持的自定义标签锚点在没有该变通方案的情况下仍能在图表交互后保持不变时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript
- 领域
- data-visualization
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100