Skip to content

Commit 0f2f395

Browse files
Merge pull request #164 from Speedrunyourknowledge/fix/derivative-graph
fix: derivative graph crashed on large intervals and resized itself while moving slider
2 parents 9a0545c + a99b2ad commit 0f2f395

File tree

11 files changed

+43
-19
lines changed

11 files changed

+43
-19
lines changed

calc-backend/graph-gen/graph-deriv.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def derivative(func, x, h=1e-5):
3838
return (func(x + h) - func(x - h)) / (2 * h)
3939

4040
# Create an array of x values and evaluate f(x) for these values.
41-
x_vals = np.arange(x_range[0], x_range[1] + 0.05, 0.05)
41+
x_vals = np.linspace(x_range[0], x_range[1], num=51)
4242
f_vals = f(x_vals)
4343

4444
# Initialize the Plotly figure and plot the original function.
@@ -57,28 +57,32 @@ def derivative(func, x, h=1e-5):
5757
name='Point'))
5858

5959
# Set the initial layout title with the slope at the starting point.
60-
fig.update_layout(title=f"Slope Value = {derivative(f, x0):.2f}")
60+
fig.update_layout(title=(f'Slope Value = {derivative(f, x0):.4f}'), title_font_size=16)
61+
62+
# Get the initial y-axis range
63+
initial_yaxis= fig.layout['yaxis_range']
6164

6265
# Create frames for the animation slider.
6366
frames = []
6467
for slider_val in x_vals:
6568
# Compute the tangent line at each slider value using our numerical derivative.
66-
tan_y = derivative(f, slider_val) * (x_vals - slider_val) + f(slider_val)
69+
save_deriv = derivative(f, slider_val)
70+
tan_y = save_deriv * (x_vals - slider_val) + f(slider_val)
6771
frames.append(go.Frame(
6872
data=[
6973
go.Scatter(), # Placeholder for the function trace (remains unchanged).
7074
go.Scatter(x=x_vals, y=tan_y), # Tangent line.
7175
go.Scatter(x=[slider_val], y=[f(slider_val)]) # Point of tangency.
7276
],
73-
layout=go.Layout(title={'text': f"Slope Value = {derivative(f, slider_val):.2f}"}),
77+
layout=go.Layout(title=(f'Slope Value = {save_deriv:.4f}'), yaxis_range= initial_yaxis, yaxis={'autorange':False}),
7478
name=str(slider_val)
7579
))
7680
fig.frames = frames
7781

7882
# Create a slider to control the animation.
7983
sliders = [dict(
8084
active=0,
81-
currentvalue={"visible": False},
85+
currentvalue=dict(prefix= "Slider", font={'size':14}),
8286
ticklen=0, # Hides the line ticks (not enough alone)
8387
len=1.0,
8488
y=-0.05,
@@ -88,14 +92,16 @@ def derivative(func, x, h=1e-5):
8892
args=[
8993
[frame.name],
9094
dict(mode='immediate',
91-
frame=dict(duration=40, redraw=True),
92-
transition=dict(duration=0))
95+
frame=dict(duration=0, redraw=True),
96+
)
9397
],
9498
label="", # Hides numbers
9599
)
96100
for frame in frames
97101
],
98102
tickcolor='rgba(0,0,0,0)',
103+
bordercolor='#949fb3',
104+
pad={"t": 10, "b": 10},
99105
)]
100106

101107
# Adjust padding and final layout settings.
@@ -106,7 +112,8 @@ def derivative(func, x, h=1e-5):
106112
xaxis=dict(range=[x_range[0], x_range[1]], fixedrange=True),
107113
yaxis=dict(range=[np.min(f_vals) - pad, np.max(f_vals) + pad], fixedrange=True),
108114
sliders=sliders,
109-
uirevision='static'
115+
uirevision='static',
116+
margin=dict(t=50, r=0,l=60),
110117
)
111118

112119
fig_json = fig.to_json(pretty=True)

calc-backend/graph-gen/graph-integral.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ def generate_bars(n_bars):
123123

124124
sliders = [dict(
125125
active=0,
126-
currentvalue={"prefix": "n = "},
126+
currentvalue={"prefix": "n = ", "font":{'size':14}},
127127
pad={"t": 30},
128+
bordercolor='#949fb3',
128129
steps=steps
129130
)]
130131

calc-backend/src/controllers/graph.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ export function createDerivGraph(req: Request, res: Response, next: NextFunction
7878
res.status(200).send(prettyStr);
7979
}
8080
})
81-
82-
setTimeout(function(){ process.kill()}, 7500);
81+
// timeout if graph takes longer than 10 seconds
82+
setTimeout(function(){ process.kill()}, 10000);
8383
}

calc-frontend/src/components/Cosine/IntCosineGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ useLayoutEffect(() => {
212212
"sliders": [{
213213
"active": 0,
214214
"currentvalue": {
215-
"prefix": "n = "
215+
"prefix": "n = ",
216+
"font":{'size':14}
216217
},
218+
"bordercolor":'#949fb3',
217219
"pad": {
218220
"t": 30
219221
},

calc-frontend/src/components/Cubic/IntCubicGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ useLayoutEffect(() => {
225225
"sliders": [{
226226
"active": 0,
227227
"currentvalue": {
228-
"prefix": "n = "
228+
"prefix": "n = ",
229+
"font":{'size':14}
229230
},
231+
"bordercolor":'#949fb3',
230232
"pad": {
231233
"t": 30
232234
},

calc-frontend/src/components/Euler/IntEulerGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ useLayoutEffect(() => {
211211
"sliders": [{
212212
"active": 0,
213213
"currentvalue": {
214-
"prefix": "n = "
214+
"prefix": "n = ",
215+
"font":{'size':14}
215216
},
217+
"bordercolor":'#949fb3',
216218
"pad": {
217219
"t": 30
218220
},

calc-frontend/src/components/Line/IntLineGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ useLayoutEffect(() => {
211211
"sliders": [{
212212
"active": 0,
213213
"currentvalue": {
214-
"prefix": "n = "
214+
"prefix": "n = ",
215+
"font":{'size':14}
215216
},
217+
"bordercolor":'#949fb3',
216218
"pad": {
217219
"t": 30
218220
},

calc-frontend/src/components/NatLog/IntNatLogGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ useLayoutEffect(() => {
212212
"sliders": [{
213213
"active": 0,
214214
"currentvalue": {
215-
"prefix": "n = "
215+
"prefix": "n = ",
216+
"font":{'size':14}
216217
},
218+
"bordercolor":'#949fb3',
217219
"pad": {
218220
"t": 30
219221
},

calc-frontend/src/components/Quadratic/IntQuadraticGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ useLayoutEffect(() => {
224224
"sliders": [{
225225
"active": 0,
226226
"currentvalue": {
227-
"prefix": "n = "
227+
"prefix": "n = ",
228+
"font":{'size':14}
228229
},
230+
"bordercolor":'#949fb3',
229231
"pad": {
230232
"t": 30
231233
},

calc-frontend/src/components/Sine/IntSineGraph.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ useLayoutEffect(() => {
212212
"sliders": [{
213213
"active": 0,
214214
"currentvalue": {
215-
"prefix": "n = "
215+
"prefix": "n = ",
216+
"font":{'size':14}
216217
},
218+
"bordercolor":'#949fb3',
217219
"pad": {
218220
"t": 30
219221
},

0 commit comments

Comments
 (0)