I am a d3js noobie so please bear with me. I'm following Mike Bostock's step-by-step tutorial on how to create a bar chart with external data files. I am using the file format tsv
, which I created by entering data into Excel, saving it as a txt
file then renaming the extension to tsv
. I then set up a local web server with python -m SimpleHTTPServer
per this SO answer
While all the files appear in the Directory Listing, the bar chart is still absent. No errors appear in my console. I'm wondering if it's a problem with how the values are separated in my tsv file though it appears the same as the example. I'd really appreciate any help as I'm eager to get started with d3js.
Edit: I'm not sure if this is relevant, but the files on local server appear at the address: http://0.0.0.0:8000/
Here's the js (copied directly from the tutorial):
.chart rect {
fill: steelblue;
}
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
</style>
<svg class="chart"></svg>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8">
<script>
var width = 420,
barHeight = 20;
var x = d3.scale.linear()
.range([0, width]);
var chart = d3.select(".chart")
.attr("width", width);
d3.tsv("data2.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("rect")
.attr("width", function(d) { return x(d.value); })
.attr("height", barHeight - 1);
d
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;
}
</script>
and the tsv file:
name value
Locke 4
Reyes 8
Ford 15
Jarrah 16
Shephard 23
Kwon 42