yeoman angularjs and d3 - a solid stack for web apps

50
Yeoman, AngularJS and D3.js Solid stack for web apps Created by / / Oscar Villarreal @climboid Suman Paul @sumankpaul

Upload: climboid

Post on 10-May-2015

8.162 views

Category:

Technology


1 download

DESCRIPTION

This was a course given in Bangalore India for JSChannel conf 2013. It encompases the use of angular js and d3 in a harmonious way and gives an overview over each of the frameworks / libraries.

TRANSCRIPT

Page 1: Yeoman AngularJS and D3 - A solid stack for web apps

Yeoman,AngularJS and

D3.jsSolid stack for web apps

Created by

/

/

Oscar Villarreal @climboid

Suman Paul @sumankpaul

Page 2: Yeoman AngularJS and D3 - A solid stack for web apps

Structure of the courseIntrosDive into YeomanDive into AngularDive into D3.jsProjects throughout all pointsMaster project using all 3 tools

Page 3: Yeoman AngularJS and D3 - A solid stack for web apps

Oscar VillarrealUI LeadJS GeneralistAngularJSD3JSCSSRock climboscarvillareal.com@climboid

Page 4: Yeoman AngularJS and D3 - A solid stack for web apps

Suman PaulUI LeadJS GeneralistAngularJSGruntYeomanCSS

Page 5: Yeoman AngularJS and D3 - A solid stack for web apps

Mexico and India

We are very similar in many ways, especially when it comes toworking hard and being passionate about our teams

Page 6: Yeoman AngularJS and D3 - A solid stack for web apps

We need to bemore productive

Page 7: Yeoman AngularJS and D3 - A solid stack for web apps

We need toachieve utopiafaster & better

Page 8: Yeoman AngularJS and D3 - A solid stack for web apps

How do we achieve a higherlevel of productivity?

The secret sauce is in using the right tools

Page 9: Yeoman AngularJS and D3 - A solid stack for web apps

Using the right tools also means that we have the power to dothings better and more organized with richer interactions

Page 10: Yeoman AngularJS and D3 - A solid stack for web apps

Start with your workflow

Page 11: Yeoman AngularJS and D3 - A solid stack for web apps
Page 12: Yeoman AngularJS and D3 - A solid stack for web apps

"A collection of tools and best practices working in harmonyto make developing for the web even better"Allows you to quickly assemble files and folders along withboilerplate code.Uses grunt for all of its tasksUses Bower for all of the project dependenciesVia the use of generators one can scaffold projects veryquickly

Page 13: Yeoman AngularJS and D3 - A solid stack for web apps

GruntTakes care of minification, compilation (sass, coffeescript), unit

testing etcAutomation

Page 14: Yeoman AngularJS and D3 - A solid stack for web apps

BowerA package manager for the web, mainly front end packaging

management.

Page 15: Yeoman AngularJS and D3 - A solid stack for web apps

GeneratorA way to instantiate convention over configuration

Boilerplate code

Page 16: Yeoman AngularJS and D3 - A solid stack for web apps

Who contributes to Yeoman?Addy Osmani, Hemanth HM and some of the best in the world

Page 17: Yeoman AngularJS and D3 - A solid stack for web apps

How does Yeoman workLive example

Page 18: Yeoman AngularJS and D3 - A solid stack for web apps

Lets install YeomanYou will need Make sure you have git installed as wellIf you use SASS you will need ruby and compassUse for windows installs (courtesy of Suman Paul)

NodeJS

this linknpm install -g yonpm install -g generator-webappnpm install -g generator-angular

Page 19: Yeoman AngularJS and D3 - A solid stack for web apps

What is an MVC frameworkA pattern for your code that separates interaction and view

Where does it come from? (smalltalk)How has it evolved in the front end

DOJO ToolkitJavaScript MVCBackbone.js - Jeremy AshkenasSpine.jsKnockout.js MVVMEmber.js - Yehuda Katz & Tom DaleAngularJS - Misko Hevery

Page 20: Yeoman AngularJS and D3 - A solid stack for web apps

Why choose Angular?

Page 21: Yeoman AngularJS and D3 - A solid stack for web apps

No silver bullet

Page 22: Yeoman AngularJS and D3 - A solid stack for web apps

You can still create a messwith angular...

The key is to learn and understand what Angular is doing toavoid making a mess

Page 23: Yeoman AngularJS and D3 - A solid stack for web apps

Who contributes to Angular?Google

Misko Hevery

Igor Minar

Vojta Jina

Matias Niemela

Brian Ford

Page 24: Yeoman AngularJS and D3 - A solid stack for web apps

The key in angular is alwaysthink about the data, not the

DOM, the DOM will take care ofits self so long as it is data

binded with the corresponding$scope of a given controller

Page 25: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularthe ng...ng-app="App"ng-view

<!doctype html><html> <head> <meta charset="utf-8"/> </head> <body ng-app="App"> <div class="container" ng-view></div> <script src="components/angular/angular.js"></script> <script src="scripts/app.js"></script> <script src="scripts/controllers/main.js"></script> </body></html>

Page 26: Yeoman AngularJS and D3 - A solid stack for web apps

Core concpets of angularYour main app module

'use strict';

angular.module('App', []) .config(function ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }) .otherwise({ redirectTo: '/' }); });

Page 27: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularRouter

A router allows you to glue a controller to a view, creates ascope underneath that controller and maintains state within the

app.

$routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }) .when('/directory', { templateUrl: 'views/directory.html', controller: 'DirectoryCtrl' }) .otherwise({ redirectTo: '/'});

Page 28: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularController

Instantiation of a new controller object along with its childscope

'use strict';

angular.module('App')

.controller('MainCtrl', function ($scope) {

$scope.awesomeThings = [

'HTML5 Boilerplate',

'AngularJS',

'Karma'

];

});

Page 29: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularViewng-view

Templates that interpolate scope objects that are part of agiven controller or are inherited prototypically.

<div class="hero-unit"> <h1>'Allo, 'Allo!</h1> <p>You now have</p> <ul> <li ng-repeat="thing in awesomeThings">{{thing}}</li> </ul> <p>installed.</p> <h3>Enjoy coding! - Yeoman</h3></div>

Page 30: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angular$scope

Keeps your logic contained to that controller unless you areusing $rootScope

$scope will allow you to interpolate its properties in a givenview

$scope.hello = "world"

What gets rendered in the ... ?

<div ng-bind="hello">...</div><div>{{hello}}... </div>

Page 31: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angular$scope

angular.module('App') .controller('MainCtrl', function ($scope) { $scope.hello = [ { title:'me',color:'blue',value:2 }, { title:'you',color:'red',value:0 } ]; $scope.btnClicked = function(){ ... }; $scope.falsy = false; });

<!-- main.html--><ul ng-if="!falsy"> <li ng-repeat="item in hello" ng-click="btnClicked()"> <div ng-bind="item.title"></div> <div>{{item.color}}</div> <input type="number" ng-model="item.value"/> </li></ul>

Page 32: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularServices

Something that spans multiple controllers and doesn't have aview tied to it.

'use strict';

angular.module('App')

.service('Welcoming', function Calculations() {

return{

sayHello: function(){ return 'hello' },

sayGoodbye: function(){ return 'goodbye' }

}

});

Page 33: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularConstants

Maintained across the entire application. Can be injected as aservice. Use this for constants within your application.

'use strict';

angular.module('App') .constant('Pi', 3.141516);

Page 34: Yeoman AngularJS and D3 - A solid stack for web apps

Core concepts of angularCustom Directives

UI components, charts, visualization... anything that hasspecific html conversion and needs to be part of the given

scope.

'use strict';

angular.module('App') .directive('chart', function () { return { restrict: 'E', controller: function($scope, $element){ ... }, templateUrl: 'my-dialog.html', link: function (scope, element) { scope.name = 'Jeff'; } }; });

Page 35: Yeoman AngularJS and D3 - A solid stack for web apps

Goodies of Angular andYeoman

Karma test runnerngMin for minification / concatenationLive reload...

Page 36: Yeoman AngularJS and D3 - A solid stack for web apps

Lets make an Angular appIf you have Yeoman installed follow the presenter

If you do not have Yeoman installed please clone this repo

Page 37: Yeoman AngularJS and D3 - A solid stack for web apps

What is the canvas?<canvas> "magic" </canvas>

Used to draw graphics, on the fly, via scripting (usually

JavaScript)

Upon clicking you need to find X and Y and compare to

elements inside of canvas

Can be super charged for webGL (context 3d)

If you need a lot of things in your visualization use Canvas

for it will not kill the DOM

Raster

Page 38: Yeoman AngularJS and D3 - A solid stack for web apps

Third party libraries for CanvasKinetic JSFabric JSthree JS

Page 39: Yeoman AngularJS and D3 - A solid stack for web apps

What is SVG?Scalable Vector Graphics, vector image format for 2

dimensional graphics

It is part of the DOM, can be styled using css

Because its part of the DOM one can attach events to it via

JavaScript

Vectors can be scaled and because of that they will look

beautifully on retina displays

Ability to use images as SVG format and even fonts

Page 40: Yeoman AngularJS and D3 - A solid stack for web apps

Third party libraries for SVGD3.js

RaphaelBonsai JS

Page 41: Yeoman AngularJS and D3 - A solid stack for web apps

Why choose D3.jsFull power and control over every single vector drawn

Extremely easy to data bind

Extremely easy to bind events

Its vectors so its works beautifully on all displays

Huge support from community

Page 42: Yeoman AngularJS and D3 - A solid stack for web apps

wuhu!

Page 43: Yeoman AngularJS and D3 - A solid stack for web apps

Who's behind D3.js

many others

Edward Tufte

Mike BostockJason Davies

"I suggest keeping an eye on the work ofBret Victor and Mike Bostock. Both are

cutting edge. Bret Victor visualisesprogramming, sees and thinks beautifully,

just starting to blossom (seeworrydream.com). Mike Bostock (see

bost.ocks.org) developed the open-sourceD3, Data-Driven Documents (d3js.org)."

Page 44: Yeoman AngularJS and D3 - A solid stack for web apps

Structure of a D3 visualizationInclude the librarysvg container <svg> "magic" <svg>Create your scales using the right domain and rangeSome boiler plate for your svg containerCreate SVG itemsData bind the SVG item attributesEnter, update, exit

Page 45: Yeoman AngularJS and D3 - A solid stack for web apps

Mike Bostocksrecommendations

For reusable charts

Page 46: Yeoman AngularJS and D3 - A solid stack for web apps

Things to know about whenusing D3

SVG (elements & attributes)Scales (linear, log, exp, ordinal, ... )Utility functions (max, min, extent, ...)Boilerplate process

Page 47: Yeoman AngularJS and D3 - A solid stack for web apps

Lets create our own

Page 48: Yeoman AngularJS and D3 - A solid stack for web apps

11/16/13 JS-CHANNEL

localhost:8000/#/57 1/1

Some maps maybe?

Page 49: Yeoman AngularJS and D3 - A solid stack for web apps

Code for maps

var width = 960, height = 500;

var projection = d3.geo.albersUsa() .scale(1000) .translate([width / 2, height / 2]);

var path = d3.geo.path() .projection(projection);

var mapContainer = d3.select("#usaMap").append("svg") .attr("width", width) .attr("height", height);

d3.json("/data/output.json", function(error, us) { mapContainer.insert("path", ".graticule") .datum(topojson.feature(us, us.objects.roads)) .attr("class", "land")

Page 50: Yeoman AngularJS and D3 - A solid stack for web apps

Lets create our final repo!Create an angular application using Yeoman

That app should have two routes, main and aboutUs, createthem using the generatorInclude d3 using bower

Insert the d3 code into a directiveInsert the directive into the aboutUs route