D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
curiousYi
Full window
Github gist
D3 Playground
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:50px;right:50px;bottom:0;left:50px; } svg { width: 100%; height: 100%; } </style> </head> <body> <svg> </svg> <script> // Feel free to change or delete any of the code you see in this editor! const data = [200, 150, 200, 300, 325, 200], rectWidth = 50, height = 300; const svg = d3.select('svg'); const currentSelection = svg.selectAll('rect') .data(data) .enter().append('rect') .attr('x', (d, i) => i * rectWidth) .attr('y', d => height - d) .attr('width', rectWidth) .attr('height', d => d) .attr('fill', 'blue') .attr('stroke', '#fff') .attr('transform', 'translate(40,20)'); //Addressing the issue of making things fit on the screen //eg super small values or large humonguous values that go off-screen const extent = d3.extent(data, d => d); console.log(extent); const yScale = d3.scaleLinear() .domain(extent) //input .range([height, 0]) //output //Now I need to do scale axis const yAxis = d3.axisLeft() .scale(yScale); d3.select('svg') .append('g') .attr('transform', 'translate(40,20)') .call(yAxis); /* const data = [100, 200, 250, 125, 175] const rectWidth = 50; const height = 300; d3.selectAll('rect') .data(data) .attr('x', (d, i) => i * rectWidth) .attr('y', d => height - d) .attr('width', rectWidth) .attr('height', d => d) .attr('fill', 'blue') .attr('stroke', '#fff'); */ </script> </body>
https://d3js.org/d3.v4.min.js