X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;ds=sidebyside;f=gallery%2Fplotter.js;h=d077b0965def365a473812ee0fe56c4fe9b95553;hb=1357d81ebb85b87b69a9de0eb01c5d8cb43774d2;hp=7a60aba87c56560f6ca6c8cd11375152e222ac18;hpb=d0d1f76beeadb9f41d0194cf689ea93c4987a565;p=dygraphs.git diff --git a/gallery/plotter.js b/gallery/plotter.js index 7a60aba..d077b09 100644 --- a/gallery/plotter.js +++ b/gallery/plotter.js @@ -5,37 +5,38 @@ Gallery.register( name: 'Function Plotter', title: 'Define your data with functions', setup: function(parent) { - parent.innerHTML = - "
Equation:
\n" +
- "
\n" +
- "Preset functions: \n" +
- "
x range: \n" + - " to
\n" + - " \n" + - "\n" + - " "; + parent.innerHTML = [ + "Equation:
",
+ "
",
+ "Preset functions: ",
+ "
x range: ", + "to
", + "", + "", + ""].join("\n"); }, run: function() { - window.preset = function() { - var sel = document.getElementById("presets").selectedIndex; - var id = document.getElementById("presets").options[sel].id; - var presets = { - 'id': [ -10, 10, 'function(x) {\n return x;\n}' ], - 'sine': [ -10, 10, 'function(x) {\n return Math.sin(x);\n}' ], - 'taylor': [ -3, 3, 'function(x) {\n return [Math.cos(x), 1 - x*x/2 + x*x*x*x/24];\n}' ], - '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}' ] - }; + var select = document.getElementById("presets"); + var presets = { + 'id': [ -10, 10, 'function(x) {\n return x;\n}' ], + 'sine': [ -10, 10, 'function(x) {\n return Math.sin(x);\n}' ], + 'taylor': [ -3, 3, 'function(x) {\n return [Math.cos(x), 1 - x*x/2 + x*x*x*x/24];\n}' ], + '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}' ] + }; + select.onchange = function() { + var sel = select.selectedIndex; + var id = select.options[sel].id; if (id == "custom") { return; } document.getElementById("x1").value = presets[id][0]; @@ -44,7 +45,8 @@ Gallery.register( plot(); } - window.plot = function() { + var plotButton = document.getElementById("plot"); + var plot = function() { var eq = document.getElementById("eq").value; eval("fn = " + eq); @@ -71,6 +73,7 @@ Gallery.register( g = new Dygraph(graph, data); } - window.plot(); + plotButton.onclick = plot; + plot(); } });