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