aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2025-01-23 18:10:28 -0700
committerNavan Chauhan <navanchauhan@gmail.com>2025-01-23 18:10:28 -0700
commit8fcfee73b2b1977f1816bd7b6bd2cd53a46ea64b (patch)
treed5330d73a05e5125b1415646613e5630403406d0
parentd5242aa7d9d02f87d9090c770e5ed578edbaa81f (diff)
add option to select trendline typeHEADmain
-rw-r--r--app.py45
-rw-r--r--templates/index.html40
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 @@
</div>
<div class="row mb-3">
- <label for="trendlineEquation" class="form-label">Trendline Equation (Leave blank if you want it to be auto-calculated)</label>
+ <div class="col-md-3">
+ <div class="mb-3">
+ <label for="trendlineSelect" class="form-label">Trendline Type:</label>
+ <select name="trendlineSelect" id="trendlineSelect" class="form-select">
+ <option value="none" selected>None</option>
+ <option value="linear">Linear</option>
+ <option value="exponential">Exponential</option>
+ <option value="logarithmic">Logarithmic</option>
+ <option value="polynomial">Polynomial</option>
+ <option value="power">Power</option>
+ </select>
+ </div>
+ </div>
+ <div class="col-md-3">
+ <div class="mb-3" id="polynomialOrderContainer" style="display: none;">
+ <label for="polynomialOrder" class="form-label">Polynomial Order:</label>
+ <input type="number" id="polynomialOrder" name="polynomialOrder" class="form-control" value="2" min="1"/>
+ </div>
+ </div>
+ </div>
+
+ <div class="row mb-3">
+ <label for="trendlineEquation" class="form-label">Trendline Equation Override (Leave blank if you want it to be auto-calculated)</label>
<input type="text" name="trendlineEquation" id="trendlineEquation" class="form-control" placeholder="">
</div>
@@ -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;