Skip to content

Commit 4c8bc92

Browse files
committed
Added bar chart examples
1 parent d5f1a6e commit 4c8bc92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1134
-62
lines changed

generalizingD3/README.html

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ <h2 id="examples">Examples</h2>
5555
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot01">Example 1</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot01">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot01/index.html">index.html</a>) - Original D3 Bar Chart Example</li>
5656
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot02">Example 2</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot02">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot02/index.html">index.html</a>) - Split into HTML, CSS and JS files</li>
5757
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot03">Example 3</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot03">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot03/index.html">index.html</a>) - Implement Single Var Pattern</li>
58+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot04">Example 4</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot04">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot04/index.html">index.html</a>) - Make HTML structure explicit</li>
59+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot05">Example 5</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot05">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot05/index.html">index.html</a>) - Split bar chart and main program</li>
60+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot06">Example 6</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot06">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot06/index.html">index.html</a>) - Split up SVG and Group creation and configuration</li>
61+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot07">Example 7</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot07">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot07/index.html">index.html</a>) - Split up X axis group creation and configuration</li>
62+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot08">Example 8</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot08">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot08/index.html">index.html</a>) - Split up Y axis group creation and configuration</li>
63+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot09">Example 9</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot09">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot09/index.html">index.html</a>) - Split up enter and update for bars</li>
64+
<li><a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot10">Example 10</a> - (<a href="http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot10">run it!</a> | <a href="https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot10/index.html">index.html</a>) - Isolate hard-coded visualization size</li>
5865
</ul>
5966
<p>Final Result:</p>
6067
<ul>

generalizingD3/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ Listing of all examples:
5252
* [Example 1](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot01) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot01) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot01/index.html)) - Original D3 Bar Chart Example
5353
* [Example 2](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot02) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot02) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot02/index.html)) - Split into HTML, CSS and JS files
5454
* [Example 3](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot03) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot03) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot03/index.html)) - Implement Single Var Pattern
55+
* [Example 4](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot04) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot04) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot04/index.html)) - Make HTML structure explicit
56+
* [Example 5](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot05) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot05) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot05/index.html)) - Split bar chart and main program
57+
* [Example 6](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot06) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot06) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot06/index.html)) - Split up SVG and Group creation and configuration
58+
* [Example 7](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot07) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot07) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot07/index.html)) - Split up X axis group creation and configuration
59+
* [Example 8](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot08) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot08) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot08/index.html)) - Split up Y axis group creation and configuration
60+
* [Example 9](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot09) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot09) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot09/index.html)) - Split up enter and update for bars
61+
* [Example 10](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot10) - ([run it!](http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot10) | [index.html](https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot10/index.html)) - Isolate hard-coded visualization size
5562

5663
Final Result:
5764

generalizingD3/examples.json

+97
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,102 @@
3535
],
3636
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot03",
3737
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot03"
38+
},
39+
{
40+
"name": "snapshot04",
41+
"number": 4,
42+
"message": "Make HTML structure explicit",
43+
"files": [
44+
"index.html",
45+
"barChart.js",
46+
"data.tsv",
47+
"styles.css"
48+
],
49+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot04",
50+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot04"
51+
},
52+
{
53+
"name": "snapshot05",
54+
"number": 5,
55+
"message": "Split bar chart and main program",
56+
"files": [
57+
"index.html",
58+
"barChart.js",
59+
"main.js",
60+
"data.tsv",
61+
"styles.css"
62+
],
63+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot05",
64+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot05"
65+
},
66+
{
67+
"name": "snapshot06",
68+
"number": 6,
69+
"message": "Split up SVG and Group creation and configuration",
70+
"files": [
71+
"index.html",
72+
"barChart.js",
73+
"main.js",
74+
"data.tsv",
75+
"styles.css"
76+
],
77+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot06",
78+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot06"
79+
},
80+
{
81+
"name": "snapshot07",
82+
"number": 7,
83+
"message": "Split up X axis group creation and configuration",
84+
"files": [
85+
"index.html",
86+
"barChart.js",
87+
"main.js",
88+
"data.tsv",
89+
"styles.css"
90+
],
91+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot07",
92+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot07"
93+
},
94+
{
95+
"name": "snapshot08",
96+
"number": 8,
97+
"message": "Split up Y axis group creation and configuration",
98+
"files": [
99+
"index.html",
100+
"barChart.js",
101+
"main.js",
102+
"data.tsv",
103+
"styles.css"
104+
],
105+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot08",
106+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot08"
107+
},
108+
{
109+
"name": "snapshot09",
110+
"number": 9,
111+
"message": "Split up enter and update for bars",
112+
"files": [
113+
"index.html",
114+
"barChart.js",
115+
"main.js",
116+
"data.tsv",
117+
"styles.css"
118+
],
119+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot09",
120+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot09"
121+
},
122+
{
123+
"name": "snapshot10",
124+
"number": 10,
125+
"message": "Isolate hard-coded visualization size",
126+
"files": [
127+
"index.html",
128+
"barChart.js",
129+
"main.js",
130+
"data.tsv",
131+
"styles.css"
132+
],
133+
"url": "https://github.com/curran/screencasts/tree/gh-pages/introToAngular/examples/snapshots/snapshot10",
134+
"runUrl": "http://curran.github.io/screencasts/introToAngular/examples/snapshots/snapshot10"
38135
}
39136
]

generalizingD3/examples/barChart.js

+77-57
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,81 @@
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
3456
.attr("transform", "translate(0," + height + ")")
3557
.call(xAxis);
3658

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+
}
6181
}

generalizingD3/examples/index.html

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<!DOCTYPE html>
2-
<meta charset="utf-8">
3-
<link rel="stylesheet" href="styles.css">
4-
<body>
5-
<script src="http://d3js.org/d3.v3.min.js"></script>
6-
<script src="barChart.js"> </script>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="stylesheet" href="styles.css">
6+
<script src="http://d3js.org/d3.v3.min.js"></script>
7+
<script src="barChart.js"> </script>
8+
</head>
9+
<body>
10+
<script src="main.js"> </script>
11+
</body>
12+
</html>

generalizingD3/examples/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BarChart();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make HTML structure explicit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var margin = {top: 20, right: 20, bottom: 30, left: 40},
2+
width = 960 - margin.left - margin.right,
3+
height = 500 - margin.top - margin.bottom,
4+
5+
x = d3.scale.ordinal()
6+
.rangeRoundBands([0, width], .1),
7+
8+
y = d3.scale.linear()
9+
.range([height, 0]),
10+
11+
xAxis = d3.svg.axis()
12+
.scale(x)
13+
.orient("bottom"),
14+
15+
yAxis = d3.svg.axis()
16+
.scale(y)
17+
.orient("left")
18+
.ticks(10, "%"),
19+
20+
svg = d3.select("body").append("svg")
21+
.attr("width", width + margin.left + margin.right)
22+
.attr("height", height + margin.top + margin.bottom)
23+
.append("g")
24+
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
25+
26+
d3.tsv("data.tsv", type, function(error, data) {
27+
x.domain(data.map(function(d) { return d.letter; }));
28+
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
29+
30+
svg.append("g")
31+
.attr("class", "x axis")
32+
.attr("transform", "translate(0," + height + ")")
33+
.call(xAxis);
34+
35+
svg.append("g")
36+
.attr("class", "y axis")
37+
.call(yAxis)
38+
.append("text")
39+
.attr("transform", "rotate(-90)")
40+
.attr("y", 6)
41+
.attr("dy", ".71em")
42+
.style("text-anchor", "end")
43+
.text("Frequency");
44+
45+
svg.selectAll(".bar")
46+
.data(data)
47+
.enter().append("rect")
48+
.attr("class", "bar")
49+
.attr("x", function(d) { return x(d.letter); })
50+
.attr("width", x.rangeBand())
51+
.attr("y", function(d) { return y(d.frequency); })
52+
.attr("height", function(d) { return height - y(d.frequency); });
53+
54+
});
55+
56+
function type(d) {
57+
d.frequency = +d.frequency;
58+
return d;
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
letter frequency
2+
A .08167
3+
B .01492
4+
C .02782
5+
D .04253
6+
E .12702
7+
F .02288
8+
G .02015
9+
H .06094
10+
I .06966
11+
J .00153
12+
K .00772
13+
L .04025
14+
M .02406
15+
N .06749
16+
O .07507
17+
P .01929
18+
Q .00095
19+
R .05987
20+
S .06327
21+
T .09056
22+
U .02758
23+
V .00978
24+
W .02360
25+
X .00150
26+
Y .01974
27+
Z .00074
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="stylesheet" href="styles.css">
6+
<script src="http://d3js.org/d3.v3.min.js"></script>
7+
</head>
8+
<body>
9+
<script src="barChart.js"> </script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make HTML structure explicit

0 commit comments

Comments
 (0)