Skip to content

Commit

Permalink
Determinant charts data table
Browse files Browse the repository at this point in the history
  • Loading branch information
smdsgn committed Dec 5, 2024
1 parent 1bdf95a commit 7a7b58e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 33 deletions.
33 changes: 1 addition & 32 deletions project/templates/project/components/dashboard/consommation.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,38 +139,7 @@ <h6 class="fr-mt-2w">Calcul</h6>
<p class="fr-text--sm">Données brutes, sans calcul</p>

<h6 class="fr-mt-2w">Données</h6>
<div class="fr-table fr-table--bg-whiteed">
<div class="fr-table__wrapper">
<div class="fr-table__container">
<div class="fr-table__content">
<table class="table-last-column-bold table-last-row-bold">
<caption>
Consommation d'espace annuelle sur le territoire par destination (en ha)
</caption>
<thead>
<tr>
<th scope="col" class="fr-cell--fixed">Destination</th>
{% for year in project.years %}
<th scope="col" class="fr-cell--right">{{ year }}</th>
{% endfor %}
<th scope="col" class="fr-cell--right">Total</th>
</tr>
</thead>
<tbody>
{% for determinant_name, data in data_determinant.items %}
<tr>
<th scope="row" class="fr-cell--fixed">{{ determinant_name }}</th>
{% for year, val in data.items %}
<td class="fr-cell--right">+{{ val|floatformat:1 }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{{ determinant_data_table }}
</div>
</div>
</div>
Expand Down
13 changes: 12 additions & 1 deletion project/views/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
from public_data.domain.impermeabilisation.difference.ImperSolTableMapper import (
ImperSolTableMapper,
)
from public_data.infra.consommation.progression.table.ConsoByDeterminantTableMapper import (
ConsoByDeterminantTableMapper,
)
from public_data.infra.consommation.progression.table.ConsoComparisonTableMapper import (
ConsoComparisonMapper,
)
Expand Down Expand Up @@ -171,7 +174,15 @@ def get_context_data(self, **kwargs):
"population_conso_comparison_chart": charts.PopulationConsoComparisonChart(project),
# data tables
"annual_conso_data_table": annual_conso_data_table,
"data_determinant": add_total_line_column(det_chart.get_series()),
"determinant_data_table": ConsoByDeterminantTableMapper.map(
consommation_progression=PublicDataContainer.consommation_progression_service()
.get_by_land(
land=project.land_proxy,
start_date=int(project.analyse_start_date),
end_date=int(project.analyse_end_date),
)
.consommation
),
"comparison_table": ConsoComparisonMapper.map(
consommation_progression=PublicDataContainer.consommation_progression_service().get_by_lands(
lands=project.comparison_lands_and_self_land(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.template.loader import render_to_string

from public_data.domain.consommation.entity import ConsommationCollection


class ConsoByDeterminantTableMapper:
@staticmethod
def map(consommation_progression: list[ConsommationCollection]):
category_to_attr = {
"Habitat": "habitat",
"Activité": "activite",
"Mixte": "mixte",
"Route": "route",
"Ferré": "ferre",
"Inconnu": "non_reseigne",
"Total": "total",
}

headers = ["Destination"] + [str(conso.year) for conso in consommation_progression] + ["Total"]

data = []
for category in category_to_attr:
category_values = [getattr(conso, category_to_attr[category]) for conso in consommation_progression]
category_total = sum(category_values)
# On arrondit ensuite pour ne pas fausser le total
category_values_rounded = [round(value, 2) for value in category_values]
category_total_rounded = round(category_total, 2)
data.append([category] + category_values_rounded + [category_total_rounded])

return render_to_string(
"public_data/partials/conso_by_determinant_table.html",
{
"headers": headers,
"data": data,
},
)

return render_to_string(
"public_data/partials/conso_by_determinant_table.html",
{
"headers": headers,
"data": data,
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="fr-table fr-table--bordered">
<div class="fr-table__wrapper">
<div class="fr-table__container">
<div class="fr-table__content">
<table>
<caption>
Consommation d'espaces NAF sur le territoire par an et par destination (en ha)
</caption>
<thead>
<tr>
{% for header in headers %}
<th scope="col" class="{% if forloop.first %}fr-text--bold fr-cell--fixed{% else %}fr-cell--right{% endif %} {% if forloop.last %}fr-text--bold{% endif %}">
{{ header }}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in data %}
<tr class="{% if forloop.last %}fr-text--bold{% endif %}">
{% for cell in row %}
<td class="{% if forloop.first %}fr-text--bold fr-cell--fixed{% else %}fr-cell--right{% endif %} {% if forloop.last %}fr-text--bold{% endif %}">
{{ cell }}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>

0 comments on commit 7a7b58e

Please sign in to comment.