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-59 / remove restriction for multi variable line charts #7

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
32 changes: 18 additions & 14 deletions ckanext/charts/chart_builders/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def to_json(self) -> Any:
return self.build_line_chart()

def build_line_chart(self) -> Any:
"""Build a line chart. For now we support only Two Y Axes in the line chart."""
"""
Build a line chart. It supports multi columns for y-axis
to display on the line chart.
"""
fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(
Expand All @@ -60,14 +63,15 @@ def build_line_chart(self) -> Any:
)

if len(self.settings["y"]) > 1:
fig.add_trace(
go.Scatter(
x=self.df[self.settings["x"]],
y=self.df[self.settings["y"][1]],
name=self.settings["y"][1],
),
secondary_y=True,
)
for column in self.settings["y"][1:]:
fig.add_trace(
go.Scatter(
x=self.df[self.settings["x"]],
y=self.df[column],
name=column,
),
secondary_y=True,
)

if chart_title := self.settings.get("chart_title"):
fig.update_layout(title_text=chart_title)
Expand Down Expand Up @@ -163,13 +167,13 @@ class PlotlyLineForm(BasePlotlyForm):
builder = PlotlyLineBuilder

def plotly_y_multi_axis_field(
self, columns: list[dict[str, str]], max_y: int
self, columns: list[dict[str, str]], max_y: int = 0
) -> dict[str, Any]:
"""Plotly line chart support up to 2 y-axis."""
field = self.y_multi_axis_field(columns, 2)
"""Plotly line chart supports multi columns for y-axis"""
field = self.y_multi_axis_field(columns, max_y)

field["help_text"] = (
"Select the columns for the Y axis. You can select up to 2 columns."
"Select the columns for the Y axis."
)

return field
Expand All @@ -189,7 +193,7 @@ def get_form_fields(self):
self.type_field(chart_types),
self.engine_details_field(),
self.x_axis_field(columns),
self.plotly_y_multi_axis_field(columns, 2),
self.plotly_y_multi_axis_field(columns),
self.more_info_button_field(),
self.invert_x_field(),
self.invert_y_field(),
Expand Down
Loading