|
1 |
| -// Bar chart example from |
2 |
| -// http://bl.ocks.org/mbostock/3885304 |
3 |
| -var margin = {top: 20, right: 20, bottom: 30, left: 40}, |
4 |
| - width = 960 - margin.left - margin.right, |
5 |
| - height = 500 - margin.top - margin.bottom, |
6 |
| - |
7 |
| - x = d3.scale.ordinal() |
8 |
| - .rangeRoundBands([0, width], .1), |
9 |
| - |
10 |
| - y = d3.scale.linear() |
11 |
| - .range([height, 0]), |
12 |
| - |
13 |
| - xAxis = d3.svg.axis() |
14 |
| - .scale(x) |
15 |
| - .orient("bottom"), |
16 |
| - |
17 |
| - yAxis = d3.svg.axis() |
18 |
| - .scale(y) |
19 |
| - .orient("left") |
20 |
| - .ticks(10, "%"), |
21 |
| - |
22 |
| - svg = d3.select("body").append("svg") |
23 |
| - .attr("width", width + margin.left + margin.right) |
24 |
| - .attr("height", height + margin.top + margin.bottom) |
25 |
| - .append("g") |
26 |
| - .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
27 |
| - |
28 |
| -d3.tsv("data.tsv", type, function(error, data) { |
29 |
| - x.domain(data.map(function(d) { return d.letter; })); |
30 |
| - y.domain([0, d3.max(data, function(d) { return d.frequency; })]); |
31 |
| - |
32 |
| - svg.append("g") |
33 |
| - .attr("class", "x axis") |
| 1 | +function BarChart(){ |
| 2 | + var margin = {top: 20, right: 20, bottom: 30, left: 40}, |
| 3 | + outerWidth = 960, |
| 4 | + outerHeight = 500, |
| 5 | + width = outerWidth - margin.left - margin.right, |
| 6 | + height = outerHeight - margin.top - margin.bottom, |
| 7 | + |
| 8 | + x = d3.scale.ordinal() |
| 9 | + .rangeRoundBands([0, width], .1), |
| 10 | + |
| 11 | + y = d3.scale.linear() |
| 12 | + .range([height, 0]), |
| 13 | + |
| 14 | + xAxis = d3.svg.axis() |
| 15 | + .scale(x) |
| 16 | + .orient("bottom"), |
| 17 | + |
| 18 | + yAxis = d3.svg.axis() |
| 19 | + .scale(y) |
| 20 | + .orient("left") |
| 21 | + .ticks(10, "%"), |
| 22 | + |
| 23 | + svg = d3.select("body").append("svg"), |
| 24 | + g = svg.append("g"), |
| 25 | + |
| 26 | + xAxisG = g.append("g") |
| 27 | + .attr("class", "x axis"), |
| 28 | + |
| 29 | + yAxisG = g.append("g") |
| 30 | + .attr("class", "y axis"), |
| 31 | + |
| 32 | + yAxisLabel = yAxisG.append("text"); |
| 33 | + |
| 34 | + // Set width and height on the SVG element |
| 35 | + svg.attr("width", outerWidth) |
| 36 | + .attr("height", outerHeight); |
| 37 | + |
| 38 | + // Transform the SVG group that contains the visualization |
| 39 | + // based on the margin |
| 40 | + g.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
| 41 | + |
| 42 | + yAxisLabel |
| 43 | + .attr("transform", "rotate(-90)") |
| 44 | + .attr("y", 6) |
| 45 | + .attr("dy", ".71em") |
| 46 | + .style("text-anchor", "end") |
| 47 | + .text("Frequency"); |
| 48 | + |
| 49 | + d3.tsv("data.tsv", type, function(error, data) { |
| 50 | + var bars; |
| 51 | + |
| 52 | + x.domain(data.map(function(d) { return d.letter; })); |
| 53 | + y.domain([0, d3.max(data, function(d) { return d.frequency; })]); |
| 54 | + |
| 55 | + xAxisG |
34 | 56 | .attr("transform", "translate(0," + height + ")")
|
35 | 57 | .call(xAxis);
|
36 | 58 |
|
37 |
| - svg.append("g") |
38 |
| - .attr("class", "y axis") |
39 |
| - .call(yAxis) |
40 |
| - .append("text") |
41 |
| - .attr("transform", "rotate(-90)") |
42 |
| - .attr("y", 6) |
43 |
| - .attr("dy", ".71em") |
44 |
| - .style("text-anchor", "end") |
45 |
| - .text("Frequency"); |
46 |
| - |
47 |
| - svg.selectAll(".bar") |
48 |
| - .data(data) |
49 |
| - .enter().append("rect") |
50 |
| - .attr("class", "bar") |
51 |
| - .attr("x", function(d) { return x(d.letter); }) |
52 |
| - .attr("width", x.rangeBand()) |
53 |
| - .attr("y", function(d) { return y(d.frequency); }) |
54 |
| - .attr("height", function(d) { return height - y(d.frequency); }); |
55 |
| - |
56 |
| -}); |
57 |
| - |
58 |
| -function type(d) { |
59 |
| - d.frequency = +d.frequency; |
60 |
| - return d; |
| 59 | + yAxisG.call(yAxis); |
| 60 | + |
| 61 | + // Create the D3 selection with data binding |
| 62 | + bars = g.selectAll(".bar").data(data); |
| 63 | + |
| 64 | + // Enter: stuff that happens only once when DOM elements are created. |
| 65 | + // Should be independent of data and visualization size. |
| 66 | + bars.enter().append("rect").attr("class", "bar"); |
| 67 | + |
| 68 | + // Update: stuff that may happen many times, |
| 69 | + // and may change based on the data or visualization size. |
| 70 | + bars.attr("x", function(d) { return x(d.letter); }) |
| 71 | + .attr("width", x.rangeBand()) |
| 72 | + .attr("y", function(d) { return y(d.frequency); }) |
| 73 | + .attr("height", function(d) { return height - y(d.frequency); }); |
| 74 | + |
| 75 | + }); |
| 76 | + |
| 77 | + function type(d) { |
| 78 | + d.frequency = +d.frequency; |
| 79 | + return d; |
| 80 | + } |
61 | 81 | }
|
0 commit comments