From 8fcfee73b2b1977f1816bd7b6bd2cd53a46ea64b Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Thu, 23 Jan 2025 18:10:28 -0700 Subject: add option to select trendline type --- app.py | 45 +++++++++++++++++++++++++++++++++++++++------ templates/index.html | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 18b66c1..f2f3e41 100644 --- a/app.py +++ b/app.py @@ -29,7 +29,7 @@ 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_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 = [] @@ -77,9 +77,36 @@ def plot_data(x_data, y_data, std_dev_data, color_picker, labels, df, 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) - h, = ax.plot(x,p(x), linestyle="dashed", label="Trendline", color=trendline_color) + # 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 @@ -144,7 +171,7 @@ def patent(): # POST for process_data @app.route('/process_data', methods=['POST']) def process_data(): - print(request.form) + print("Request form",request.form) textarea = request.form['excelData'] df = create_df_from_textarea(textarea) @@ -239,6 +266,12 @@ def process_data(): 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 @@ -259,7 +292,7 @@ def process_data(): 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_equation=trendline_equation, chart_type=chart_type, trendline_type=trendline_type, polynomial_order=polynomial_order) # Return plot as image from io import BytesIO diff --git a/templates/index.html b/templates/index.html index 46b53e0..ca40c20 100644 --- a/templates/index.html +++ b/templates/index.html @@ -194,7 +194,29 @@
- +
+
+ + +
+
+
+ +
+
+ +
+
@@ -242,6 +264,18 @@ // add change handler for textarea and execute processData function //document.getElementById('excelData').addEventListener('change', processData); + window.onload = async() => { + document.getElementById('trendlineSelect').addEventListener('change', function () { + const trendlineType = this.value; + const orderContainer = document.getElementById('polynomialOrderContainer'); + if (trendlineType === 'polynomial') { + orderContainer.style.display = 'block'; + } else { + orderContainer.style.display = 'none'; + } + }); + } + function prepareSubmission() { const data = document.getElementById('excelData').value; @@ -271,8 +305,8 @@ function processData() { const data = document.getElementById('excelData').value; - // const rows = data.split('\n').map(row => row.split('\t')); - const rows = data.split('\n').filter(row => row.trim() !== '').map(row => row.split('\t')); + const rows = data.split('\n').map(row => row.split('\t')); + // const rows = data.split('\n').filter(row => row.trim() !== '').map(row => row.split('\t')); if (rows.length === 0) { alert('No data found'); return; -- cgit v1.2.3