Skip to content

Instantly share code, notes, and snippets.

@alanmclean
Forked from mbostock/.block
Last active December 28, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanmclean/7474904 to your computer and use it in GitHub Desktop.
Save alanmclean/7474904 to your computer and use it in GitHub Desktop.

The fill transition does not work in Chrome when the target of the transition is within an <a> tag with the xlink:href attribute set.

name value
Locke 4
Reyes 8
Ford 15
Jarrah 16
Shephard 23
Kwon 42
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart rect {
fill: steelblue;
transition: fill 1.5s;
}
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
.chart rect.new-color{
fill: #900 !important;
}
</style>
<svg class="chart"></svg>
<button onclick="changeColor();">Change Color</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 420,
barHeight = 20;
var x = d3.scale.linear()
.range([0, width]);
var chart = d3.select(".chart")
.attr("width", width);
d3.tsv("data.tsv", type, function(error, data) {
x.domain([0, d3.max(data, function(d) { return d.value; })]);
chart.attr("height", barHeight * data.length);
var bar = chart.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });
bar.append("a")
.attr("xlink:href", "http://www.d3js.org")
.append("rect")
.attr("width", function(d) { return x(d.value); })
.attr("height", barHeight - 1);
bar.append("text")
.attr("x", function(d) { return x(d.value) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d.value; });
});
function type(d) {
d.value = +d.value; // coerce to number
return d;
}
newColor = false
changeColor = function(){
d3.selectAll('rect').classed('new-color', !newColor)
newColor = !newColor
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment