我通过刷牙解决了Focus + Context的问题(bl.ocks.org/mbostock/1667367#index.html) 当我加载它时工作得很好,但是当我选择要在主图表中显示的区域时,我在主图表中什么都没有
在选择区域之前: http://puu.sh/hdB8a/f112d63d12.png
选择区域后: http://puu.sh/hdBa0/c744f6e914.png
代码:(变量数据是包含具有值和日期属性的对象的数组)
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: this.h-70, right: 10, bottom: 20, left: 40},
width = this.w - margin.left - margin.right,
height = this.h - margin.top - margin.bottom,
height2 = this.h - margin2.top - margin2.bottom;
var parseDate = d3.time.format("%b %Y").parse;
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(6).tickSize(2),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom").ticks(6).tickSize(2),
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(2);
var brush = d3.svg.brush()
.x(x2)
.on("brush", brushed);
var area = d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(new Date(d.date)); })
.y0(height)
.y1(function(d) { return y(d.value); });
var area2 = d3.svg.area()
.interpolate("basis")
.x(function(d) { return x2(new Date(d.date)); })
.y0(height2)
.y1(function(d) { return y2(d.value); });
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("class", "focus")
.attr("fill","none")
.attr("stroke","#000")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("class", "context")
.attr("fill","none")
.attr("stroke","#000")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
data.sort(function(a, b) {
return new Date(a.date).getTime() - new Date(b.date).getTime();
});
x.domain(d3.extent(data.map(function(d) { return new Date(d.date); })));
y.domain([d3.min(data.map(function(d) { return d.value; })), d3.max(data.map(function(d) { return d.value; }))]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area)
.attr("clip-path","url(#clip)")
.attr("stroke",chartcolor);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.attr("stroke","none")
.attr("fill",chartcolor)
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.attr("stroke","none")
.attr("fill",chartcolor)
.call(yAxis);
context.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area2)
.attr("stroke",chartcolor)
.attr("clip-path","url(#clip)");
context.append("g")
.attr("class", "x axis")
.attr("stroke","none")
.attr("fill",chartcolor)
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.attr("stroke","none")
.attr("fill",chartcolor)
.attr("fill-opacity","0.125")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
function brushed() {
x.domain(brush.empty() ? x2.domain() : brush.extent() );
focus.select(".area").attr("d", area);
focus.select(".x.axis").call(xAxis);
}
感谢您的帮助。
编辑:
我解决了我的问题,而不是d3.svg.area(),我使用d3.svg.line()
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(new Date(d.date))+margin.left+margin.right; })
.y(function(d) { return y(Number(d.value))+margin.top+margin.bottom; });
var line2 = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x2(new Date(d.date))+margin2.left+margin2.right; })
.y(function(d) { return y2(Number(d.value))+margin2.top+margin2.bottom; });
行(数据)而不是数据(数据)
focus.append("path")
.attr("class", "line")
.attr("d", line(data))
.attr("clip-path","url(#clip)")
.attr("stroke",chartcolor);
context.append("path")
.attr("class", "line")
.attr("d", line2(data))
.attr("stroke",chartcolor)
.attr("clip-path","url(#clip)");
感谢您寻找