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