Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

创建有文字的柱子(g / text) #12

Open
ckinmind opened this issue Jul 17, 2017 · 1 comment
Open

创建有文字的柱子(g / text) #12

ckinmind opened this issue Jul 17, 2017 · 1 comment

Comments

@ckinmind
Copy link
Owner

ckinmind commented Jul 17, 2017

var scores = [
  { name: 'Alice', score: 96 },
  { name: 'Billy', score: 83 },
  { name: 'Cindy', score: 91 },
  { name: 'David', score: 96 },
  { name: 'Emily', score: 88 }
];

var bar = d3.select('.chart')
  .append('svg')
    .attr('width', 225)
    .attr('height', 300)
  .selectAll('g')
  .data(scores)
  .enter()
    .append('g')
    .attr('transform', (d, i) => 'translate(0, ' + i * 33 + ')');

bar.append('rect')
    .style('width', d => d.score)
    .attr('class', 'bar');

bar.append('text')
  .attr('y', 20)
  .text(function (d) {
    return d.name;
  });

注意:

  1. 元素g是用来组合对象的容器。添加到g元素上的变换会应用到其所有的子元素上。添加到g元素的属性会被其所有的子元素继承。此外,g元素也可以用来定义复杂的对象
  2. g元素没有x,y属性,所以如果要移动g属性通过transform
  3. 显示文字需要添加text属性,这个是svg中的一个属性
@ckinmind
Copy link
Owner Author

dingtalk20170718095542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant