Skip to content

Commit

Permalink
refactor: refactor and fix type hinting for plotly builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Nov 26, 2024
1 parent 178f4e9 commit 58568ad
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions ckanext/charts/chart_builders/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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.
"""
Expand Down

0 comments on commit 58568ad

Please sign in to comment.