javascript - How to fit a polynomial curve with D3? -


i have following d3 code in index.html:

<!doctype html> <svg width="960" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script>  var svg = d3.select("svg"),     margin = {top: 60, right: 20, bottom: 40, left: 50},     width = +svg.attr("width") - margin.left - margin.right,     height = +svg.attr("height") - margin.top - margin.bottom,     g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");  var x = d3.scalelinear()     .rangeround([0, width]);  var y = d3.scalelinear()     .rangeround([height, 0]);  var line = d3.line()     .x(function(d) { return x(d.error); })     .y(function(d) { return y(d.close); });  d3.tsv("data.tsv", function(d) {   d.error = d.error;   d.close = +d.close;   return d; }, function(error, data) {   if (error) throw error;    x.domain(d3.extent(data, function(d) { return d.error; }));   y.domain(d3.extent(data, function(d) { return d.close; }));    g.append("g")       .attr("transform", "translate(0," + height + ")")       .call(d3.axisbottom(x))       .select(".domain")       .remove();    g.append("g")       .call(d3.axisleft(y))       .append("text")       .attr("fill", "#000")       .attr("transform", "rotate(-90)")       .attr("y", 6)       .attr("dy", "0.71em")       .attr("text-anchor", "end")       .text("error (%)");    g.append("path")       .datum(data)       .attr("fill", "none")       .attr("stroke", "steelblue")       .attr("stroke-linejoin", "round")       .attr("stroke-linecap", "round")       .attr("stroke-width", 1.5)       .attr("d", line); });  </script> 

the data.tsv looks this:

error close 100 93.24 200 -85.35 300 53.89 400 -18.57 500 198.1 600 -8.4 700 98.623 800 9.89 900 118.56 1000  98.71 

i fit curve these data points. right code connects data points straight lines. looks this:

enter image description here

i wondering how can fit polynomial curve instead.

you have use line.curve([curve]). parameter pass curve type part of d3. link contains example: https://bl.ocks.org/pstuffa/26363646c478b2028d36e7274cedefa6

and api reference: https://github.com/d3/d3-shape/blob/master/readme.md#line_curve


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -