D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
zikes
Full window
Github gist
Exponential Regression Line Using numbers.js & d3.js
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Exponential Regression Line Using numbers.js & d3.js</title> <script src="https://d3js.org/d3.v3.min.js"></script> <script src="numbers.js"></script> <style> body {font: 10px sans-serif;} .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } .line.regression { stroke: red; } </style> </head> <body> <script> function rnd(a,b){return Math.floor(Math.random() * b) + a;} var data = [], i; for(i = 0; i < 50; i++){data.push(rnd(1000,5000))} for(i = 0; i < 100; i++){data.push(rnd(50,1000));} for(i = 0; i < 200; i++){data.push(rnd(50,200));} data.sort(function(a,b){return b-a;}); var regression = numbers.statistic.exponentialRegression(data); var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var svg = d3.select('body') .append('svg') .attr('width',width+margin.left+margin.right) .attr('height',height+margin.top+margin.bottom) .append('g') .attr('transform','translate('+margin.left+','+margin.top+')'); var x = d3.scale.linear().range([0,width]).domain([0,data.length]); var y = d3.scale.linear().range([height,0]).domain(d3.extent(data)); var xAxis = d3.svg.axis().scale(x).orient('bottom'); var yAxis = d3.svg.axis().scale(y).orient('left'); var line = d3.svg.line().x(function(d,i){return x(i)}).y(function(d,i){return y(d)}); var reg_line = d3.svg.line().x(function(d,i){return x(i)}).y(function(d,i){return y(regression(i))}); svg.append('g') .attr('class','x axis') .attr('transform','translate(0,'+height+')') .call(xAxis); svg.append('g') .attr('class','y axis') .call(yAxis); svg.append('path') .datum(data) .attr('class','line') .attr('d',line); svg.append('path') .datum(data) .attr('class','regression line') .attr('d',reg_line); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js