File size: 3,796 Bytes
fd38574
8206722
fd38574
b466419
 
fd38574
 
 
b466419
 
 
fd38574
b466419
fd38574
b466419
 
fd38574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
fd38574
 
 
 
 
 
 
 
4214624
fd38574
b466419
fd38574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe161c7
 
b466419
 
 
fe161c7
 
 
 
b466419
fe161c7
 
 
 
 
b466419
fe161c7
 
 
b466419
 
 
fe161c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
fe161c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
 
 
fe161c7
 
 
 
 
 
b466419
fe161c7
 
fd38574
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import plotly.graph_objects as go
import plotly.express as px

def create_concentration_chart(
    df,
    period_col,
    metric_col,
    value_col,
    order,
    palette
): 
    fig = go.Figure()

    # Create stacked area traces
    for i, metric in enumerate(order):
        metric_data = df[df[metric_col] == metric]

        # Sort by time and get values
        metric_data = metric_data.sort_values(period_col)
        x_vals = metric_data[period_col]
        y_vals = metric_data[value_col]
        
        # Add area trace
        fig.add_trace(
            go.Scatter(
                x=x_vals,
                y=y_vals,
                name=metric,
                mode='lines',
                line=dict(width=0),
                fill='tonexty' if i > 0 else 'tozeroy',
                fillcolor=palette[i % len(palette)],
                stackgroup='one',
                hovertemplate='<b>%{fullData.name}</b><br>' +
                             'Time: %{x}<br>' +
                             'Value: %{y}<extra></extra>'
            )
        )
    
    fig.update_layout(
        autosize=True,
        font_size=14,
        showlegend=True,
        legend=dict(
            title="Language Concentration",
            orientation="v",
            yanchor="top",
            y=1,
            xanchor="left",
            x=1.02
        ),
        margin=dict(l=60, r=150, t=40, b=60),  # Extra right margin for legend
        plot_bgcolor='white',
        hovermode='x unified'
    )
    
    fig.update_xaxes(
        title_text="",
        showgrid=True,
        gridcolor='lightgray',
        gridwidth=1
    )
    
    fig.update_yaxes(
        title_text="",
        showgrid=True,
        gridcolor='lightgray',
        gridwidth=1
    )
    
    return fig

def create_line_plot(
    df,
    plot_choices,
    color_palette=None
):
    fig = go.Figure()
    
    groups = df['status'].unique()
    
    if color_palette is None:
        color_palette = px.colors.qualitative.Set1
    
    for i, group in enumerate(groups):
        group_data = df[df['status'] == group]
        group_data = group_data.sort_values('period')
        
        x_vals = group_data['period']
        y_vals = group_data[plot_choices["y_col"]]

        if plot_choices.get("y_format") == "percent":
            y_vals = y_vals * 100
        
        fig.add_trace(
            go.Scatter(
                x=x_vals,
                y=y_vals,
                name=group,
                mode='lines',
                line=dict(
                    color=color_palette[i % len(color_palette)],
                    width=3
                ),
                opacity=0.85,
                hovertemplate='<b>%{fullData.name}</b><br>' +
                             'Period: %{x}<br>' +
                             'Value: %{y:.2f}%<extra></extra>' if plot_choices.get("y_format") == "percent" 
                             else '<b>%{fullData.name}</b><br>Period: %{x}<br>Value: %{y}<extra></extra>'
            )
        )
    
    fig.update_layout(
        width=1125,
        height=225,
        showlegend=True,
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.02,
            xanchor="right",
            x=1
        ),
        margin=dict(l=60, r=60, t=60, b=60),
        plot_bgcolor='white',
        hovermode='x unified'
    )
    
    fig.update_xaxes(
        title_text="Period",
        showgrid=False,
        zeroline=False
    )

    y_title = plot_choices["y_col"]
    if plot_choices.get("y_format") == "percent":
        y_title += " (%)"
    
    fig.update_yaxes(
        title_text=y_title,
        showgrid=False,
        zeroline=False,
        type='log' if plot_choices.get("y_log") else 'linear'
    )
    
    return fig