码迷,mamicode.com
首页 > 其他好文 > 详细

[D3] SVG Graphics Containers and Text Elements in D3 v4

时间:2017-08-04 21:26:24      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:data   container   http   https   gre   const   hit   app   contain   

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);

 

 

[D3] SVG Graphics Containers and Text Elements in D3 v4

标签:data   container   http   https   gre   const   hit   app   contain   

原文地址:http://www.cnblogs.com/Answer1215/p/7286928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!