|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<style> |
| 4 | + |
| 5 | +.bar { |
| 6 | + fill: steelblue; |
| 7 | +} |
| 8 | + |
| 9 | +.bar:hover { |
| 10 | + fill: brown; |
| 11 | +} |
| 12 | + |
| 13 | +.axis--x path { |
| 14 | + display: none; |
| 15 | +} |
| 16 | + |
| 17 | +</style> |
| 18 | +<svg width="960" height="500"></svg> |
| 19 | +<script src="https://d3js.org/d3.v4.min.js"></script> |
| 20 | +<script> |
| 21 | + |
| 22 | +var svg = d3.select("svg"), |
| 23 | + margin = {top: 20, right: 20, bottom: 30, left: 40}, |
| 24 | + width = +svg.attr("width") - margin.left - margin.right, |
| 25 | + height = +svg.attr("height") - margin.top - margin.bottom; |
| 26 | + |
| 27 | +var x = d3.scaleBand().rangeRound([0, width]).padding(0.1), |
| 28 | + y = d3.scaleLinear().rangeRound([height, 0]); |
| 29 | + |
| 30 | +var g = svg.append("g") |
| 31 | + .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
| 32 | + |
| 33 | +d3.json("final_results.json", function(d) { |
| 34 | + console.log(d) |
| 35 | + // d.frequency = +d.frequency; |
| 36 | + return d; |
| 37 | +}, function(error, data) { |
| 38 | + if (error) throw error; |
| 39 | + |
| 40 | + x.domain(data.map(function(d) { return d.Season; })); |
| 41 | + y.domain([0, d3.max(data, function(d) { return d.TR6; })]); |
| 42 | + |
| 43 | + g.append("g") |
| 44 | + .attr("class", "axis axis--x") |
| 45 | + .attr("transform", "translate(0," + height + ")") |
| 46 | + .call(d3.axisBottom(x)); |
| 47 | + |
| 48 | + g.append("g") |
| 49 | + .attr("class", "axis axis--y") |
| 50 | + .call(d3.axisLeft(y).ticks(10, "%")) |
| 51 | + .append("text") |
| 52 | + .attr("transform", "rotate(-90)") |
| 53 | + .attr("y", 6) |
| 54 | + .attr("dy", "0.71em") |
| 55 | + .attr("text-anchor", "end") |
| 56 | + .text("Frequency"); |
| 57 | + |
| 58 | + g.selectAll(".bar") |
| 59 | + .data(data) |
| 60 | + .enter().append("rect") |
| 61 | + .attr("class", "bar") |
| 62 | + .attr("x", function(d) { return x(d.Season); }) |
| 63 | + .attr("y", function(d) { return y(d.TR6); }) |
| 64 | + .attr("width", x.bandwidth()) |
| 65 | + .attr("height", function(d) { return height - y(d.TR6); }); |
| 66 | +}); |
| 67 | + |
| 68 | +</script> |
0 commit comments