diff --git a/diagnostic_word/renderers.py b/diagnostic_word/renderers.py
index 2d54b5fab..b5d2e38e4 100644
--- a/diagnostic_word/renderers.py
+++ b/diagnostic_word/renderers.py
@@ -16,6 +16,9 @@
from public_data.domain.impermeabilisation.difference.ImpermeabilisationDifferenceService import (
ImpermeabilisationDifferenceService,
)
+from public_data.infra.consommation.progression.export.ConsoByDeterminantExportTableMapper import (
+ ConsoByDeterminantExportTableMapper,
+)
from public_data.infra.consommation.progression.export.ConsoComparisonExportTableMapper import (
ConsoComparisonExportTableMapper,
)
@@ -197,7 +200,15 @@ def get_context_data(self) -> Dict[str, Any]:
series=annual_total_conso_chart.get_series(), line=False
),
"communes_data_table": add_total_line_column(chart_conso_cities.get_series()),
- "determinants_data_table": add_total_line_column(det_chart.get_series()),
+ "determinants_data_table": ConsoByDeterminantExportTableMapper.map(
+ consommation_progression=PublicDataContainer.consommation_progression_service()
+ .get_by_land(
+ land=diagnostic.land_proxy,
+ start_date=int(diagnostic.analyse_start_date),
+ end_date=int(diagnostic.analyse_end_date),
+ )
+ .consommation
+ ),
# Target 2031
"target_2031_consumed": target_2031_consumption,
"projection_zan_cumulee_ref": round(objective_chart.total_2020, 1),
diff --git a/project/charts/AnnualConsoByDeterminantChart.py b/project/charts/AnnualConsoByDeterminantChart.py
index 868315564..68f8981c2 100644
--- a/project/charts/AnnualConsoByDeterminantChart.py
+++ b/project/charts/AnnualConsoByDeterminantChart.py
@@ -2,27 +2,67 @@
from project.charts.constants import (
CEREMA_CREDITS,
DEFAULT_VALUE_DECIMALS,
- HIGHLIGHT_COLOR,
LEGEND_NAVIGATION_EXPORT,
)
+from public_data.domain.containers import PublicDataContainer
class AnnualConsoByDeterminantChart(ProjectChart):
"""
Graphique en barre de consommation annuelle par destination (habitat, activité, mixte etc.)
- avec une courbe de consommation totale en pointillés.
"""
name = "determinant per year"
+ def _get_series(self):
+ """
+ Génère et retourne la liste des séries à utiliser dans le graphique.
+ """
+ consommation_progression = PublicDataContainer.consommation_progression_service().get_by_land(
+ land=self.project.land_proxy,
+ start_date=self.project.analyse_start_date,
+ end_date=self.project.analyse_end_date,
+ )
+
+ category_to_attr = {
+ "Habitat": "habitat",
+ "Activité": "activite",
+ "Mixte": "mixte",
+ "Route": "route",
+ "Ferré": "ferre",
+ "Inconnu": "non_renseigne",
+ "Total": "total",
+ }
+
+ data = {category: {} for category in category_to_attr.keys()}
+
+ for annual_conso in consommation_progression.consommation:
+ for category, attr in category_to_attr.items():
+ data[category][annual_conso.year] = getattr(annual_conso, attr)
+
+ series = [
+ {
+ "name": determinant,
+ "data": [{"name": year, "y": value} for year, value in data[determinant].items()],
+ **(
+ {"id": "main", "type": "column", "zIndex": 0, "stacking": None, "color": "#CFD1E5"}
+ if determinant == "Total"
+ else {"type": "column", "stacking": "normal", "zIndex": 1}
+ ),
+ }
+ for determinant in data
+ ]
+
+ return series
+
@property
def param(self):
return super().param | {
"chart": {"type": "column"},
"title": {"text": "Par an"},
"yAxis": {
- "title": {"text": "Consommation annuelle (en ha)"},
- "stackLabels": {"enabled": True, "format": "{total:,.1f}"},
+ "title": {"text": "Consommation d'espaces NAF (en ha)"},
+ "stackLabels": {"enabled": True, "format": "{total:,.2f}"},
},
"tooltip": {
"headerFormat": "{point.key}
",
@@ -37,32 +77,13 @@ def param(self):
"align": "right",
"verticalAlign": "middle",
},
- "plotOptions": {
- "column": {
- "stacking": "normal",
- "dataLabels": {"enabled": True, "format": "{point.y:,.1f}"},
- }
- },
- "series": [],
+ "plotOptions": {"series": {"grouping": False, "borderWidth": 0}},
+ "series": self._get_series(),
}
- def get_series(self):
- if not self.series:
- self.series = self.project.get_determinants(group_name=self.group_name)
- return self.series
-
+ # To remove after refactoring
def add_series(self):
- super().add_series()
- if not self.group_name:
- self.add_serie(
- "Total",
- self.project.get_conso_per_year(),
- **{
- "type": "line",
- "color": HIGHLIGHT_COLOR,
- "dashStyle": "ShortDash",
- },
- )
+ pass
class AnnualConsoByDeterminantChartExport(AnnualConsoByDeterminantChart):
diff --git a/project/charts/AnnualTotalConsoChart.py b/project/charts/AnnualTotalConsoChart.py
index dc2f8962c..8fb8af6b4 100644
--- a/project/charts/AnnualTotalConsoChart.py
+++ b/project/charts/AnnualTotalConsoChart.py
@@ -16,7 +16,7 @@ def param(self):
return super().param | {
"chart": {"type": "column"},
"title": {"text": ""},
- "yAxis": {"title": {"text": "Consommé (ha)"}},
+ "yAxis": {"title": {"text": "Consommation d'espaces NAF (ha)"}},
"xAxis": {"type": "category"},
"tooltip": {
"headerFormat": DEFAULT_HEADER_FORMAT,
diff --git a/project/charts/ConsoByDeterminantPieChart.py b/project/charts/ConsoByDeterminantPieChart.py
index a805e9608..c6fe877bb 100644
--- a/project/charts/ConsoByDeterminantPieChart.py
+++ b/project/charts/ConsoByDeterminantPieChart.py
@@ -1,10 +1,43 @@
from project.charts.base_project_chart import ProjectChart
from project.charts.constants import CEREMA_CREDITS
+from public_data.domain.containers import PublicDataContainer
class ConsoByDeterminantPieChart(ProjectChart):
+ """
+ Graphique en secteurs de consommation totale par destination (habitat, activité, mixte etc.)
+ """
+
name = "determinant overview"
+ def _get_series(self):
+ """
+ Génère et retourne la liste des séries à utiliser dans le graphique.
+ """
+ consommation_total = PublicDataContainer.consommation_stats_service().get_by_land(
+ land=self.project.land_proxy,
+ start_date=self.project.analyse_start_date,
+ end_date=self.project.analyse_end_date,
+ )
+
+ category_to_attr = {
+ "Habitat": "habitat",
+ "Activité": "activite",
+ "Mixte": "mixte",
+ "Route": "route",
+ "Ferré": "ferre",
+ "Inconnu": "non_renseigne",
+ }
+
+ data = {category: getattr(consommation_total, attr) for category, attr in category_to_attr.items()}
+
+ return [
+ {
+ "name": "Destinations",
+ "data": [{"name": category, "y": value} for category, value in data.items()],
+ }
+ ]
+
@property
def param(self):
return super().param | {
@@ -12,32 +45,24 @@ def param(self):
"title": {
"text": "Sur la période",
},
- "tooltip": {"enabled": True, "pointFormat": "{point.y:.1f} Ha"},
+ "tooltip": {"enabled": False},
"plotOptions": {
"pie": {
"allowPointSelect": True,
"cursor": "pointer",
"dataLabels": {
+ "distance": 15,
"enabled": True,
- "format": "{point.name} : {point.y:.1f} Ha",
+ "format": "{point.name}
{point.y:.2f} Ha",
},
}
},
- "series": [],
+ "series": self._get_series(),
}
- def __init__(self, *args, **kwargs):
- if "series" in kwargs:
- self.series = kwargs.pop("series")
- super().__init__(*args, **kwargs)
-
- def get_series(self):
- if not self.series:
- self.series = self.project.get_determinants(group_name=self.group_name)
- return {"Destinations": {n: sum(v.values()) for n, v in self.series.items()}}
-
+ # To remove after refactoring
def add_series(self):
- super().add_series(sliced=True)
+ pass
class ConsoByDeterminantPieChartExport(ConsoByDeterminantPieChart):
diff --git a/project/models/project_base.py b/project/models/project_base.py
index 43a010f42..056af3deb 100644
--- a/project/models/project_base.py
+++ b/project/models/project_base.py
@@ -554,27 +554,6 @@ def get_look_a_like(self):
self.save(update_fields=["look_a_like"])
return sorted(lands, key=lambda x: x.name)
- def get_determinants(self, group_name=None):
- from public_data.domain.containers import PublicDataContainer
-
- conso = PublicDataContainer.consommation_progression_service().get_by_land(
- land=self.land_proxy,
- start_date=self.analyse_start_date,
- end_date=self.analyse_end_date,
- )
- data = {"Habitat": {}, "Activité": {}, "Mixte": {}, "Route": {}, "Ferré": {}, "Inconnu": {}}
-
- for annual_conso in conso.consommation:
- year_as_str = str(annual_conso.year)
- data["Habitat"][year_as_str] = annual_conso.habitat
- data["Activité"][year_as_str] = annual_conso.activite
- data["Mixte"][year_as_str] = annual_conso.mixte
- data["Route"][year_as_str] = annual_conso.route
- data["Ferré"][year_as_str] = annual_conso.ferre
- data["Inconnu"][year_as_str] = annual_conso.non_reseigne
-
- return data
-
def get_bilan_conso(self):
"""Return the space consummed between 2011 and 2020 in hectare"""
from public_data.domain.containers import PublicDataContainer
diff --git a/project/templates/project/components/dashboard/consommation.html b/project/templates/project/components/dashboard/consommation.html
index e7889f71b..5a023c160 100644
--- a/project/templates/project/components/dashboard/consommation.html
+++ b/project/templates/project/components/dashboard/consommation.html
@@ -139,38 +139,7 @@
Données brutes, sans calcul
Destination | - {% for year in project.years %} -{{ year }} | - {% endfor %} -Total | -
---|---|---|
{{ determinant_name }} | - {% for year, val in data.items %} -+{{ val|floatformat:1 }} | - {% endfor %} -
+ {{ header }} + | + {% endfor %} +
---|
+ {{ cell }} + | + {% endfor %} +