首页 > 代码库 > [D3] SVG Graphics Containers and Text Elements in D3 v4
[D3] SVG Graphics Containers and Text Elements in D3 v4
SVG is a great output format for data visualizations because of its scalability, but it comes with some idiosyncrasies and unique challenges. In this lesson we’ll learn how to use graphics containers, the SVG equivalent of a div, as well as text elements for displaying, you guessed it, text.
var scores = [ { name: ‘Alice‘, score: 96 }, { name: ‘Billy‘, score: 83 }, { name: ‘Cindy‘, score: 91 }, { name: ‘David‘, score: 96 }, { name: ‘Emily‘, score: 88 }];const bars = d3.select(‘.chart‘) .append(‘svg‘) .attr(‘width‘, 300) .attr(‘height‘, 300) .style(‘background‘, ‘white‘) .selectAll(‘g‘) // ‘g‘ works as canvas on svg .data(scores) .enter() .append(‘g‘) .attr(‘transform‘, (d, i) => ‘translate(0, ‘ + i * 33 + ‘)‘);bars.append(‘rect‘) .attr(‘width‘, d => d.score) .attr(‘class‘, ‘bar‘);bars.append(‘text‘) .text(d => d.name) .attr(‘y‘, 20);
<iframe style="width: 100%; height: 500px;" src="https://embed.plnkr.co/NRYm5dxmM4kZGt8XKBNU/" width="320" height="240"></iframe>
[D3] SVG Graphics Containers and Text Elements in D3 v4
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。