【发布时间】:2014-02-10 17:58:37
【问题描述】:
我对如何将工具提示添加到我的线性 d3 图表中变得很疯狂,以便用户可以看到多年来性能的具体价值。 这是小提琴:http://jsfiddle.net/d9Lya/ 这是代码
// define dimensions of graph
var m = [20, 80, 80, 80]; // margins
var w = 650 - m[1] - m[3]; // width
var h = 500 - m[0] - m[2]; // height
var data = [30, 28, 33] ;
var years = [2010, 2011, 2012] ;
var data2 = [100, 200, 200] ;
var years2 = [2009, 2010, 2011] ;
var alldata = data.concat(data2);
var allyears = years.concat(years2);
var data3 = [200, 220, 300] ;
var years3 = [2011, 2012, 2013] ;
var alldata = data.concat(data2, data3);
var allyears = years.concat(years2, years3);
//unique vals functin
var unique = function(origArr) {
var newArr = [],
origLen = origArr.length,
found,
x, y;
for ( x = 0; x < origLen; x++ ) {
found = undefined;
for ( y = 0; y < newArr.length; y++ ) {
if ( origArr[x] === newArr[y] ) {
found = true;
break;
}
}
if ( !found) newArr.push( origArr[x] );
}
return newArr;
};
allyears = unique(allyears);
var x = d3.scale.linear().domain([d3.min(allyears), d3.max(allyears)]).range([0,w]);
var y = d3.scale.linear().domain([0, (d3.max(alldata))*1.3]).range([h, 0]);
var line = d3.svg.line()
.x(function(d,i) {
return x(years[i]);
})
.y(function(d) {
return y(d);
})
var line2 = d3.svg.line()
.x(function(d,i) {
return x(years2[i]);
})
.y(function(d) {
return y(d);
})
var line3 = d3.svg.line()
.x(function(d,i) {
return x(years3[i]);
})
.y(function(d) {
return y(d);
})
// Add an SVG element with the desired dimensions and margin.
var graph = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
// create xAxis
var xAxis = d3.svg.axis().scale(x).ticks(allyears.length).tickSize(-h).tickSubdivide(true);
// Add the x-axis.
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// create left yAxis
var yAxisLeft = d3.svg.axis().scale(y).ticks(8).orient("left");
// Add the y-axis to the left
graph.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(-25,0)")
.call(yAxisLeft);
graph.append("svg:text")
.attr("class", "title1")
.attr("x", (w/2))
.attr("y", 0)
.text("Performance")
.style({ "stroke": "Black", "fill": "Black", "stroke-width": "1px"})
.attr("text-anchor", "middle")
.style("font-size", "16px") ;
graph.append("svg:path").attr("d", line(data)).style("stroke", "steelblue");
graph.append("svg:text")
.attr("class", "title1")
.attr("x", 0)
.attr("y", 30)
.text("TEAM A")
.style({ "stroke": "steelblue", "fill": "steelblue", "stroke-width": "0px"});
graph.append("svg:path")
.attr("d", line2(data2)).style("stroke", "green")
;
graph.append("svg:text")
.attr("class", "title2")
.attr("x", 0)
.attr("y", 50)
.text("TEAM B")
.style({ "stroke": "Green", "fill": "Green", "stroke-width": "0px"});
graph.append("svg:path").attr("d", line3(data3)).style("stroke", "red");
graph.append("svg:text")
.attr("class", "title3")
.attr("x", 0)
.attr("y", 70)
.text("team C")
.style({ "stroke": "Red", "fill": "Red", "stroke-width": "0px"});
我实际上是 D3 的新手,我已经看到了几个其他示例,但我无法在我的 js 代码中重现它们。谁能帮我? 问候
【问题讨论】:
-
你见过this example吗?
-
是的,但要让它发挥作用还很遥远。我实际上可以使用以下方法添加工具提示: .on("mouseover", function (d, i) { div.transition() .duration(200) .style("opacity", .9); div.html("我们在这里" ) .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY - 28) + "px"); })跨度>
-
但我真的不知道如何获取 Y 数据以显示真实值
-
您是将鼠标悬停事件添加到 lines 还是添加到不可见的圆圈?只有圆圈可以轻松访问各个数据点——线的数据是完整的数组,要确定用户的鼠标在线上的位置需要额外的计算。
-
谢谢我没有设置隐形圆圈,我得到处找这个词..