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

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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);

 

 

以上是关于[D3] SVG Graphics Containers and Text Elements in D3 v4的主要内容,如果未能解决你的问题,请参考以下文章

SVG基础图形和D3.js

D3 在调整大小窗口上更改 SVG 尺寸

d3:结合canvas + svg用于视网膜

D3 SVG 路径未填充线性比例渐变

D3 svg图表无法在IE中显示

d3 svg 阴影差异