在将d3.tip方法添加到代码中之前,将显示条形图。但是,在添加d3.tip方法后,没有显示任何内容,并且此错误显示在控制台中。如何解决此问题并将d3.tip添加到条形图中?未捕获的TypeError:对象#没有方法'提示'
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.y.axisRight text {
fill: orange;
}
.y.axisLeft text {
fill: steelblue;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar1 {
fill: steelblue;
}
.bar2 {
fill: orange;
}
.x.axis path {
display: black;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 80, right: 80, bottom: 80, left: 80},
width = 400 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y0 = d3.scale.linear().domain([100,500]).range([height, 0]),
y1 = d3.scale.linear().domain([500,1000]).range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
// create left yAxis
var yAxisLeft = d3.svg.axis().scale(y0).ticks(5).orient("left");
// create right yAxis
var yAxisRight = d3.svg.axis().scale(y1).orient("right");
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Health Workers:</strong> <span style='color:red'>" + d.totalhealthworkers + "</span>";
})
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("class", "graph")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
svg.call(tip);
d3.json("workforce.json", function(error, data) {
x.domain(data.map(function(d) { return d.countryName; }));
y0.domain([0, d3.max(data, function (d) { return d.totalhealthworkers; })]);
y1.domain([0, d3.max(data, function (d) { return d.totalhealthfacilities; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis axisLeft")
.attr("transform", "translate(0,0)")
.call(yAxisLeft)
.append("text")
.attr("y", 35)
.attr("dy", "-2em")
.attr("transform", "rotate(-90)")
.style("text-anchor", "end")
.text("Total health workers per 100,000 people");
svg.append("g")
.attr("class", "y axis axisRight")
.attr("transform", "translate(" + (width) + ",0)")
.call(yAxisRight)
.append("text")
.attr("y", 15)
.attr("x", -20)
.attr("transform", "rotate(-90)")
.attr("dy", "-2em")
.attr("dx", "2em")
.style("text-anchor", "end")
.text("Total health facilities per 100,000 people");
bars = svg.selectAll(".bar").data(data).enter();
bars.append("rect")
.attr("class", "bar1")
.attr("x", function (d) { return x(d.countryName); })
.attr("width", x.rangeBand() / 3)
.attr("y", function (d) { return y0(d.totalhealthworkers); })
.attr("height", function (d, i, j) { return height - y0(d.totalhealthworkers); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
bars.append("rect")
.attr("class", "bar2")
.attr("x", function(d) { return x(d.countryName) + x.rangeBand()/2; })
.attr("width", x.rangeBand() / 3)
.attr("y", function(d) { return y1(d.totalhealthfacilities); })
.attr("height", function (d, i, j) { return height - y1(d.totalhealthfacilities); })
});
function type(d) {
d.totalhealthworkers = +d.totalhealthworkers;
d.totalhealthfacilities = +d.totalhealthfacilities;
return d;
}
</script>
</body></div>
</html>
这是我正在使用的JSON文件。
[{"countryName":"Afghanistan","totalhealthworkers":"113.9","totalhealthfacilities":"4.50239"}]
答案 0 :(得分:11)
您可能需要添加
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
答案 1 :(得分:4)
这是根据d3版本,如果你使用v3然后提示是
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
如果您使用v4,请将其用于提示
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>