我有一个条形图,我需要在特定的条形图上指出一个箭头并给它一个短标签。我能够得到箭头线,但箭头没有显示出来。 我做错了什么? 我的代码如下:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: orange;
}
.bar:hover {
fill: orangered ;
}
.x.axis path {
display: none;
}
.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;
}
line {
stroke: black;
}
path.marker {
fill: black;
}
path {
stroke: black;
fill: none;
}
</style>
<body>
<div class="container">
<div class="jumbotron">
<h2 style="text-align:center;">Head And Neck Therapy </h2>
</div>
<nav role="navigation" class="navbar navbar-default">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collection of nav links and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Data OverView</a></li>
<li><a href="#">Profile</a></li>
<li><a href="#">Messages</a></li>
</ul>
</div>
</nav>
<div id=chartId style="margin-top:50px; margin-left:50px;"></div>
</div>
</body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://cameronspear.com/downloads/bootstrap-hover-dropdown/bootstrap-hover-dropdown.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
.jumbotron {
padding: 0.5em 0.6em;
h1 {
font-size: 2em;
}
p {
font-size: 1.2em;
.btn {
padding: 0.5em;
}
}
}
</style>
<script>
var margin = {top: 10, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .2);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d) { return d + "%"; });
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>" + d.letter + " constitute : </strong> <span style='color:red'> " + d3.format(".2f")(d.frequency) + "% out of total population size</span>";
})
var svg = d3.select("#chartId").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
d3.tsv("data.tsv", type, function(error, data) {
var sum = d3.sum(data, function(d){return +d.frequency;});
//this will convert teh frequency into percent
data.forEach(function(d){ d.frequency = (d.frequency/sum)*100});
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, 100]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
svg.selectAll("text.label")
.data(data.filter(function(d) { return d.frequency == ((441/sum)*100); }))
.enter().append("text")
.attr("class", "label")
.attr("x", function (d) {
return x(d.letter) + x.rangeBand()/2;
}).attr("y", function (d) {
return y(d.frequency) - 70;
})
.style("text-anchor", "middle")
.text("Your Patient");
svg.selectAll("line.arrow")
.data(data.filter(function(d) { return d.frequency == ((441/sum)*100); }))
.enter().append("line")
.attr("class", "arrow")
.attr("x1", function (d) {
return x(d.letter) + x.rangeBand()/2;
})
.attr("x2", function (d) {
return x(d.letter) + x.rangeBand()/2;
})
.attr("y1", function (d) {
return y(d.frequency) - 67;
})
.attr("y2", function (d) {
return y(d.frequency) - 10;
})
.attr("marker-end", "url(#arrow)");
});
function type(d) {
d.frequency = +d.frequency;
d.frequency = d3.format(".2f")(d.frequency)
return d;
}
</script>
</html>
data.tsv文件如下:
letter frequency
Asian 4
African-American 96
White 441
Hispanic 81
Other 11
答案 0 :(得分:0)
问题在于:
.attr("marker-end", "url(#arrow)");
标识svg defs
的{{1}}中没有标记。
参见: http://bl.ocks.org/tomgp/d59de83f771ca2b6f1d4 https://developer.mozilla.org/en/docs/Web/SVG/Element/marker