function update_graph(g) { var payload = {i: $('index').value, s: $('series').value, t: $('tenor').value, w: $('what').value}; query("_data_indices", payload, function(data){ data = JSON.parse(data); var newdata = data["data"].map(function(e){ f = e.slice(1); f.unshift(new Date(e[0])); return f}); g.updateOptions({'file': newdata, 'labels': data['labels']}); }); }; document.addEventListener("DOMContentLoaded", function() { var g = new Dygraph($("graph"), [], // path to CSV file { labels: [], title: "Index quotes", showRoller: true, legend: "always"}); ["index", "series", "tenor", "what"].forEach(function(id) { $(id).addEventListener('change', function(){ update_graph(g)}); }) update_graph(g); });