D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
maggie-lee
Full window
Github gist
U.S. Container Port Depth
<!DOCTYPE html> <html> <head> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: black; opacity: 0.2; shape-rendering: crispEdges; } .x.axis path, x.axis line { fill: none; stroke: black; opacity: 0.2; shape-rendering: crispEdges; } .bar { fill: steelblue; } </style> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> </head> <body> <script> //capacity data from U.S. DOT Maritime Administration, Vessels Calls at U.S. Ports by Vessel Type: https://www.marad.dot.gov/library_landing_page/data_and_statistics/Data_and_Statistics.htm // Channel depth & tidal data from National Geospatial Intelligence Agency's World Port Index: https://msi.nga.mil/NGAPortal/MSI.portal?_nfpb=true&_pageLabel=msi_portal_page_62&pubCode=0015 var margin = {top: 30, right: 50, bottom: 30, left: 30}, width = 900 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var width_factor = 0.000005; var temp = margin.left; var temp2 = margin.left; var bar_padding = 10; var text_padding = 40; var depthConverter = function(some_letter) {var x = 0; switch(some_letter) { case "A": x = 76; break; case "B": x = 75; break; case "C": x = 70; break; case "D": x = 65; break; case "E": x = 60; break; case "F": x = 55; break; case "G": x = 50; break; case "H": x = 45; break; case "J": x = 40; break; case "K": x = 35; break; case "M": x = 25; break; default: x = 0 } {return x;}}; var svgContainer = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom); var y = d3.scale.linear() .domain([0, 90]) .range([margin.top, height]); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(9); var x = d3.scale.linear() .domain([157, 53]) .range([650, margin.left]); var xAxis = d3.svg.axis() .scale(x) .ticks(0) .orient("top"); console.log("ok harbors"); var csv_data = []; console.log(typeof(csv_data)); d3.csv("ports.csv", function(data) { data.forEach(function(d) { d.port = d.port d.longitude = parseInt(d.longitude) console.log(d.longitude) console.log(d) console.log(d.port) console.log(parseInt(d.capacity)); }); var text = svgContainer.selectAll("text") .data(data) .enter().append("text") .attr("x", function(d) { temp2 = bar_padding + temp2 + (parseInt((d.capacity*width_factor))) return ( temp2 - (parseInt((d.capacity*width_factor)))); }) .attr("y", function(d) {return (text_padding)+y(depthConverter(d.depth)); }) .text(function(d) {return d.port; }) .attr("font-family", "sans-serif") .attr("font-size", "10px") .attr("fill", "black"); var rectangles = svgContainer.selectAll("rect") .data(data) .enter().append("rect") .attr("x", function(d) { temp = bar_padding + temp + (parseInt((d.capacity*width_factor))) return ( temp - (parseInt((d.capacity*width_factor)))); }) .attr("y", margin.top) .attr("height", function(d) {return (y(depthConverter(d.depth))); }) .attr("width", function(d) {return (parseInt(d.capacity))*width_factor;}) .attr("fill", "blue") .attr("opacity", 0.2) .attr("stroke", "black"); //.attr("stroke-width", "2"); var yAxisGroup = svgContainer.append("g") .attr("class", "y axis") .attr("transform", "translate(" + margin.left + ",0)") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 8) .attr("x", -355) .attr("dy", ".17em") .style("text-anchor", "end") .text("Depth in feet"); var xAxisGroup = svgContainer.append("g") .attr("class", "y axis") .attr("transform", "translate(0," + margin.top + ")") .call(xAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js