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