-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmychart.js
More file actions
30 lines (26 loc) · 764 Bytes
/
mychart.js
File metadata and controls
30 lines (26 loc) · 764 Bytes
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
document.getElementById('fx').addEventListener('click', draw);
// tocalculate.innerHTML="4*sin(x)+5*cos(x/2)"
function draw() {
try {
// compile the expression once
const expression = tocalculate.innerHTML;
const expr = math.compile(expression)
// evaluate the expression repeatedly for different values of x
const xValues = math.range(-10, 10, 0.5).toArray()
const yValues = xValues.map(function (x) {
return expr.evaluate({x: x})
})
// render the plot using plotly
const trace1 = {
x: xValues,
y: yValues,
type: 'scatter'
}
const data = [trace1]
Plotly.newPlot('plot', data)
}
catch (err) {
console.error(err)
alert(err)
}
}