From 178f4e906f9f4f01b32a11d3d08815f1a894d894 Mon Sep 17 00:00:00 2001 From: mutantsan Date: Tue, 26 Nov 2024 10:35:35 +0200 Subject: [PATCH] refactor: refactor _break_chart_by_missing_data func --- ckanext/charts/chart_builders/plotly.py | 62 ++++++++++--------------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/ckanext/charts/chart_builders/plotly.py b/ckanext/charts/chart_builders/plotly.py index 8ad1ace..c38c1b9 100644 --- a/ckanext/charts/chart_builders/plotly.py +++ b/ckanext/charts/chart_builders/plotly.py @@ -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( @@ -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", ) @@ -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 @@ -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. @@ -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: """ @@ -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) @@ -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( @@ -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