Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLCAXCHZF-54/implement option to invert x and y axes #1

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ckanext/charts/assets/js/charts-render-observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ckan.module("charts-render-observable", function ($, _) {
plot = Plot.dot(this.options.config.data, this.options.config.settings).plot();
break;
case "line":
plot = Plot.line(this.options.config.data, this.options.config.settings).plot();
plot = Plot.line(this.options.config.data, this.options.config.settings).plot(this.options.config.plot);
break;
case "pie":
plot = PieChart(this.options.config.data, this.options.config.settings);
Expand Down
24 changes: 24 additions & 0 deletions ckanext/charts/chart_builders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion ckanext/charts/chart_builders/chartjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ def to_json(self) -> str:
datasets.append(dataset)

data["data"]["datasets"] = datasets

data["options"]["scales"] = {
"x": {
"reverse": self.settings.get("invert_x", False),
},
"y": {
"reverse": self.settings.get("invert_y", False),
},
}
return json.dumps(data)


Expand All @@ -151,6 +158,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),
]

Expand Down
6 changes: 6 additions & 0 deletions ckanext/charts/chart_builders/observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def to_json(self) -> str:
"type": "line",
"data": self.df.to_dict(orient="records"),
"settings": self.settings,
"plot": {
"x": {"reverse": self.settings.get("invert_x", False)},
"y": {"reverse": self.settings.get("invert_y", False)}
}
}
)

Expand All @@ -108,6 +112,8 @@ def get_form_fields(self):
self.type_field(chart_types),
self.x_axis_field(columns),
self.y_axis_field(columns),
self.invert_x_field(),
self.invert_y_field(),
self.sort_x_field(),
self.sort_y_field(),
self.limit_field(),
Expand Down
8 changes: 8 additions & 0 deletions ckanext/charts/chart_builders/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def build_line_chart(self) -> Any:
if chart_title := self.settings.get("chart_title"):
fig.update_layout(title_text=chart_title)

if self.settings.get("invert_x", False):
fig.update_xaxes(autorange="reversed")

if self.settings.get("invert_y", False):
fig.update_yaxes(autorange="reversed")

return fig.to_json()


Expand Down Expand Up @@ -179,6 +185,8 @@ def get_form_fields(self):
self.type_field(chart_types),
self.x_axis_field(columns),
self.plotly_y_multi_axis_field(columns, 2),
self.invert_x_field(),
self.invert_y_field(),
self.sort_x_field(),
self.sort_y_field(),
self.limit_field(),
Expand Down
Loading