官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 瓮安县| 新绛县| 招远市| 伊金霍洛旗| 宁远县| 拜泉县| 河西区| 定日县| 汪清县| 昆明市| 桃园县| 醴陵市| 依安县| 淮阳县| 高平市| 河南省| 长沙县| 郯城县| 永登县| 阿巴嘎旗| 贺州市| 芒康县| 房产| 秦安县| 中卫市| 东光县| 乌拉特后旗| 曲沃县| 泗阳县| 宁德市| 龙口市| 临城县| 昌黎县| 拉萨市| 共和县| 龙门县| 潢川县| 获嘉县| 左云县| 河南省| 社会|