1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
from flask import Flask, render_template, request, redirect, url_for, flash
import json
import pandas as pd
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import re
from sympy import symbols, sympify
font_path = 'times_new_roman.ttf'
times_new_roman = font_manager.FontProperties(fname=font_path, style='normal')
"""
fig = plot_data(x_data, y_data, std_dev_data, color_picker, title=plot_title,
x_label=x_axis_label, y_label=y_axis_label,
plot_background_color=plot_background_color,
constant_line=constant_lines,
enable_trendline=enable_trendline,
trendline_color=color_picker_trendline)
"""
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, chart_type="default", trendline_type="linear", polynomial_order=2):
fig, ax = plt.subplots(dpi=300)
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]
data_series_title = labels[idx]
print("Chart type:")
print(chart_type)
if chart_type == "patent_bar":
plot = ax.bar(x, y, yerr=df[std_dev_data[idx]].astype(float) if std_dev_data[idx] else None,
capsize=10, color='gray', alpha=0.5, width=0.5, # Adjust the width here
error_kw={'elinewidth': 2, 'capsize': 5, 'capthick': 2})
elif chart_type == "patent_scatter":
plot = ax.errorbar(x, y, yerr=df[std_dev_data[idx]].astype(float) if std_dev_data[idx] else None,
fmt='o-', capsize=5, capthick=2, color='grey', ecolor='black',
elinewidth=2, markerfacecolor='black', markersize=10)
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)
else:
plot = ax.plot(x, y, 'o', color=color, label=data_series_title)
if (type(plot) == list):
plots.extend(plot)
else:
plots.append(plot)
handles = plots
if enable_trendline:
if trendline_equation != None:
try:
x = symbols('x')
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="dashed", label="Trendline", color=trendline_color)
handles.append(h)
print("Valid Equation", p)
except ValueError:
print("Invalid Equation")
else:
x = df[x_data[0]].astype(float)
y = df[y_data[0]].astype(float)
# z = np.polyfit(x, y, 2)
# p = np.poly1d(z)
#
z = None
if trendline_type == 'linear':
z = np.polyfit(x, y, 1)
elif trendline_type == 'polynomial':
z = np.polyfit(x, y, polynomial_order)
elif trendline_type == 'exponential':
z = np.polyfit(x, np.log(y), 1)
p = lambda x: np.exp(z[1]) * np.exp(z[0] * x)
elif trendline_type == 'logarithmic':
z = np.polyfit(np.log(x), y, 1)
p = lambda x: z[1] + z[0] * np.log(x)
elif trendline_type == 'power':
z = np.polyfit(np.log(x), np.log(y), 1)
p = lambda x: np.exp(z[1]) * x ** z[0]
else:
print("Should not have happened")
print("Trendline type: ", trendline_type)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
x_range = np.linspace(x.min(), x.max(), 100)
if trendline_type in ['exponential', 'logarithmic', 'power']:
y_range = p(x_range)
else:
y_range = np.poly1d(z)(x_range)
h, = ax.plot(x_range, y_range, linestyle="dashed", label="Trendline", color=trendline_color)
handles.append(h)
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='--', color=color, label=name)
handles.append(h)
if chart_type == "default":
ax.grid(True,linestyle=(0,(1,5))) # enable_grid)
else:
ax.grid(False)
ax.set_facecolor(plot_background_color)
ax.set_xlabel(x_label, fontproperties=times_new_roman)
ax.set_ylabel(y_label, fontproperties=times_new_roman)
title = title.replace(' in ', '\nin ')
ax.set_title(title, wrap=True, fontproperties=times_new_roman)
for label in (ax.get_xticklabels() + ax.get_yticklabels()):
label.set_fontproperties(times_new_roman)
print(handles)
print(handles[0])
print(handles[0].get_label())
labels = [h.get_label() for h in handles]
ax.legend(handles, labels, loc='best', prop=times_new_roman)
fig.patch.set_facecolor(plot_background_color)
if chart_type != "patent_bar":
fig.tight_layout(pad=3.0)
ax.set_xscale(x_axis_scale)
ax.set_yscale(y_axis_scale)
else:
fig.tight_layout()
return fig
app = Flask(__name__)
def create_df_from_textarea(textarea):
rows = textarea.split('\n')
data = [row.split("\t") for row in rows]
df = pd.DataFrame(data[1:], columns=data[0])
return df
@app.route('/')
def index():
return render_template('index.html')
@app.route("/patent")
def patent():
return render_template('patent.html')
# POST for process_data
@app.route('/process_data', methods=['POST'])
def process_data():
print("Request form",request.form)
textarea = request.form['excelData']
df = create_df_from_textarea(textarea)
print(df)
data_keys = f"{request.form.keys()}"
xPattern = r'xColumn-\d+'
yPattern = r'yColumn-\d+'
stdDevPattern = r'stdDevColumn-\d+'
colorPickerPattern = r'colorPicker-\d+'
labelPattern = r'dataSeries-\d+'
constantLinePattern = r'constantLine\d+'
constantLineLabelPattern = r'constantLineLabel\d+'
# match in data_keys string
x_data_matches = re.findall(xPattern, data_keys)
y_data_matches = re.findall(yPattern, data_keys)
std_dev_data_matches = re.findall(stdDevPattern, data_keys)
color_picker_matches = re.findall(colorPickerPattern, data_keys)
label_matches = re.findall(labelPattern, data_keys)
constant_line_matches = re.findall(constantLinePattern, data_keys)
constant_line_label_matches = re.findall(constantLineLabelPattern, data_keys)
x_data = []
for x in x_data_matches:
val = request.form.get(x)
if val != '':
x_data.append(df.columns[int(val)])
else:
x_data.append(None)
y_data = []
for y in y_data_matches:
val = request.form.get(y)
if val != '':
y_data.append(df.columns[int(val)])
else:
y_data.append(None)
std_dev_data = []
for std_dev in std_dev_data_matches:
val = request.form.get(std_dev)
if val != '':
std_dev_data.append(df.columns[int(val)])
else:
std_dev_data.append(None)
color_picker = []
for color in color_picker_matches:
val = request.form.get(color)
if val != '':
color_picker.append(val)
else:
color_picker.append(None)
data_series_label = []
for label in label_matches:
val = request.form.get(label)
if val != '':
data_series_label.append(val)
else:
data_series_label.append("Data")
constant_lines = []
for idx, val in enumerate(constant_line_matches):
val = request.form.get(val)
if val != '':
label = request.form.get(constant_line_label_matches[idx]) or "Constant Line"
val = float(val)
constant_lines.append((val, label))
x_axis_label = request.form.get('xTitle', 'X Axis')
y_axis_label = request.form.get('yTitle', 'Y Axis')
plot_title = request.form.get('plotTitle', 'Plot Title')
plot_background_color = request.form.get('colorPickerPlotBackground', '#ffffff')
color_picker_trendline = request.form.get('colorPickerTrendline', '#00ff00')
x_axis_scale = request.form.get('xAxisScale', 'linear')
y_axis_scale = request.form.get('yAxisScale', 'linear')
trendline_type = request.form.get('trendlineSelect', 'none')
polynomial_order = int(request.form.get('polynomialOrder', '2'))
if polynomial_order > 6:
polynomial_order = 6
calc_trendline = request.form.get('calcTrendline', 'off')
if calc_trendline == 'on':
enable_trendline = True
else:
enable_trendline = False
trendline_equation = request.form.get('trendlineEquation', None)
if trendline_equation == "":
trendline_equation = None
chart_type = request.form.get('chartType', 'default')
fig = plot_data(x_data, y_data, std_dev_data, color_picker, data_series_label, df, title=plot_title,
x_label=x_axis_label, y_label=y_axis_label,
plot_background_color=plot_background_color,
constant_line=constant_lines,
enable_trendline=enable_trendline,
trendline_color=color_picker_trendline,
x_axis_scale=x_axis_scale,
y_axis_scale=y_axis_scale,
trendline_equation=trendline_equation, chart_type=chart_type, trendline_type=trendline_type, polynomial_order=polynomial_order)
# Return plot as image
from io import BytesIO
import base64
buffer = BytesIO()
fig.savefig(buffer, format='png')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8').replace('\n', '')
# Embed base64 image in HTML
return '<img src="data:image/png;base64,{}">'.format(image_base64)
if __name__ == '__main__':
app.run(port=8080,debug=True)
|