- Expert Data Visualization
- Jos Dirksen
- 285字
- 2021-07-09 18:22:48
Adding an x-axis with years
The first thing we need to do is add an axis that will show the years. We do this with the addAxis function:
function addAxis(yIncomeScale, yIndexedScale, xScale, xRangeAdjusted) {
...
var bottomAxis = d3.axisBottom().scale(xScale).ticks(15,"f");
var bottomAxisChart = chart.append("g")
.attr('transform', 'translate( 0 ' + yIndexedScale(100) + ')')
.call(bottomAxis);
bottomAxisChart.selectAll('text')
.attr("transform", "translate(-16 14) rotate(-70)");
...
}
For the center axis, we use the d3.axisBottom function to create an axis based on our xScale. With the ticks function, we specify that we want to have 15 ticks (one for every two years), and we want to format the ticks with a fixed point notation (f).
D3 has an additional module that can be used to format numbers. You can use this library to specify scientific, fixed point, binary, octal, and many other types of notation. Besides notation types, it also contains a large set of other format options to make sure the number output looks exactly as you want. In this book, we'll not pe into the details of the different options. You can look at the excellent D3 API documentation to see what is possible: https://github.com/d3/d3-format#locale_format.
After defining the axis, we add it to a specific group, which we position at the center of the graph by setting the transform property and translating this element using yIndexedScale(100) to position it in the vertical center of the chart. The last thing we do is that we change the position of the text element of the axis so that they are rotated and moved a little bit (translate(-16 14) rotate(-70)). We do this to avoid the labels overlapping with the axis on the right side.
Next, we add the axis on the right side.
- Java EE 7 Development with NetBeans 8
- Mastering macOS Programming
- Linux Device Drivers Development
- 基于Struts、Hibernate、Spring架構的Web應用開發
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- App Inventor創意趣味編程進階
- 零基礎學HTML+CSS
- Java EE 7 with GlassFish 4 Application Server
- Magento 2 Beginners Guide
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施
- 交互設計師成長手冊:從零開始學交互
- Mastering OpenStack
- Learning ROS for Robotics Programming
- Learning Perforce SCM
- Python程序員面試算法寶典