javascript - How to draw many functions with a loop in D3? -


i've got parabola , derivative line in point, , want draw lines approximations. data (points) right arrays. don't know how draw these approximations d3 using loop. obtain last approximation (in array) here's code:

var x = d3.range(-4., 4.1, 0.1) var m = d3.range(2.5, 3.1, 0.1)  myfunction = x => -math.pow(x, 2) + 2 * x myderiv = x => -2 * x + 2 mytangent = x => -4 * x + 9 myslope = x => (-math.pow(x, 2) + 2 * x + 3) / (x - 3) mylim = (x, m) => myslope(m) * x - 3 * myslope(m) - 3  var y = new array() var y1 = new array() var y2 = new array() var y3 = new array()  (var = 0 ; < x.length ; i++) {     y[i] = myfunction(x[i])     y1[i] = myderiv(x[i])     y2[i] = mytangent(x[i])     (var j = 0 ; j < m.length ; j++)         y3[i] = mylim(x[i], m[j]) } var dataset = [] (var j = 0; j < x.length; j++) {     dataset[j] = []     dataset[j][0] = x[j]     dataset[j][1] = y[j] } var dataset1 = [] (var j = 0; j < x.length; j++) {     dataset1[j] = []     dataset1[j][0] = x[j]     dataset1[j][1] = y1[j] } var dataset2 = [] (var j = 0; j < x.length; j++) {     dataset2[j] = []     dataset2[j][0] = x[j]     dataset2[j][1] = y2[j] } var dataset3 = [] (var j = 0; j < x.length; j++) {     dataset3[j] = []     dataset3[j][0] = x[j]     dataset3[j][1] = y3[j] } console.log(dataset3) var w = 500 var h = 500 var padding = 50  var xscale = d3.scalelinear()             .domain([d3.min(x, function(d) { return d }), d3.max(x, function(d) { return d })])             .range([padding, w - padding])  var yscale = d3.scalelinear()              .domain([-4, 4])              .range([h - padding, padding])  function mycanvas() {     var svg = d3.select('.myfunction')             .append('svg')             .attr('width', w)             .attr('height', h)     svg.append('rect')         .attr('width', '100%')         .attr('height', '100%')         .style('fill', '#ffcc99')      // define axis     var xaxis = d3.axisbottom().scale(xscale).ticks(9)     var yaxis = d3.axisleft().scale(yscale).ticks(9)      // create axis     svg.append('g')         .attr('class', 'axis')         .attr('transform', 'translate(0,' + (h - padding) + ')')         .call(xaxis)     svg.append('g')         .attr('class', 'axis')         .attr('transform', 'translate(' + padding + ', 0)')         .call(yaxis)      // define , plotting function     var line = d3.line()         .x(function(d) { return xscale(d[0])})         .y(function(d) { return yscale(d[1])})      svg.append('path')     .attr('d', line(dataset))     .attr('stroke', 'blue')     .attr('fill', 'none')     svg.append('path')     .attr('d', line(dataset2))     .attr('stroke', 'green')     .attr('fill', 'none')     svg.append('path')     .attr('d', line(dataset3))     .attr('stroke', 'red')     .attr('fill', 'none') }  function main() {     mycanvas() }  window.onload = main 

every line must depends of "m" valor

could answer helpful you?

you can create function return different function (line generator) per each of data columns:

https://stackoverflow.com/a/40555785/343261


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -