From 0b8490325100dcc4b886bb5796b06564102f2179 Mon Sep 17 00:00:00 2001 From: cirun Date: Tue, 23 Jul 2024 19:37:17 +0200 Subject: [PATCH] LLCAXCHZF-54/implement option to invert x and y axes --- ckanext/charts/chart_builders/base.py | 24 ++++++++++++++++++++++++ ckanext/charts/chart_builders/chartjs.py | 13 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ckanext/charts/chart_builders/base.py b/ckanext/charts/chart_builders/base.py index 7677a63..58d4690 100644 --- a/ckanext/charts/chart_builders/base.py +++ b/ckanext/charts/chart_builders/base.py @@ -469,6 +469,30 @@ def sort_y_field(self) -> dict[str, Any]: ], } + def invert_x_field(self) -> dict[str, Any]: + return { + "field_name": "invert_x", + "label": "Invert X-axis", + "form_snippet": "chart_checkbox.html", + "group": "Data", + "validators": [ + self.get_validator("default")(False), + self.get_validator("boolean_validator"), + ], + } + + def invert_y_field(self) -> dict[str, Any]: + return { + "field_name": "invert_y", + "label": "Invert Y-axis", + "form_snippet": "chart_checkbox.html", + "group": "Data", + "validators": [ + self.get_validator("default")(False), + self.get_validator("boolean_validator"), + ], + } + def log_x_field(self) -> dict[str, Any]: return { "field_name": "log_x", diff --git a/ckanext/charts/chart_builders/chartjs.py b/ckanext/charts/chart_builders/chartjs.py index 4a94dee..3ebb39a 100644 --- a/ckanext/charts/chart_builders/chartjs.py +++ b/ckanext/charts/chart_builders/chartjs.py @@ -115,7 +115,16 @@ def to_json(self) -> str: data: dict[str, Any] = { "type": "line", "data": {"labels": self.df[self.settings["x"]].to_list()}, - "options": self.settings, + "options": { + "scales": { + "x": { + "reverse": self.settings.get("invert_x", False), + }, + "y": { + "reverse": self.settings.get("invert_y", False), + } + } + } } datasets = [] @@ -151,6 +160,8 @@ def get_form_fields(self): self.sort_x_field(), self.sort_y_field(), self.limit_field(), + self.invert_x_field(), + self.invert_y_field(), self.filter_field(columns), ]