Skip to content

Commit

Permalink
refactor: refactor _break_chart_by_missing_data func
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Nov 26, 2024
1 parent efab148 commit 178f4e9
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions ckanext/charts/chart_builders/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def build_bar_chart(self) -> Any:
self.df = self.df[self.df[self.settings["y"]].notna()]

fig = px.bar(
data_frame = self.df,
x = self.settings["x"],
y = self.settings["y"],
data_frame=self.df,
x=self.settings["x"],
y=self.settings["y"],
)

fig.update_xaxes(
Expand All @@ -56,9 +56,9 @@ def build_horizontal_bar_chart(self) -> Any:
self.df = self.df[self.df[self.settings["y"]].notna()]

fig = px.bar(
data_frame = self.df,
y = self.settings["x"],
x = self.settings["y"],
data_frame=self.df,
y=self.settings["x"],
x=self.settings["y"],
orientation="h",
)

Expand All @@ -78,7 +78,6 @@ class PlotlyLineBuilder(PlotlyBuilder):
def to_json(self) -> Any:
return self.build_line_chart()


def _split_data_by_year(self) -> dict[str, Any]:
"""
Prepare data for a line chart. It splits the data by year stated
Expand All @@ -104,7 +103,6 @@ def _split_data_by_year(self) -> dict[str, Any]:

return self


def _skip_null_values(self, column: str) -> tuple[Any]:
"""
Return values for x-axis and y-axis after removing missing values.
Expand All @@ -126,35 +124,28 @@ def _skip_null_values(self, column: str) -> tuple[Any]:

return x, y


def _break_chart_by_missing_data(self, df: DataFrame, column: str) -> tuple[Any]:
"""
Find gaps in date column and fill them with missing dates.
"""
if len(self.settings["years"]) > 1:
df["xaxis"] = df[self.settings["x"]]
if len(self.settings["years"]) <= 1:
return df[self.settings["x"]], df[column]

if self.settings.get("split_data"):
df[self.settings["x"]] = df.index
df["xaxis"] = df[self.settings["x"]]

df["date"] = pd.to_datetime(df[self.settings["x"]]).dt.date
if self.settings.get("split_data"):
df[self.settings["x"]] = df.index

all_dates = pd.date_range(
start=df["date"].min(),
end=df["date"].max(),
unit="ns",
).date
date_range_df = pd.DataFrame({"date": all_dates})
df = pd.merge(date_range_df, df, on="date", how="left")
df["date"] = pd.to_datetime(df[self.settings["x"]]).dt.date

x = df["xaxis"]
y = df[column]
else:
x = df[self.settings["x"]]
y = df[column]
all_dates = pd.date_range(
start=df["date"].min(), end=df["date"].max(), unit="ns"
).date

return x, y
date_range_df = pd.DataFrame({"date": all_dates})
df = pd.merge(date_range_df, df, on="date", how="left")

return df["xaxis"], df[column]

def build_line_chart(self) -> Any:
"""
Expand Down Expand Up @@ -239,7 +230,6 @@ class PlotlyScatterBuilder(PlotlyBuilder):
def to_json(self) -> Any:
return self.build_scatter_chart()


def build_scatter_chart(self) -> Any:
self.df = self.df.fillna(0)

Expand All @@ -248,16 +238,16 @@ def build_scatter_chart(self) -> Any:

if self.df[self.settings["size"]].dtype not in ["int64", "float64"]:
raise exception.ChartBuildError(
"""The 'size' source should be a field of positive integer
"""The 'size' source should be a field of positive integer
or float type."""
)

fig = px.scatter(
data_frame = self.df,
x = self.settings["x"],
y = self.settings["y"],
size = self.settings["size"],
size_max = self.settings["size_max"],
data_frame=self.df,
x=self.settings["x"],
y=self.settings["y"],
size=self.settings["size"],
size_max=self.settings["size_max"],
)

fig.update_xaxes(
Expand Down Expand Up @@ -347,9 +337,7 @@ def plotly_y_multi_axis_field(
"""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."
)
field["help_text"] = "Select the columns for the Y axis."

return field

Expand Down

0 comments on commit 178f4e9

Please sign in to comment.