1 | <!DOCTYPE html>
|
2 | <html>
|
3 | <head>
|
4 | <title>math.js | plot</title>
|
5 | <script src="../../dist/math.js"></script>
|
6 |
|
7 | <script src="https://cdn.plot.ly/plotly-1.35.2.min.js"></script>
|
8 |
|
9 | <style>
|
10 | input[type=text] {
|
11 | width: 300px;
|
12 | }
|
13 | input {
|
14 | padding: 6px;
|
15 | }
|
16 | body, html, input {
|
17 | font-family: sans-serif;
|
18 | font-size: 11pt;
|
19 |
|
20 | }
|
21 | form {
|
22 | margin: 20px 0;
|
23 | }
|
24 | </style>
|
25 | </head>
|
26 | <body>
|
27 |
|
28 | <form id="form">
|
29 | <label for="eq">Enter an equation:</label>
|
30 | <input type="text" id="eq" value="4 * sin(x) + 5 * cos(x/2)" />
|
31 | <input type="submit" value="Draw" />
|
32 | </form>
|
33 |
|
34 | <div id="plot"></div>
|
35 |
|
36 | <p>
|
37 | Used plot library: <a href="https://plot.ly/javascript/">Plotly</a>
|
38 | </p>
|
39 |
|
40 | <script>
|
41 | function draw() {
|
42 | try {
|
43 |
|
44 | const expression = document.getElementById('eq').value
|
45 | const expr = math.compile(expression)
|
46 |
|
47 |
|
48 | const xValues = math.range(-10, 10, 0.5).toArray()
|
49 | const yValues = xValues.map(function (x) {
|
50 | return expr.eval({x: x})
|
51 | })
|
52 |
|
53 |
|
54 | const trace1 = {
|
55 | x: xValues,
|
56 | y: yValues,
|
57 | type: 'scatter'
|
58 | }
|
59 | const data = [trace1]
|
60 | Plotly.newPlot('plot', data)
|
61 | }
|
62 | catch (err) {
|
63 | console.error(err)
|
64 | alert(err)
|
65 | }
|
66 | }
|
67 |
|
68 | document.getElementById('form').onsubmit = function (event) {
|
69 | event.preventDefault()
|
70 | draw()
|
71 | }
|
72 |
|
73 | draw()
|
74 | </script>
|
75 |
|
76 | </body>
|
77 | </html>
|