diff --git a/ckanext/charts/chart_builders/plotly.py b/ckanext/charts/chart_builders/plotly.py index c38c1b9..b4962a0 100644 --- a/ckanext/charts/chart_builders/plotly.py +++ b/ckanext/charts/chart_builders/plotly.py @@ -78,32 +78,30 @@ class PlotlyLineBuilder(PlotlyBuilder): def to_json(self) -> Any: return self.build_line_chart() - def _split_data_by_year(self) -> dict[str, Any]: + def _split_data_by_year(self) -> None: """ Prepare data for a line chart. It splits the data by year stated in the date format column which is used for x-axis. """ - if len(self.settings["years"]) > 1: - self.df.drop_duplicates(subset=[self.settings["x"]], inplace=True) - self.df = self.df[[self.settings["x"], self.settings["y"][0]]] - self.df["year"] = pd.to_datetime(self.df[self.settings["x"]]).dt.year - - self.df = self.df.pivot( - index=self.settings["x"], - columns="year", - values=self.settings["y"][0], - ) + if len(self.settings["years"]) <= 1: + return - self.settings["y"] = self.df.columns.to_list() - self.df[self.settings["x"]] = self.df.index - self.df[self.settings["x"]] = pd.to_datetime( - self.df[self.settings["x"]], - unit="ns", - ).dt.strftime("%m-%d %H:%M") + self.df.drop_duplicates(subset=[self.settings["x"]], inplace=True) + self.df = self.df[[self.settings["x"], self.settings["y"][0]]] + self.df["year"] = pd.to_datetime(self.df[self.settings["x"]]).dt.year + + self.df = self.df.pivot( + index=self.settings["x"], columns="year", values=self.settings["y"][0] + ) - return self + self.settings["y"] = self.df.columns.to_list() + self.df[self.settings["x"]] = self.df.index + self.df[self.settings["x"]] = pd.to_datetime( + self.df[self.settings["x"]], + unit="ns", + ).dt.strftime("%m-%d %H:%M") - def _skip_null_values(self, column: str) -> tuple[Any]: + def _skip_null_values(self, column: str) -> tuple[Any, Any]: """ Return values for x-axis and y-axis after removing missing values. """ @@ -124,7 +122,9 @@ 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]: + def _break_chart_by_missing_data( + self, df: DataFrame, column: str + ) -> tuple[Any, Any]: """ Find gaps in date column and fill them with missing dates. """