1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| import plotly.graph_objects as go
labels = [
"เข้าเว็บ",
"สมัครฟรี",
"ทิ้งตะกร้า",
"ดู FAQ",
"ออกทันที",
"แปลงเป็นจ่ายเงิน",
"กลับมาอีกครั้ง",
"เลิกใช้งาน",
]
sources = [0, 0, 0, 0, 1, 1, 1]
targets = [1, 2, 3, 4, 5, 6, 7]
values = [420, 280, 200, 100, 260, 100, 60]
fig = go.Figure(
data=[
go.Sankey(
arrangement="snap",
node=dict(
pad=18,
thickness=18,
line=dict(color="rgba(0,0,0,0.2)", width=1),
label=labels,
color=[
"#4C78A8",
"#F58518",
"#E45756",
"#72B7B2",
"#54A24B",
"#EECA3B",
"#B279A2",
"#FF9DA6",
],
),
link=dict(
source=sources,
target=targets,
value=values,
color=[
"rgba(76,120,168,0.45)",
"rgba(229,87,86,0.45)",
"rgba(114,183,178,0.45)",
"rgba(84,162,75,0.45)",
"rgba(245,133,24,0.45)",
"rgba(238,202,59,0.45)",
"rgba(178,121,162,0.45)",
],
),
)
]
)
fig.update_layout(
title=dict(text="โฟลว์ลูกค้าหลังการเข้าชม (Plotly Sankey)", x=0.5, font=dict(size=18)),
font=dict(size=12, color="#222"),
margin=dict(l=10, r=10, t=40, b=10),
)
# HTML แบบโต้ตอบสำหรับแชร์ในเบราว์เซอร์
fig.write_html("static/images/visualize/advanced/sankey-diagram.html", include_plotlyjs="cdn")
# รูปภาพสำหรับเอกสาร (ต้องติดตั้ง kaleido: pip install kaleido)
fig.write_image("static/images/visualize/advanced/sankey-diagram.png", scale=2)
plt.show()
|