A demo to help with a Stackoverflow Question: https://stackoverflow.com/questions/47963204/adding-axes-eats-up-my-data
Built with blockbuilder.org
| license: mit |
A demo to help with a Stackoverflow Question: https://stackoverflow.com/questions/47963204/adding-axes-eats-up-my-data
Built with blockbuilder.org
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <style> | |
| body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="chartHolder"> | |
| <svg class="chart"></svg> | |
| </div> | |
| </body> | |
| <script src="main.js"> | |
| </script> | |
| </html> |
| // Investigating response to : https://stackoverflow.com/questions/47963204/adding-axes-eats-up-my-data | |
| var GIST = "https://gist.githubusercontent.com/charisseysabel/f8f48fbf11b8a1b0d62cbe2d6bdc2aa6/raw/2ead1537adb822fbd59a666afd5334d525480a13/nano-2017.tsv"; | |
| var width = 1000, | |
| height = 550, | |
| margin = {top: 20, right: 30, bottom: 30, left: 4}; | |
| var y = d3.scaleLinear() | |
| .range([height, 0]); | |
| var yScale = d3.scaleLinear() | |
| .range([height, 0]); | |
| var xScale = d3.scaleLinear() | |
| .range([0, width]); | |
| var xAxis = d3.axisLeft(yScale); | |
| var yAxis = d3.axisBottom(xScale); | |
| var chart = d3.select(".chart") | |
| .attr("width", width) | |
| .attr("height", height); | |
| d3.tsv(GIST, type, function(error, data) { | |
| y.domain([0, d3.max(data, function(d) { return d.value; })]); | |
| var barWidth = width / data.length; | |
| var bar = chart.selectAll(null) | |
| .data(data) | |
| .enter().append("g") | |
| .attr("transform", function(d, i) { | |
| return "translate(" + ((i * barWidth) + 10) + ",0)"; } | |
| ); | |
| bar.append("rect") | |
| .attr("y", function(d) { return y(d.value); }) | |
| .attr("height", function(d) { return height - y(d.value); }) | |
| .attr("width", barWidth - 1); | |
| bar.append("text") | |
| .attr("x", (barWidth / 2) - 2) | |
| .attr("y", function(d) { return y(d.value) + 3; }) | |
| .attr("dy", ".75em") | |
| .text(function(d) { return d.value; }); | |
| }); | |
| chart.append("g") | |
| .attr("transform", "translate(10, 0)") | |
| .call(xAxis); | |
| chart.append("g") | |
| .attr("transform", "translate(0, 540)") | |
| .call(yAxis); | |
| function type(d) { | |
| d.value = +d.value; | |
| return d; | |
| } |