Skip to content

Instantly share code, notes, and snippets.

@sjengle
Created April 3, 2016 04:10
Show Gist options
  • Save sjengle/d079aefb45c22914760f2d6846985db8 to your computer and use it in GitHub Desktop.
Save sjengle/d079aefb45c22914760f2d6846985db8 to your computer and use it in GitHub Desktop.
Adding Click for Tick Labels
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.axis path, .axis line { fill: none; stroke: black; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see!
var svg = d3.select("body")
.append("svg")
.attr("width", 950)
.attr("height", 500);
var scale = d3.scale.linear().domain([0, 10]).range([0, 900]);
var axis = d3.svg.axis().scale(scale);
var g = svg.append("g").attr("transform", "translate(15, 200)");
g.attr("class", "axis");
g.call(axis);
d3.selectAll(".tick text").on("click", doclick);
function doclick() {
if (scale.domain()[1] - scale.domain()[0] == 10) {
scale.domain([0, 100]);
}
else {
scale.domain([0, 10]);
}
g.call(axis);
d3.selectAll(".tick text").on("click", doclick);
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment