From 7c480c5698ed7d4d258e3f66e3633666f297790a Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Tue, 21 May 2024 01:15:42 -0600 Subject: add brand guidelines mode --- api.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 18 deletions(-) (limited to 'api.py') diff --git a/api.py b/api.py index 3c64840..4aabbd9 100644 --- a/api.py +++ b/api.py @@ -39,22 +39,36 @@ class PlotDataRequest(BaseModel): constant_line_vals: list[float] = Field([], title="Constant Line Values", description="List of values for the constant line") constant_line_name: list[str] = Field([], title="Constant Line Names", description="List of names for the constant line") + trendline_colour: str = Field("#000000", title="Trendline Colour", description="Colour of the trendline") + marker_colour: str = Field("#000000", title="Marker Colour", description="Colour of the markers") + + brand_guidelines_mode: bool = Field(False, title="Brand Guidelines Mode", description="Use Brand Guidelines Mode") + app = FastAPI() def plot_data(x_data, y_data, std_dev_data, color_picker, labels, df, title = "Plot", x_label = "X Axis", y_label = "Y Axis", plot_background_color="#ffffff", constant_line=[], enable_trendline=True, enable_grid=False, - trendline_color="#000000", x_axis_scale="linear", y_axis_scale="linear", trendline_equation=None, special_mode=False): + trendline_color="#000000", marker_colour="#000000", + brand_guidelines_mode=False, + x_axis_scale="linear", y_axis_scale="linear", trendline_equation=None, special_mode=False): fig, ax = plt.subplots(dpi=300) special_mode_color = (38/255,56/255,97/255) + if brand_guidelines_mode: + font_path = "roboto-regular.ttf" + times_new_roman = font_manager.FontProperties(fname=font_path, style='normal') + else: + font_path = 'times_new_roman.ttf' + times_new_roman = font_manager.FontProperties(fname=font_path, style='normal') + plots = [] for idx, _ in enumerate(x_data): x = df[x_data[idx]].astype(float) y = df[y_data[idx]].astype(float) - color = color_picker[idx] + #color = color_picker[idx] data_series_title = labels[idx] if special_mode: if (std_dev_data[idx] != None): @@ -63,9 +77,9 @@ def plot_data(x_data, y_data, std_dev_data, color_picker, labels, df, plot = ax.plot(x, y, 'o', color=special_mode_color, label=data_series_title) else: if (std_dev_data[idx] != None): - plot = ax.errorbar(x, y, yerr=df[std_dev_data[idx]].astype(float), fmt='o', ecolor='black', capsize=5, color=color, label=data_series_title) + plot = ax.errorbar(x, y, yerr=df[std_dev_data[idx]].astype(float), fmt='o', ecolor='black', capsize=5, color=marker_colour, label=data_series_title) else: - plot = ax.plot(x, y, color=color, label=data_series_title) + plot = ax.plot(x, y, color=marker_colour, label=data_series_title) if (type(plot) == list): plots.extend(plot) @@ -86,7 +100,7 @@ def plot_data(x_data, y_data, std_dev_data, color_picker, labels, df, p = sympify(trendline_equation) x_range = np.linspace(df[x_data[0]].astype(float).min(), df[x_data[0]].astype(float).max(), 100) y_range = [p.subs(x, i) for i in x_range] - h, = ax.plot(x_range, y_range, linestyle="dotted", label="Trendline", color=trendline_color) + h, = ax.plot(x_range, y_range, linestyle="dashdot", label="Trendline", color=trendline_color) handles.append(h) except: print("Invalid Equation") @@ -95,21 +109,35 @@ def plot_data(x_data, y_data, std_dev_data, color_picker, labels, df, y = df[y_data[0]].astype(float) z = np.polyfit(x, y, 2) p = np.poly1d(z) - h, = ax.plot(x,p(x), linestyle="dotted", label="Trendline", color=trendline_color) + h, = ax.plot(x,p(x), linestyle="dashdot", label="Trendline", color=trendline_color) handles.append(h) + if not brand_guidelines_mode: + light_grey = 0.9 + dar_grey = 0.4 - - light_grey = 0.9 - dar_grey = 0.4 - - for idx, line in enumerate(constant_line): - val, name = line - idx += 1 - grey_shade = light_grey - (light_grey - dar_grey) * (idx / len(constant_line)) - color = (grey_shade, grey_shade, grey_shade) - h = ax.axhline(y=val, linestyle='dashed', dashes=(idx+3,2) , color=color, label=name) - handles.append(h) + for idx, line in enumerate(constant_line): + val, name = line + idx += 1 + grey_shade = light_grey - (light_grey - dar_grey) * (idx / len(constant_line)) + color = (grey_shade, grey_shade, grey_shade) + h = ax.axhline(y=val, linestyle='dashed', dashes=(idx+3,2) , color=color, label=name) + handles.append(h) + else: + light_green = (139/255, 196/255, 63/255) + dark_green = (21/255, 130/255, 56/255) + + for idx, line in enumerate(constant_line): + val, name = line + idx += 1 + ratio = idx / len(constant_line) + color = ( + light_green[0] + (dark_green[0] - light_green[0]) * ratio, + light_green[1] + (dark_green[1] - light_green[1]) * ratio, + light_green[2] + (dark_green[2] - light_green[2]) * ratio + ) + h = ax.axhline(y=val, linestyle='dashed', dashes=(idx+3,2) , color=color, label=name) + handles.append(h) ax.grid(True,linestyle=(0,(1,5))) # enable_grid) @@ -190,10 +218,13 @@ async def create_plot(request: PlotDataRequest, data: Request): labels = request.label + print(f"Got trendline colour {request.trendline_colour}") + fig = plot_data(x_data, y_data, std_dev_data, request.color_picker, labels, df, title=request.title, x_label=request.x_label, y_label=request.y_label, enable_trendline=request.enable_trendline, enable_grid=request.enable_grid, - constant_line=constant_line, + constant_line=constant_line, trendline_color=request.trendline_colour, + marker_colour=request.marker_colour, brand_guidelines_mode=request.brand_guidelines_mode, x_axis_scale=request.x_axis_scale, y_axis_scale=request.y_axis_scale, trendline_equation=request.trendline_equation, special_mode=request.special_mode ) -- cgit v1.2.3