Skip to content

Instantly share code, notes, and snippets.

@pbogden
Last active October 6, 2015 23:32
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 pbogden/4d2f9cec7ce6e8029cf0 to your computer and use it in GitHub Desktop.
Save pbogden/4d2f9cec7ce6e8029cf0 to your computer and use it in GitHub Desktop.
binesh5

Small changes to Binesh's block: http://bl.ocks.org/geoatlantic/85a2c8083e9808eebeb0

Original error in console:

Invalid value for attribute transform="translate(NaN,0)"

This indicates that you're incorrectly setting the value of the "g" attribute, which is used by d3.svg.axis. It turns out that you were using the wrong scale. I changed it from ordinal to linear, which is what Suzee used. You could use the ordinal scale instead, but then you'd have to be careful how you set the scale's domain.

I made a few other changes too, as indicated in the code.

My recommendation is that you fix up the labeling and styling, and make sure you understand how things work. You could also see if you can change back from linear to ordinal scale for the xAxis.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
font: 10px sans-serif;
}
.bar {
fill: orange;
}
.bar:hover {
fill: orangered ;
}
/*
.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;
}*/
.chart text {
font: 12px sans-serif;
}
</style>
<svg class = "chart"></svg>
<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>
<script>
var quakes; //global variable
var url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson";
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
//var formatPercent = d3.format(".0%");
var x = d3.scale.linear() // Note from PB: Changed scale from ordinal to linear
.range([0, width]) // Note from PB: Changed range to [0, width]
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.tickValues ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
//.tickFormat(formatPercent);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
})
var chart = d3.select(".chart") //.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.json(url, function(error, data) {
if (error) console.log(error);
console.log("Data from USGS:", data);
quakes = data
quakesMag = [];
quakesMag = data.features.map(function(d) { return d.properties.mag; });
console.log("Earthquake Magnitude:", quakesMag);
quakesBin = []; // empty array to bin the mag values
for (var i = 0; i < 10; i++) {
quakesBin[i] = quakesMag.filter(function(d) { return d >= i && d < i + 1;}).length;
}
x.domain([0, quakesBin.length]);
// y.domain([d3.min(quakesBin, function(d) { return d.frequency; })]);
y.domain(d3.extent(quakesBin));
var barWith = width / quakesMag.length;
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
chart.append("g")
.attr("class", "y axis")
.call(yAxis);
chart.append("text") // append for y axis label
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("y", -1 - margin.left/3)
.attr("dy", ".71em")
.attr("text-anchor", "middle")
.text("Earthquake Frequency");
chart.append("text") // append for x axis label
.attr("text-anchor", "end")
.attr("x", width/2)
.attr("y", height + 2*margin.bottom/3 + 9)
.text("Earthquake Magnitude");
chart.append("text") // append title
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", -margin.top/2)
.attr("dy", "+.75em")
.text("Earthquake Occurences - 4.5 days");
// chart.selectAll(".bar") // Note from PB: Commented this line
// .data(quakesBin) // Note from PB: Commented this line
// .enter().append("rect") // Note from PB: Commented this line
// .attr("class", "bar") // Note from PB: Commented this line
chart.selectAll("rect")
.data(quakesBin)
.enter().append("rect") // Note from PB: added this line
.attr("x", function(d, i) { return x(i); })
.attr("y", function(d) { return y(d); }) // Note from PB: Changed "d.properties" to "d"
.attr("height", function(d) { return height - y(d); }) // Note from PB: Changed "d.properties" to "d"
.attr("width", barWith)
//move mouse
/*
.on('mouseover', tip.show)
.on('mouseout', tip.hide);*/
});
/*function type(d) {
d.frequency = +d.frequency;
return d;
}*/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment