You're reading from Data Visualization with D3.js Cookbook
D3 was initially released without the built-in support of the Axis component. This situation did not last long; since axes are the universal building blocks in many Cartesian coordinate system-based visualization projects, it quickly became clear that D3 needs to provide built-in support for axes. Therefore, it was introduced quite early on and is continuously being enhanced ever since it was released. In this chapter, we will explore the usage of the Axis component and some related techniques.
In this recipe, we will focus on introducing the basic concepts and supports of the Axis component in D3 while covering different types and features of Axis as well as their SVG structures.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter5/basic-axes.html
Let's first take a look at the following code sample:
<div class="control-group"> <button onclick="renderAll('bottom')"> horizontal bottom </button> <button onclick="renderAll('top')"> horizontal top </button> <button onclick="renderAll('left')"> vertical left </button> <button onclick="renderAll('right')"> vertical right </button> </div> <script type="text/javascript"> var height = 500, width = 500, margin = 25, offset = 50, axisWidth = width...
We already saw how to use the ticks
function in the previous recipe. This is the simplest ticks-related customization you can do on a D3 axis. In this recipe, we will cover some of the most common and useful ticks-related customizations with D3 axis.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter5/ticks.html
In the following code example, we will customize the sub-ticks, padding, and formatting of its label. Let's take a look at the code snippet first:
<script type="text/javascript"> var height = 500, width = 500, margin = 25, axisWidth = width - 2 * margin; var svg = d3.select("body").append("svg") .attr("class", "axis") .attr("width", width) .attr("height", height); var scale = d3.scale.linear() .domain([0, 100]) .range([0, axisWidth]); var axis = d3.svg...
Quite often, we need horizontal and vertical grid lines to be drawn in consistency with ticks on both x and y axes. As we have shown in the previous recipe, typically we don't have or don't want to have precise control of how ticks are rendered on D3 axes. Therefore, we might not know how many ticks are present and their values, before they are rendered. This is especially true if you are building a re-usable visualization library where it is impossible to know the tick configuration ahead of time. In this recipe, we will explore some useful techniques of drawing consistent grid lines on Axis without actually needing to know the tick values.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter5/grid-line.html
In some cases, the scale used by axes might change when triggered by user interaction or changes from data feeds. For example, a user might change the time range for the visualization. This kind of change also needs to be reflected by rescaling the axes. In this recipe, we will explore how this can be achieved dynamically while also redrawing the grid lines associated with each tick.
Open your local copy of the following file in your web browser:
https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter5/rescaling.html
Here is the code showing how to perform dynamic rescaling:
<script type="text/javascript"> var height = 500, width = 500, margin = 25, xAxis, yAxis, xAxisLength, yAxisLength; var svg = d3.select("body").append("svg") .attr("class", "axis") .attr("width", width) .attr("height", height); function renderXAxis(){ ...