diff --git a/py-scripts/lf_graph.py b/py-scripts/lf_graph.py index 7c22f30d..26e63b3c 100644 --- a/py-scripts/lf_graph.py +++ b/py-scripts/lf_graph.py @@ -72,12 +72,84 @@ class lf_bar_graph(): fig = plt.gcf() plt.savefig("%s.png"% (self.graph_image_name), dpi=96) + plt.close() print("{}.png".format(self.graph_image_name)) return "%s.png" % (self.graph_image_name) +class lf_scatter_graph(): + def __init__(self, + _x_data_set= ["sta0 ","sta1","sta2","sta3"], + _y_data_set= [[30,55,69,37]], + _xaxis_name="x-axis", + _yaxis_name="y-axis", + _label = ["num1", "num2"], + _graph_image_name="image_name", + _color=["r","y"], + _figsize=(9,4)): + self.x_data_set = _x_data_set + self.y_data_set = _y_data_set + self.xaxis_name = _xaxis_name + self.yaxis_name = _yaxis_name + self.figsize = _figsize + self.graph_image_name = _graph_image_name + self.color = _color + self.label = _label + def build_scatter_graph(self): + if self.color is None: + self.color = ["orchid", "lime", "aquamarine", "royalblue", "darkgray", "maroon"] + fig = plt.subplots(figsize=self.figsize) + plt.scatter(self.x_data_set, self.y_data_set[0], color=self.color[0], label=self.label[0]) + if len(self.y_data_set) > 1: + for i in range(1,len(self.y_data_set)): + plt.scatter(self.x_data_set, self.y_data_set[i], color=self.color[i], label=self.label[i]) + plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) + plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) + plt.gcf().autofmt_xdate() + plt.legend() + plt.savefig("%s.png" % (self.graph_image_name), dpi=96) + plt.close() + print("{}.png".format(self.graph_image_name)) + + return "%s.png" % (self.graph_image_name) + +class lf_stacked_graph(): + def __init__(self, + _data_set= [[1,2,3,4],[1,1,1,1],[1,1,1,1]], + _xaxis_name="Stations", + _yaxis_name="Numbers", + _label = ['Success','Fail'], + _graph_image_name="image_name", + _color = ["b","g"], + _figsize=(9,4)): + self.data_set = _data_set # [x_axis,y1_axis,y2_axis] + self.xaxis_name = _xaxis_name + self.yaxis_name = _yaxis_name + self.figsize = _figsize + self.graph_image_name = _graph_image_name + self.label = _label + self.color = _color + + + def build_stacked_graph(self): + fig = plt.subplots(figsize=self.figsize) + if self.color is None: + self.color = ["darkred", "tomato", "springgreen", "skyblue", "indigo", "plum"] + plt.bar(self.data_set[0], self.data_set[1], color=self.color[0]) + plt.bar(self.data_set[0], self.data_set[2], bottom=self.data_set[1], color=self.color[1]) + if len(self.data_set) > 3: + for i in range(3, len(self.data_set)): + plt.bar(self.data_set[0], self.data_set[i], bottom=np.array(self.data_set[i-2])+np.array(self.data_set[i-1]), color=self.color[i-1]) + plt.xlabel(self.xaxis_name) + plt.ylabel(self.yaxis_name) + plt.legend(self.label) + plt.savefig("%s.png" % (self.graph_image_name), dpi=96) + plt.close() + print("{}.png".format(self.graph_image_name)) + + return "%s.png" % (self.graph_image_name) # Unit Test if __name__ == "__main__": diff --git a/py-scripts/lf_report_test.py b/py-scripts/lf_report_test.py index 5869fae3..9921ead9 100644 --- a/py-scripts/lf_report_test.py +++ b/py-scripts/lf_report_test.py @@ -6,7 +6,7 @@ import numpy as np import pandas as pd import pdfkit from lf_report import lf_report -from lf_graph import lf_bar_graph +from lf_graph import lf_bar_graph, lf_scatter_graph , lf_stacked_graph # Unit Test if __name__ == "__main__": @@ -83,7 +83,36 @@ if __name__ == "__main__": report.move_graph_image() report.build_graph() + set1 = [1, 2, 3, 4] + set2 = [[45, 67, 45, 34], [34, 56, 45, 34], [45, 78, 23, 45]] + graph2 = lf_scatter_graph(_x_data_set=set1, _y_data_set=set2, _xaxis_name="x-axis", + _yaxis_name="y-axis", + _graph_image_name="image_name1", + _color=None, + _label=["s1", "s2", "s3"]) + graph_png = graph2.build_scatter_graph() + print("graph name {}".format(graph_png)) + + report.set_graph_image(graph_png) + report.move_graph_image() + + report.build_graph() + dataset = [["1", "2", "3", "4"], [12, 45, 67, 34], [23, 67, 23, 12], [25, 45, 34, 23]] + graph = lf_stacked_graph(_data_set=dataset, + _xaxis_name="Stations", + _yaxis_name="Login PASS/FAIL", + _label=['Success', 'Fail', 'both'], + _graph_image_name="login_pass_fail1", + _color=None) + + graph_png = graph.build_stacked_graph() + + print("graph name {}".format(graph_png)) + + report.set_graph_image(graph_png) + report.move_graph_image() + report.build_graph() #report.build_all() html_file = report.write_html()