[英]d3.js/css: Positioning elements within div
I've created a line chart in d3.js with a panel at the bottom (a div outside of the chart) with certain elements like text and img within individual groups. 我已经在d3.js中创建了一个折线图,在其底部(面板位于图的div处)带有一个面板,其中某些元素(如文本和img)位于各个组中。 The problem I'm having is positioning these elements within each group and they just seem to go after each other:
我遇到的问题是将这些元素放置在每个组中,它们似乎互相追逐:
How would I, for instance, have them looking like I did previously but for an external div? 例如,对于外部div,我将如何使它们看起来像以前一样? Would I be looking to use CSS formatting?
我会使用CSS格式吗?
(This is how I had them looking previously, which is what I'm sort of looking for) (这就是我以前让他们看的方式,这就是我要寻找的东西)
Also, this is the code I have for appending each element to their respective groups: 另外,这是我将每个元素附加到其各自组的代码:
for(item in tweet_list) {
var tweet = tweet_list[item];
d3.select(".panel")
.append("g")
.attr("id", function(){return "p"+tweet['id_str'];})
.attr("position", "absolute")
.style("display", "block")
.classed("panel-body", true);
var group = d3.selectAll("#p"+tweet['id_str']+"");
group.append("text")
.text(function(){
var tweet_created_format = d3.timeFormat("%-I:%M%p, %e %b %Y")(d3.timeParse("%a %b %d %H:%M:%S %Z %Y")(tweet['created_at']));
return "@"+tweet['user']['screen_name']+" ("+tweet_created_format+")";
});
group.append("img")
.attr("width", 20)
.attr("height", 20)
.attr("src", function(d){return tweet['user']['profile_image_url']});
group.append("text")
.text(function(){
return tweet['text'];
});
group.append("img")
.attr("src", "{{ url_for('static', filename = 'img/twitter-like.svg') }}");
group.append("img")
.attr("src", "{{ url_for('static', filename = 'img/twitter-retweet.svg') }}");
group.append("text")
.text(function(){
return tweet['retweet_count'];
});
group.append("text")
.text(function(){
return tweet['favorite_count'];
});
}
You will definitely need to use css to style the tweets. 您肯定需要使用CSS设置推文的样式。 Also one should not mix svg and html elements eg.
也不应混用svg和html元素,例如。 append 'g' to a 'div'.
将“ g”附加到“ div”。 Finally in d3 you can (and should) bind data to selections instead of looping manually.
最后,在d3中,您可以(并且应该)将数据绑定到选择内容,而不是手动循环。
What you want is something like this: 您想要的是这样的:
var tweetDivs = d3.select(".panel").selectAll('div.panel-body')
.data(tweet_list)
.enter()
.append("div")
.attr("id", function(d){return "p"+d['id_str'];})
.classed("panel-body", true);
tweetDivs.append("img")
.attr("width", 20)
.attr("height", 20)
.attr("src", function(d){return d['user']['profile_image_url']})
.attr('style', 'display: inline-block;')
tweetDivs.append("p")
.text(function(d){
return "@"+d['user']['screen_name'];//+" ("+tweet_created_format+")";
})
.attr('style', 'display: inline-block;')
tweetDivs.append("p")
.text(function(d){
return d['text'];
})
tweetDivs.append("img")
.attr("src", "{{ url_for('static', filename = 'img/twitter-like.svg') }}");
tweetDivs.append("img")
.attr("src", "{{ url_for('static', filename = 'img/twitter-retweet.svg') }}");
tweetDivs.append("p")
.text(function(d){
return d['retweet_count'];
});
tweetDivs.append("p")
.text(function(d){
return d['favorite_count'];
});
EDIT: Fixed invisible reference to html and svg tags. 编辑:修复了对html和svg标签的不可见引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.