D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
pstuffa
Full window
Github gist
Axis Grid II
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { background: #2f3337 !important; } #chart { text-align: center; } .dots { fill: #53535f; } </style> </head> <body> <div id="chart"> </div> <feTurbulence id="filter" baseFrequency = ".04" numOctaves="2" seed="201"/> <script> var margin = {top: 60, right: 60, bottom: 60, left: 60}, width = window.innerHeight - margin.left - margin.right, height = window.innerHeight - margin.top - margin.bottom, numTicks = 10, numDashes = 5, numDots = 20; tickSpace = 8; var y = d3.scaleLinear() .range([height, 0]) .domain([-1,1]) var x = d3.scaleLinear() .range([0, width]) .domain([-1,1]) var svg = d3.select("#chart").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .style("filter", "url(#filter)") .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).tickSize(-height).ticks(numTicks)) .selectAll("line") .style("stroke-dasharray", function(d,i) { var dashArray = [] for (var k = 0; k < numTicks; k += 1) { dashArray.push((height/numTicks - (tickSpace/2) - (k > 0 ? (tickSpace/2) : 0))); dashArray.push(tickSpace) } return dashArray.join(",") }) svg.append("g") .attr("class", "y axis") .call(d3.axisLeft(y).tickSize(-width).ticks(numTicks)) .selectAll("line") .style("stroke-dasharray", function(d,i) { var dashArray = [] for (var k = 0; k < numTicks; k += 1) { dashArray.push((width/numTicks - (tickSpace/2) - (k > 0 ? (tickSpace/2) : 0))); dashArray.push(tickSpace) } return dashArray.join(",") }) svg.selectAll(".verticalDashes") .data(d3.range(numTicks*numDashes).map(Object)) .enter().append("line") .attr("class", "verticalDashes") .attr("y1", function(d, i) { return i*(height/numTicks/numDashes); } ) .attr("y2", function(d, i) { return i*(height/numTicks/numDashes); } ) .attr("x1", x(-.025)) .attr("x2", x(.025)) .style("stroke", "#000") .style("stroke-dasharray", function() { var unit = x(.025) - x(0); return (unit - (tickSpace/4)) + "," + (tickSpace/2) + "," + (unit - (tickSpace/4)) }) svg.selectAll(".horizontalDashes") .data(d3.range(numTicks*numDashes).map(Object)) .enter().append("line") .attr("class", "horizontalDashes") .attr("x1", function(d, i) { return i*(width/numTicks/numDashes); } ) .attr("x2", function(d, i) { return i*(width/numTicks/numDashes); } ) .attr("y1", y(-.025)) .attr("y2", y(.025)) .style("stroke", "#000") .style("stroke-dasharray", function() { var unit = x(.025) - x(0); return (unit - (tickSpace/4)) + "," + (tickSpace/2) + "," + (unit - (tickSpace/4)) }) svg.selectAll(".rowOne dots") .data(d3.range(numTicks*numDots).map(Object)) .enter().append("circle") .attr("class", "rowOne dots") .attr("cx", function(d, i) { return i*(width/numTicks/numDots); } ) .attr("cy", function(d, i) { return i % 2 == 0 ? y(.28) : y(-.28) }) .attr("r", 1); svg.selectAll(".rowTwo") .data(d3.range(numTicks*numDots).map(Object)) .enter().append("circle") .attr("class", "rowTwo dots") .attr("cx", function(d, i) { return i*(width/numTicks/numDots); } ) .attr("cy", function(d, i) { return i % 2 == 0 ? y(.44) : y(-.44) }) .attr("r", 1); </script> </body>
https://d3js.org/d3.v4.min.js