- Expert Data Visualization
- Jos Dirksen
- 210字
- 2021-07-09 18:22:44
Adding the axis on the top and bottom
The final step we need to take to get the figure from the beginning of this section is to add the top and bottom axes. D3 provides you with a d3.axis<orientation> function, which allows you to create an axis at the bottom, top, left, or right side. When creating an axis, we pass in a scale (which we also used for the width of the rectangles), and tell D3 how the axis should be formatted. In this case, we want 20 ticks, and use the s formatting, which tells D3 to use the international system of units (SI).
This means that D3 will use metric prefixes to format the tick values (more info can be found here: https://en.wikipedia.org/wiki/Metric_prefix).
var bottomAxis = d3.axisBottom().scale(yScale).ticks(20, "s");
var topAxis = d3.axisTop().scale(yScale).ticks(20, "s");
chart.append("g")
.attr('transform', 'translate( 0 ' + both.length * (barWidth + barMargin) + ')')
.call(bottomAxis);
chart.append("g")
.attr('transform', 'translate( 0 ' + -barMargin + ' )')
.call(topAxis);
And with that, we've recreated the example we saw at the beginning of this section:

If you look back at the code we showed at the beginning of this section, you can see that we only need a small number of lines of code to create a nice visualization.
- ThinkPHP 5實(shí)戰(zhàn)
- Building a Game with Unity and Blender
- Go語言高效編程:原理、可觀測(cè)性與優(yōu)化
- Visual Basic程序設(shè)計(jì)教程
- 少年輕松趣編程:用Scratch創(chuàng)作自己的小游戲
- Oracle BAM 11gR1 Handbook
- PLC編程及應(yīng)用實(shí)戰(zhàn)
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Learning Raspbian
- App Inventor創(chuàng)意趣味編程進(jìn)階
- 小型編譯器設(shè)計(jì)實(shí)踐
- Vue.js應(yīng)用測(cè)試
- C語言程序設(shè)計(jì)教程
- 快速搞定Spring Boot+Vue全棧開發(fā)
- Boost.Asio C++ Network Programming Cookbook