Skip to content

Commit 5c1e99d

Browse files
committed
example fo rusing d3
1 parent 030e460 commit 5c1e99d

File tree

9 files changed

+180
-2
lines changed

9 files changed

+180
-2
lines changed

app/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ <h3 class="text-muted">whatami</h3>
7777
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
7878
<script src="bower_components/angular-touch/angular-touch.js"></script>
7979
<script src="bower_components/modernizr/modernizr.js"></script>
80+
<script src="bower_components/d3/d3.js"></script>
8081
<!-- endbower -->
8182
<!-- endbuild -->
8283

8384
<!-- build:js({.tmp,app}) scripts/scripts.js -->
8485
<script src="scripts/app.js"></script>
8586
<script src="scripts/controllers/about.js"></script>
8687
<script src="scripts/controllers/place.js"></script>
88+
<script src="scripts/services/d3service.js"></script>
89+
<script src="scripts/directives/simplelinechart.js"></script>
8790
<!-- endbuild -->
8891
</body>
8992
</html>

app/scripts/app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ angular
1515
'ngResource',
1616
'ngRoute',
1717
'ngSanitize',
18-
'ngTouch'
18+
'ngTouch',
19+
'd3'
1920
])
2021
.config(function ($routeProvider) {
2122
$routeProvider
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
/**
4+
* @ngdoc directive
5+
* @name whatamiApp.directive:simpleLineChart
6+
* @description
7+
* # simpleLineChart
8+
*/
9+
angular.module('whatamiApp')
10+
.directive('simpleLineChart', ['d3Service', function(d3Service) {
11+
return {
12+
restrict: 'EA',
13+
scope: {},
14+
link: function(scope, element, attrs) {
15+
d3Service.d3().then(function(d3) {
16+
17+
var margin = {top: 20, right: 20, bottom: 30, left: 50},
18+
width = 600 - margin.left - margin.right,
19+
height = 700 - margin.top - margin.bottom;
20+
21+
var parseDate = d3.time.format('%d-%b-%y').parse;
22+
23+
var x = d3.time.scale()
24+
.range([0, width]);
25+
26+
var y = d3.scale.linear()
27+
.range([height, 0]);
28+
29+
var xAxis = d3.svg.axis()
30+
.scale(x)
31+
.orient('bottom');
32+
33+
var yAxis = d3.svg.axis()
34+
.scale(y)
35+
.orient('left');
36+
37+
var line = d3.svg.line()
38+
.x(function(d) { return x(d.date); })
39+
.y(function(d) { return y(d.close); });
40+
41+
var svg = d3.select(element[0]).append('svg')
42+
.attr('width', width + margin.left + margin.right)
43+
.attr('height', height + margin.top + margin.bottom)
44+
.append('g')
45+
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
46+
47+
// Hard coded data
48+
scope.data = [
49+
{date: '4-Apr-12', close: 34},
50+
{date: '5-Apr-12', close: 45},
51+
{date: '6-Apr-12', close: 37},
52+
{date: '7-Apr-12', close: 56},
53+
{date: '8-Apr-12', close: 50},
54+
{date: '9-Apr-12', close: 77}
55+
];
56+
57+
scope.data.forEach(function(d) {
58+
d.date = parseDate(d.date);
59+
d.close = +d.close;
60+
});
61+
62+
x.domain(d3.extent(scope.data, function(d) { return d.date; }));
63+
y.domain(d3.extent(scope.data, function(d) { return d.close; }));
64+
65+
svg.append('g')
66+
.attr('class', 'x axis')
67+
.attr('transform', 'translate(0,' + height + ')')
68+
.call(xAxis);
69+
70+
svg.append('g')
71+
.attr('class', 'y axis')
72+
.call(yAxis)
73+
.append('text')
74+
.attr('transform', 'rotate(-90)')
75+
.attr('y', 6)
76+
.attr('dy', '.71em')
77+
.style('text-anchor', 'end')
78+
.text('Price ($)');
79+
80+
svg.append('path')
81+
.datum(scope.data)
82+
.attr('class', 'line')
83+
.attr('d', line);
84+
});
85+
}};
86+
}]);

app/scripts/services/d3service.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
angular.module('d3', [])
4+
.factory('d3Service', ['$document', '$q', '$rootScope', function($document, $q, $rootScope) {
5+
var d = $q.defer();
6+
function onScriptLoad() {
7+
// Load client in the browser
8+
$rootScope.$apply(function() { d.resolve(window.d3); });
9+
}
10+
// Create a script tag with d3 as the source
11+
// and call our onScriptLoad callback when it
12+
// has been loaded
13+
var scriptTag = $document[0].createElement('script');
14+
scriptTag.type = 'text/javascript';
15+
scriptTag.async = true;
16+
scriptTag.src = 'bower_components/d3/d3.js';
17+
scriptTag.onreadystatechange = function () {
18+
if (this.readyState === 'complete') { onScriptLoad(); }
19+
};
20+
scriptTag.onload = onScriptLoad;
21+
22+
var s = $document[0].getElementsByTagName('body')[0];
23+
s.appendChild(scriptTag);
24+
25+
return {
26+
d3: function() { return d.promise; }
27+
};
28+
}]);

app/styles/main.scss

+19
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,22 @@ body {
9090
border-bottom: 0;
9191
}
9292
}
93+
94+
/* D3 */
95+
96+
.axis path,
97+
.axis line {
98+
fill: none;
99+
stroke: #000;
100+
shape-rendering: crispEdges;
101+
}
102+
103+
.x.axis path {
104+
display: none;
105+
}
106+
107+
.line {
108+
fill: none;
109+
stroke: steelblue;
110+
stroke-width: 1.5px;
111+
}

app/views/place.html

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
<img ng-show="isGeolocating" src="images/red-dot.png"/>
88
<img ng-hide="isGeolocating" src="images/green-dot.png"/>
99
{{positions}}
10+
11+
<div simple-line-chart></div>

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"angular-resource": "^1.3.0",
1212
"angular-route": "^1.3.0",
1313
"angular-sanitize": "^1.3.0",
14-
"angular-touch": "^1.3.0"
14+
"angular-touch": "^1.3.0",
15+
"d3": "~3.5.2"
1516
},
1617
"devDependencies": {
1718
"angular-mocks": "~1.3.0",
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
describe('Directive: simpleLineChart', function () {
4+
5+
// load the directive's module
6+
beforeEach(module('whatamiApp'));
7+
8+
var element,
9+
scope;
10+
11+
beforeEach(inject(function ($rootScope) {
12+
scope = $rootScope.$new();
13+
}));
14+
15+
it('should make hidden element visible', inject(function ($compile) {
16+
element = angular.element('<simple-line-chart></simple-line-chart>');
17+
element = $compile(element)(scope);
18+
expect(element.text()).toBe('this is the simpleLineChart directive');
19+
}));
20+
});

test/spec/services/d3service.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
describe('Service: d3Service', function () {
4+
5+
// load the service's module
6+
beforeEach(module('whatamiApp'));
7+
8+
// instantiate service
9+
var d3Service;
10+
beforeEach(inject(function (_d3Service_) {
11+
d3Service = _d3Service_;
12+
}));
13+
14+
it('should do something', function () {
15+
expect(!!d3Service).toBe(true);
16+
});
17+
18+
});

0 commit comments

Comments
 (0)