Commit | Line | Data |
---|---|---|
6bef45dc DV |
1 | <html> |
2 | <head> | |
3 | <title>dygraphs Equation Plotter</title> | |
4 | <!--[if IE]> | |
5 | <script type="text/javascript" src="excanvas.js"></script> | |
6 | <![endif]--> | |
7 | <script type="text/javascript" src="../dygraph-combined.js"></script> | |
8 | <script type="text/javascript" src="../dygraph-canvas.js"></script> | |
9 | <script type="text/javascript" src="../dygraph.js"></script> | |
10 | <script type="text/javascript"> | |
11 | function plot() { | |
12 | var eq = document.getElementById("eq").value; | |
13 | eval("fn = " + eq); | |
14 | ||
d16579a0 | 15 | var graph = document.getElementById("graph_div"); |
6bef45dc DV |
16 | var width = parseInt(graph.style.width); |
17 | var x1 = parseFloat(document.getElementById("x1").value); | |
18 | var x2 = parseFloat(document.getElementById("x2").value); | |
19 | var xs = 1.0 * (x2 - x1) / width; | |
20 | ||
21 | var data = []; | |
22 | for (var i = 0; i < width; i++) { | |
23 | var x = x1 + i * xs; | |
24 | var y = fn(x); | |
e82114b2 DV |
25 | var row = [x]; |
26 | if (y.length > 0) { | |
27 | for (var j = 0; j < y.length; j++) { | |
28 | row.push(y[j]); | |
29 | } | |
30 | } else { | |
31 | row.push(y); | |
32 | } | |
33 | data.push(row); | |
6bef45dc DV |
34 | } |
35 | ||
e82114b2 DV |
36 | g = new Dygraph(graph, data); |
37 | } | |
38 | ||
39 | function preset() { | |
40 | var sel = document.getElementById("presets").selectedIndex; | |
41 | var id = document.getElementById("presets").options[sel].id; | |
42 | var presets = { | |
43 | 'id': [ -10, 10, 'function(x) {\n return x;\n}' ], | |
44 | 'sine': [ -10, 10, 'function(x) {\n return Math.sin(x);\n}' ], | |
45 | 'taylor': [ -3, 3, 'function(x) {\n return [Math.cos(x), 1 - x*x/2 + x*x*x*x/24];\n}' ], | |
46 | 'sawtooth': [-10, 10, 'function(x) {\n var y = 0;\n for (var i = 1; i < 20; i+=2) {\n y += Math.sin(i * x)/i;\n }\n var final = 1 - 2*(Math.abs(Math.floor(x / Math.PI)) % 2);\n return [4/Math.PI * y, final];\n}' ] | |
47 | }; | |
48 | ||
49 | if (id == "custom") { return; } | |
50 | document.getElementById("x1").value = presets[id][0]; | |
51 | document.getElementById("x2").value = presets[id][1]; | |
52 | document.getElementById("eq").value = presets[id][2]; | |
53 | plot(); | |
6bef45dc DV |
54 | } |
55 | </script> | |
56 | </head> | |
e82114b2 | 57 | <body onload="preset()"> |
6bef45dc DV |
58 | <p><b>Equation: </b><br/> |
59 | <textarea cols="40" rows="10" id="eq">function(x) { | |
e82114b2 DV |
60 | return [0.1 * x, 0.1 * x + Math.sin(x), 0.1*x + Math.cos(x)]; |
61 | }</textarea><br/> | |
62 | <b>Preset functions:</b> <select id=presets onchange="preset()"> | |
63 | <option id=custom>(custom)</option> | |
64 | <option id=id>Identity</option> | |
65 | <option id=sine>Sine Wave</option> | |
66 | <option id=taylor>Taylor series</option> | |
67 | <option selected id=sawtooth>Sawtooth</option> | |
68 | </select> | |
6bef45dc | 69 | </p> |
e82114b2 | 70 | |
6bef45dc DV |
71 | <p><b>x range: </b> <input type=text width="5" id="x1" value="-10" /> |
72 | to <input type=text width="5" id="x2" value="10" /></p> | |
73 | <p><input type=button value="Plot" onClick="plot()" /></p> | |
74 | ||
d16579a0 | 75 | <div id="graph_div" style="width:1024px; height:400px;"></div> |
6bef45dc DV |
76 | </body> |
77 | </html> |