Skip to content

Commit

Permalink
Replace line chart with treemap chart
Browse files Browse the repository at this point in the history
  • Loading branch information
smdsgn committed Dec 6, 2024
1 parent 13f775f commit 4751684
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
from project.charts.base_project_chart import ProjectChart
from project.charts.constants import (
CEREMA_CREDITS,
DEFAULT_HEADER_FORMAT,
DEFAULT_POINT_FORMAT,
DEFAULT_VALUE_DECIMALS,
LEGEND_NAVIGATION_EXPORT,
)
from project.charts.constants import CEREMA_CREDITS, LEGEND_NAVIGATION_EXPORT
from public_data.domain.containers import PublicDataContainer
from public_data.infra.consommation.progression.highchart.ConsoProportionalComparisonMapper import (
ConsoProportionalComparisonMapper,
)


class AnnualConsoProportionalComparisonChart(ProjectChart):
"""
Graphique tree map de consommation d'espaces proportionnelle à la surface des territoires.
"""

name = "conso comparison"

def _get_series(self):
"""
Génère et retourne la liste des séries à utiliser dans le graphique.
"""
return ConsoProportionalComparisonMapper.map(
consommation_stats=PublicDataContainer.consommation_stats_service().get_by_lands(
lands=self.project.comparison_lands_and_self_land(),
start_date=int(self.project.analyse_start_date),
end_date=int(self.project.analyse_end_date),
),
)

@property
def param(self):
return super().param | {
"title": {"text": "Consommation d'espace proportionnelle à la surface des territoires (‰ - pour mille)"},
"yAxis": {"title": {"text": "Proportion (‰ - pour mille)"}},
"xAxis": {"type": "category"},
"tooltip": {
"headerFormat": DEFAULT_HEADER_FORMAT,
"pointFormat": DEFAULT_POINT_FORMAT,
"valueSuffix": " ‰ (pour mille)",
"valueDecimals": DEFAULT_VALUE_DECIMALS,
"tooltip": {"enabled": False},
"colorAxis": {
"minColor": "#FFFFFF",
"maxColor": "#6a6af4",
},
"series": [],
"series": self._get_series(),
}

# To remove after refactoring
def add_series(self):
self.chart["series"] = ConsoProportionalComparisonMapper.map(
land_id_to_highlight=self.project.land.official_id,
consommation_progression=PublicDataContainer.consommation_progression_service().get_by_lands(
lands=self.project.comparison_lands_and_self_land(),
start_date=int(self.project.analyse_start_date),
end_date=int(self.project.analyse_end_date),
),
)
pass


class AnnualConsoProportionalComparisonChartExport(AnnualConsoProportionalComparisonChart):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ class ConsommationStatistics:
end_date: int
total: float
total_percent: float
per_mille_of_area: float
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
from project.charts.constants import HIGHLIGHT_COLOR
from public_data.domain.consommation.entity import ConsommationCollection
from public_data.domain.consommation.entity import ConsommationStatistics


class ConsoProportionalComparisonMapper:
@staticmethod
def map(land_id_to_highlight: str, consommation_progression: list[ConsommationCollection]):
highlight_style = {
"color": HIGHLIGHT_COLOR,
"dashStyle": "ShortDash",
"lineWidth": 4,
}
default_style = {}
def map(consommation_stats: list[ConsommationStatistics]):
return [
{
"name": land_conso.land.name,
"type": "treemap",
"layoutAlgorithm": "squarified",
"clip": False,
"dataLabels": {
"enabled": True,
"format": "{point.name}<br />{point.value:.2f} ‰",
"style": {
"fontWeight": "bold",
"textOutline": "none",
},
},
"borderWidth": 0,
"data": [
{
"name": annual_conso.year,
"y": annual_conso.per_mille_of_area,
"name": land_conso.land.name,
"value": land_conso.per_mille_of_area,
"colorValue": land_conso.per_mille_of_area,
}
for annual_conso in land_conso.consommation
for land_conso in consommation_stats
],
"legendIndex": land_conso.land.official_id != land_id_to_highlight,
**(highlight_style if land_conso.land.official_id == land_id_to_highlight else default_style),
}
for land_conso in consommation_progression
]
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def get_by_land(
end_date=end_date,
total=conso_stats.total / 10000,
total_percent=conso_stats.total_percent,
per_mille_of_area=conso_stats.total / 10000 / land.area * 1000,
)

def get_by_lands(
Expand Down

0 comments on commit 4751684

Please sign in to comment.