From 08b12dd87f4e13233dbbdb6a12b20620652341e5 Mon Sep 17 00:00:00 2001 From: alexmorev Date: Mon, 4 Nov 2024 20:42:50 +0200 Subject: [PATCH] SXLLCAXAAD-4 / add editable second y-axis label --- ckanext/charts/chart_builders/base.py | 22 ++++++++++++++++------ ckanext/charts/chart_builders/plotly.py | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ckanext/charts/chart_builders/base.py b/ckanext/charts/chart_builders/base.py index cb61d1c..4b5dadf 100644 --- a/ckanext/charts/chart_builders/base.py +++ b/ckanext/charts/chart_builders/base.py @@ -302,20 +302,30 @@ def chart_xlabel_field(self) -> dict[str, Any]: "group": "Styles", "validators": [ self.get_validator("ignore_empty"), - # self.get_validator("default")(" "), self.get_validator("unicode_safe"), ], } - def chart_ylabel_field(self) -> dict[str, Any]: + def chart_ylabel_left_field(self) -> dict[str, Any]: return { - "field_name": "chart_ylabel", - "label": "Chart Y axe label", - "form_placeholder": "Y label", + "field_name": "chart_ylabel_left", + "label": "Chart Y axe left label", + "form_placeholder": "Left Y label", + "group": "Styles", + "validators": [ + self.get_validator("ignore_empty"), + self.get_validator("unicode_safe"), + ], + } + + def chart_ylabel_right_field(self) -> dict[str, Any]: + return { + "field_name": "chart_ylabel_right", + "label": "Chart Y axe right label", + "form_placeholder": "Right Y label", "group": "Styles", "validators": [ self.get_validator("ignore_empty"), - # self.get_validator("default")(" "), self.get_validator("unicode_safe"), ], } diff --git a/ckanext/charts/chart_builders/plotly.py b/ckanext/charts/chart_builders/plotly.py index 532af75..886c13a 100644 --- a/ckanext/charts/chart_builders/plotly.py +++ b/ckanext/charts/chart_builders/plotly.py @@ -101,11 +101,23 @@ def build_line_chart(self) -> Any: else: fig.update_xaxes(title_text=self.settings["x"]) - if chart_ylabel := self.settings.get("chart_ylabel"): - fig.update_yaxes(title_text=chart_ylabel) + if chart_ylabel_left := self.settings.get("chart_ylabel_left"): + fig.update_yaxes(title_text=chart_ylabel_left) else: fig.update_yaxes(title_text=self.settings["y"][0]) + if len(self.settings["y"]) > 1: + if chart_ylabel_right := self.settings.get("chart_ylabel_right"): + fig.update_yaxes( + secondary_y=True, + title_text=chart_ylabel_right, + ) + else: + fig.update_yaxes( + secondary_y=True, + title_text=self.settings["y"][1], + ) + if self.settings.get("invert_x", False): fig.update_xaxes(autorange="reversed") @@ -233,7 +245,8 @@ def get_form_fields(self): self.limit_field(maximum=1000000), self.chart_title_field(), self.chart_xlabel_field(), - self.chart_ylabel_field(), + self.chart_ylabel_left_field(), + self.chart_ylabel_right_field(), self.filter_field(columns), ]