File size: 2,289 Bytes
fb4ea38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import plotly.graph_objects as go
import streamlit


@streamlit.cache_data
def plot_sankey(data, output_file='sankey_diagram_oct.html', i=0, do='до'):
    labels = []
    sources = []
    targets = []
    values = []

    # Сбор данных для диаграммы
    for event, next_events in data.items():
        # Добавляем основное событие в метки
        if event not in labels:
            labels.append(event)

        current_event_index = labels.index(event)

        for next_event, (user_count, percentage) in next_events.items():
            if next_event not in labels:
                labels.append(next_event)

            next_event_index = labels.index(next_event)
            sources.append(current_event_index)
            targets.append(next_event_index)
            values.append(user_count)

    # Создание диаграммы
    fig = go.Figure(data=[go.Sankey(
        valueformat= ".0f",
        valuesuffix="TWh",

        # font = dict(family='Arial',
        #             size=14,
        #             color='black'),
        node=dict(
            # family='Arial',
            pad=15,
            thickness=20,
            line=dict(color="black", width=0.1),
            label=labels,
            # font = dict(size=15, color="black")
        ),
        link=dict(
            # family = 'Arial',
            source=sources,
            target=targets,
            value=values,
            label=[f"{user_count} ({percentage})" for user_count, percentage in
                   zip(values, [data[labels[sources[i]]][labels[targets[i]]][1] for i in range(len(sources))])],
        )
    )])

    return fig.update_layout(title_text=f"Воронка сервиса Финансовое здоровье за октябрь 2024, часть {i} ({do} ГЭС ФЗ)",
                      font_size=10,
                             font= dict(size=10, color='white'),
                             plot_bgcolor='white',
                             paper_bgcolor='white')

    #     Сохранение диаграммы в HTML файл
    # fig.write_html(f'test_sanki_oct_part{i}.html')
    #     print(f"Диаграмма успешно сохранена в файл: test_sanki.html")
    # fig.show()