[英]multiple d3 graphs in one svg
i've found tons of answers about multiple graphs on the same page, but i already have that. 我在同一页面上找到了关于多个图形的大量答案,但是我已经知道了。
what i want to add now is a second set of bars inside the same graph (the graph shows a percentage for every day, and i want to add a time component too.) so: both graphs in the same svg, overlapping each other, to see the comparison. 我现在要添加的是同一张图内的第二组条形图(该图显示了每天的百分比,我也想添加一个时间分量。)因此:同一svg中的两个图相互重叠,看看比较。
it currently looks like this 目前看起来像这样
how can i insert a second graph over/besides that? 我如何在/之外插入第二张图? i didn't find any resources on that
我没有找到任何资源
If I understand your question correctly, the intention is to draw 'level1', 'level2', 'level3' bars in the same chart container. 如果我正确理解了您的问题,目的是在同一图表容器中绘制“ level1”,“ level2”,“ level3”条。
There are possibly two approaches: 可能有两种方法:
One is to rearrange your source data, so that you can draw a group bar chart . 一种是重新排列源数据,以便可以绘制组条形图 。
The other is to append "rect" in the same svg container based on same x axis, sth like this . 另一种方法是根据相同的x轴在同一svg容器中附加“矩形”,就像这样 。 There are 2 sets of bars on the same chart:
同一张图表上有2条柱线:
var svg = d3.select(".main").append("svg")
.attr({
"width": "800",
"height": "600"
})
var xScale = d3.scale.ordinal()
.domain(["a","b","c"])
.rangeRoundBands([0,600], .5)
var yScale = d3.scale.linear()
.domain([25, 80])
.range([600,0])
var data1 = [
{type: "a", value: 30},
{type: "b", value: 40},
{type: "c", value: 50},
];
var data2 = [
{type: "a", value: 35},
{type: "b", value: 48},
{type: "c", value: 60},
];
var rect_type1 =svg.selectAll(".rect1")
.data(data2)
rect_type1.enter().append("rect")
.attr({
"x": function(d){return xScale(d.type)},
"y": function(d){return yScale(d.value)},
"width": xScale.rangeBand(),
"height": function(d){return 600 - yScale(d.value)},
})
.attr("fill", "teal")
//.attr("fill-opacity", "0.5")
.attr("class", "rect1")
var rect_type2 =svg.selectAll(".rect2")
.data(data1)
rect_type2.enter().append("rect")
.attr({
"x": function(d){return xScale(d.type)},
"y": function(d){return yScale(d.value)},
"width": xScale.rangeBand(),
"height": function(d){return 600 - yScale(d.value)},
})
.attr("fill", "pink")
.attr("fill-opacity", "0.5")
.attr("class", "rect2")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.