Skip to content

Instantly share code, notes, and snippets.

@pstuffa
Last active March 26, 2017 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstuffa/70142434746025fd9ee942a169406912 to your computer and use it in GitHub Desktop.
Save pstuffa/70142434746025fd9ee942a169406912 to your computer and use it in GitHub Desktop.
Axis Grid
license: mit

Template for a more intricate axis grid. Working to make small line breaks in between grid intersections. Inspired by the look of this.

Built with blockbuilder.org

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
</style>
</head>
<body>
<div id="chart"> </div>
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment