问题描述
我正在尝试根据。我已经有了基本的图表,但是当我尝试使用焦点图表放大某个区域时,我计划用于工具提示的圈子不会移动。这是我的代码
I am trying to create a Focus+Context+Tooltip graph based on d3 example of http://bl.ocks.org/1667367. I have got the basic charts working but when I try to zoom into an area using focus chart, my 'circles' which I plan to use for tooltip do not move. This is my code
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: 430, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
var data = [{
'Wed Jan 23 00:00:00 IST 2013': 3383387
}, {
'Thu Jan 24 00:00:00 IST 2013': 3883387
}, {
'Fri Jan 25 00:00:00 IST 2013': 4383387
}, {
'Sat Jan 26 00:00:00 IST 2013': 2383387
}, {
'Sun Jan 27 00:00:00 IST 2013': 5383387
}, {
'Mon Jan 28 00:00:00 IST 2013': 2283387
}];
var format = d3.time.format("%a %b %d %H:%M:%S IST %Y");
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"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brush);
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(format.parse(d3.keys(d)[0])); })
.y0(height)
.y1(function(d) { return y(d3.values(d)[0]); });
var area2 = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x2(format.parse(d3.keys(d)[0])); })
.y0(height2)
.y1(function(d) { return y2(d3.values(d)[0]); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
x.domain(d3.extent(data.map(function(d) { return format.parse(d3.keys(d)[0]); })));
y.domain([0, d3.max(data.map(function(d) { return d3.values(d)[0]; }))]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("clip-path", "url(#clip)")
.attr("d", area);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis);
context.append("path")
.datum(data)
.attr("d", area2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
focus.append("g").selectAll('dot')
.data(data)
.enter().append("svg:circle")
.attr("cx",function(d){ return x(format.parse(d3.keys(d)[0]));})
.attr("cy", function(d){ return y(d3.values(d)[0]);})
.attr("r", function(d){ return 4;})
.on('mouseover', function(d){ d3.select(this).attr('r', 8)})
.on('mouseout', function(d){ d3.select(this).attr('r', 4)});
function brush() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
focus.select("circle").attr("cx",function(d){ return x(format.parse(d3.keys(d)[0]));}).attr("cy", function(d){ return y(d3.values(d)[0]);});
}
我在这里创造了一个小提琴:。任何帮助或线索都会很棒!谢谢!
I have created a fiddle for this here: http://jsfiddle.net/PyvZ7/ . Any help or clues will be great ! Thanks !
推荐答案
我更新了小提琴:
不要忘记申请 clip-path
到该组。您的代码使用选择
而不是 selectAll
。因此,转换不适用于组内的所有圈子。另外 selectAll
是一个css3选择器。您需要确保 selectAll('。dot')
而不是 selectAll('dot')
作为后者表示标签,前者表示具有类点
的元素。
Don't forget to apply clip-path
to the group. Your code used select
instead of selectAll
. So the transforms were not applying to all the circles within the group. Also selectAll
is a css3 selector. You need to make sure you do selectAll('.dot')
instead of selectAll('dot')
as the latter would mean a tag and the former would mean elements having a class dot
.
以下是修改后的代码:
Here is the modified code:
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: 430, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
var data = [{
'Wed Jan 23 00:00:00 IST 2013': 3383387
}, {
'Thu Jan 24 00:00:00 IST 2013': 3883387
}, {
'Fri Jan 25 00:00:00 IST 2013': 4383387
}, {
'Sat Jan 26 00:00:00 IST 2013': 2383387
}, {
'Sun Jan 27 00:00:00 IST 2013': 5383387
}, {
'Mon Jan 28 00:00:00 IST 2013': 2283387
}];
var format = d3.time.format("%a %b %d %H:%M:%S IST %Y");
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"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brush);
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(format.parse(d3.keys(d)[0])); })
.y0(height)
.y1(function(d) { return y(d3.values(d)[0]); });
var area2 = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x2(format.parse(d3.keys(d)[0])); })
.y0(height2)
.y1(function(d) { return y2(d3.values(d)[0]); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
x.domain(d3.extent(data.map(function(d) { return format.parse(d3.keys(d)[0]); })));
y.domain([0, d3.max(data.map(function(d) { return d3.values(d)[0]; }))]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("clip-path", "url(#clip)")
.attr("d", area);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis);
context.append("path")
.datum(data)
.attr("d", area2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
var circlegroup = focus.append("g");
circlegroup.attr("clip-path", "url(#clip)");
circlegroup.selectAll('.dot')
.data(data)
.enter().append("circle")
.attr('class', 'dot')
.attr("cx",function(d){ return x(format.parse(d3.keys(d)[0]));})
.attr("cy", function(d){ return y(d3.values(d)[0]);})
.attr("r", function(d){ return 4;})
.on('mouseover', function(d){ d3.select(this).attr('r', 8)})
.on('mouseout', function(d){ d3.select(this).attr('r', 4)});
function brush() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
circlegroup.selectAll(".dot").attr("cx",function(d){ return x(format.parse(d3.keys(d)[0]));}).attr("cy", function(d){ return y(d3.values(d)[0]);});
}
这篇关于移动散点图圈与上下文缩放和刷D3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!