Sine Graph Slider
Hi,
I have a sine graph:
The code is:
<xyPlot name="myPlot" pan="disabled" tap="disabled" zoom="disabled">
<axis dim="y" auto="fixed" min="0" max="10"></axis>
<axis dim="x" auto="fixed" min="0" max="20"></axis>
<series name="series" manual="true" />
</xyPlot>
<script>
local x, y;
local sinePlot= myPlot.series;
for x = 10,10, 0.1 do
y = 5*math.cos((math.pi)*x)+5
sinePlot:Add(x,y)
end
sinePlot:Update()
</script>
And I want to add a slider to change a component of the sine graph itself. How would I go about doing this?

To control a component of the sine graph, you need to do a few things:
 Create variables for all the components that you want the user to be able to control
 Place your plotdrawing forloop inside of a function
 Replace the numbers in your equation with the variables defined in step 1
 Create a slider whose value alters one of the variables defined in step 1
 When the slider is moved, call the function defined in step 2
E.g.
<script>
local x, y;
local sinePlot= myPlot.series;
local amp = 5;  variable to represent the amplitude of the sine wave
local function redrawPlot()  create function and put forloop inside
sinePlot:Clear()
for x = 10,10, 0.1 do
y = amp*math.cos((math.pi)*x)+5  replace amplitude number with variable name
sinePlot:Add(x,y)
end
sinePlot:Update()
end
</script>
<! Set amplitude variable to value of the slider, then redraw the plot >
<slider name="amplitudeSlider" min="0" max="5" value="5">
<onValueChanged>
amp = value
redrawPlot()
</onValueChanged>
</slider>
Please sign in to leave a comment.
Comments
1 comment