File size: 7,013 Bytes
4eafb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from pandas import DataFrame

from plotly.subplots import make_subplots
from plotly.graph_objects import Figure, Pie, Bar, Scatter


def draw_report_figure(df: DataFrame) -> Figure:
    figure_specs = [
        [{"type": "xy"}, {"type": "xy"}],
        [{"type": "domain"}, None],
    ]

    fig = make_subplots(
        rows=2,
        cols=2,
        specs=figure_specs,
        subplot_titles=(
            "Carbon Emission by Category",
            "Cumulative Emission %",
            "Emission Distribution",
        ),
    )

    # Pie chart settings
    pie_pull = [0.15 if x == min(df["Value"]) else 0.0 for x in df["Value"]]
    fig.add_trace(
        Pie(
            values=df["Value"],
            labels=df["Category"],
            hole=0.3,
            pull=pie_pull,
            name="Emission Distribution",
            marker={"colors": ["#6DA34D", "#81C3D7", "#FFC857"]},
        ),
        row=2,
        col=1,
    )

    # Bar chart for emissions by category
    fig.add_trace(
        Bar(
            x=df["Category"],
            y=df["Value"],
            name="Carbon Emission (kgCO2)",
            marker_color=["#6DA34D", "#81C3D7", "#FFC857"],
        ),
        row=1,
        col=1,
    )

    # Annotation for highest emission
    fig.add_annotation(
        x=df["Category"][df["Value"].idxmax()],
        y=df["Value"].max(),
        text="Highest Emission",
        showarrow=True,
        arrowhead=1,
        ax=0,
        ay=-40,
        row=1,
        col=1,
    )

    # Cumulative line chart
    cumulative_percentage = (df["Value"].cumsum() / df["Value"].sum()) * 100
    fig.add_trace(
        Scatter(
            x=df["Category"],
            y=cumulative_percentage,
            name="Cumulative %",
            mode="lines+markers",
            line=dict(color="#333333", dash="dash"),
        ),
        row=1,
        col=2,
    )

    # Update layout for axes and overall layout
    fig.update_layout(
        title_text=f"Carbon Footprint of {df['Name'][0]}",
        plot_bgcolor="white",
        legend_title_text="Breakdown",
        xaxis_title="Emission Category",
        yaxis_title="Carbon Emission (kgCO2)",
        yaxis=dict(
            linecolor="black",
            showline=True,
            ticks="outside",
            mirror=True,
            gridcolor="lightgrey",
        ),
        yaxis2=dict(
            title="Cumulative Percentage",
            side="right",
            showgrid=False,
        ),
        legend=dict(
            x=1,  # Horizontal position (1 for right)
            y=0,  # Vertical position (0 for bottom)
            xanchor="right",
            yanchor="bottom",
            orientation="h",  # Horizontal layout for compactness
        ),
    )

    fig.update_xaxes(
        linecolor="black",
        ticks="outside",
        showline=True,
        mirror=True,
    )
    fig.update_yaxes(
        linecolor="black",
        showline=True,
        ticks="outside",
        mirror=True,
        gridcolor="lightgrey",
    )

    return fig


def draw_historic_figure(df: DataFrame) -> Figure:
    # Create subplots with 2 rows and 2 columns
    fig = make_subplots(
        rows=2,
        cols=2,
        subplot_titles=(
            "Energy Usage by Company",
            "Waste Generated by Company",
            "Business Travel by Company",
            "Total Carbon Footprint by Company",
        ),
    )

    # Add gradient-filled area traces for each metric
    fig.add_trace(
        Scatter(
            x=df["Name"],
            y=df["Energy Usage"],
            mode="lines",
            fill="tozeroy",
            line=dict(color="blue"),
            fillcolor="rgba(31, 119, 180, 0.5)",  # Gradient fill for blue
            name="Energy Usage",
        ),
        row=1,
        col=1,
    )

    fig.add_trace(
        Scatter(
            x=df["Name"],
            y=df["Waste Generated"],
            mode="lines",
            fill="tozeroy",
            line=dict(color="orange"),
            fillcolor="rgba(255, 127, 14, 0.5)",  # Gradient fill for orange
            name="Waste Generated",
        ),
        row=1,
        col=2,
    )

    fig.add_trace(
        Scatter(
            x=df["Name"],
            y=df["Business Travel"],
            mode="lines",
            fill="tozeroy",
            line=dict(color="green"),
            fillcolor="rgba(44, 160, 44, 0.5)",  # Gradient fill for green
            name="Business Travel",
        ),
        row=2,
        col=1,
    )

    # Calculate each company's total carbon footprint as the sum of the three metrics
    df["Carbon Footprint"] = (
        df["Energy Usage"] + df["Waste Generated"] + df["Business Travel"]
    )

    # Add a line trace for the total sum of each metric
    fig.add_trace(
        Scatter(
            x=df["Name"],
            y=df["Carbon Footprint"],
            mode="lines+markers",
            line=dict(color="black", dash="dash"),
            name="Total Carbon Footprint",
        ),
        row=2,
        col=2,
    )

    # Update layout for titles, legends, and aesthetics
    fig.update_layout(
        title="Company Metrics with Total Carbon Footprint",
        barmode="group",  # Group bars by category
        template="plotly_white",
        showlegend=True,
        height=600,
        width=1000,
        legend=dict(x=1.05, y=1),  # Adjust legend position outside plot for clarity
    )

    # Add axis labels to each subplot
    fig.update_xaxes(title_text="Company", row=1, col=1)
    fig.update_yaxes(title_text="Energy Usage", row=1, col=1)

    fig.update_xaxes(title_text="Company", row=1, col=2)
    fig.update_yaxes(title_text="Waste Generated", row=1, col=2)

    fig.update_xaxes(title_text="Company", row=2, col=1)
    fig.update_yaxes(title_text="Business Travel", row=2, col=1)

    fig.update_xaxes(title_text="Company", row=2, col=2)
    fig.update_yaxes(title_text="Carbon Footprint (total)", row=2, col=2)

    return fig


def make_dataframe(
    company_name: str,
    avg_electric_bill: float,
    avg_gas_bill: float,
    avg_transport_bill: float,
    monthly_waste_generated: float,
    recycled_waste_percent: float,
    annual_travel_kms: float,
    fuel_efficiency: float,
) -> DataFrame:
    energy_usage = (
        (avg_electric_bill * 12 * 5e-4)
        + (avg_gas_bill * 12 * 5.3e-3)
        + (avg_transport_bill * 12 * 2.32)
    )
    waste_generated = monthly_waste_generated * 12 * 0.57 - recycled_waste_percent
    business_travel = annual_travel_kms * 1 / fuel_efficiency * 2.31

    return DataFrame(
        {
            "Name": company_name,
            "Category": ["Energy Usage", "Waste Generated", "Business Travel"],
            "Value": [energy_usage, waste_generated, business_travel],
        }
    )


def dataframe_to_dict(df: DataFrame) -> dict:
    return {
        "Name": df["Name"][0],
        "Energy Usage": df["Value"][0],
        "Waste Generated": df["Value"][1],
        "Business Travel": df["Value"][2],
    }