Created
July 17, 2017 06:22
-
-
Save bgloh/bf805c4d2aaa34eafd4122a6f069f092 to your computer and use it in GitHub Desktop.
AngularJS_D3_FireBase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> | |
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script> | |
<<link rel="stylesheet" type="text/css" href="./todo.css"> | |
</head> | |
<style> | |
body{ | |
margin: 0; | |
} | |
donut-chart{ | |
width: 400px; | |
height: 400px; | |
display: inline-block; | |
} | |
</style> | |
<h1>Angular Js + D3.js</h1> | |
<body ng-app="myApp" ng-controller="d3ChartController as d3con"> | |
<d3-chart data= "data1" color = "green" radius = "50"></d3-chart> | |
<p>data1: {{d3con.data1}} </p> | |
<d3-chart data="[{x: 10, y: 10}, {x: 150, y: 50}, {x: 400, y: 150}] | |
" color = "red" radius = "20"></d3-chart> | |
<p> serviceTest : {{sayHello}} </p> | |
<div ng-controller="JobController as j"> | |
<p> variable1 : {{j.variable1}} <br> | |
{{j.sayHello();}} | |
</p> | |
</div> | |
</body> | |
</html> | |
<script> | |
// Initialize Firebase | |
var config = { | |
apiKey: "AIzaSyCw60BOszhENj-UiNMvDyKsZco29U1sMxk", | |
authDomain: "myfirstfirebaseprojec1.firebaseapp.com", | |
databaseURL: "https://myfirstfirebaseprojec1.firebaseio.com", | |
projectId: "myfirstfirebaseprojec1", | |
storageBucket: "myfirstfirebaseprojec1.appspot.com", | |
messagingSenderId: "992600788216" | |
}; | |
firebase.initializeApp(config); | |
var database = firebase.database(); | |
var ref = database.ref('scores'); | |
var data = { | |
name : "BGL2", | |
city : "Seoul", | |
score : 450 | |
} | |
//ref.push(data); | |
var scoreArray = []; | |
console.log(database); | |
ref.on('value', gotData,errData); | |
function gotData (data) { | |
// body... | |
console.log(data.val()); | |
var scores = data.val(); | |
var keys = Object.keys(scores); | |
console.log(keys); | |
for (var i=0;i<keys.length; i++){ | |
var k = keys[i]; | |
var score = scores[k].score; | |
scoreArray[i] = score; | |
} | |
console.log(scoreArray); | |
} | |
function errData (data) { | |
// body... | |
} | |
var app = angular.module('myApp', []); | |
// service | |
app.controller('d3ChartController',['$scope','testFactory',d3ChartController]); | |
function d3ChartController($scope, testFactory){ | |
$scope.variable1 = [{a:10, b:50},{city: "Seoul", loc: 24.7}]; | |
$scope.data1 = [{x: 10, y: 10}, {x: 300, y: 50}, {x: 600, y: 150}]; | |
$scope.sayHello = testFactory.sayHello("to the World !!"); | |
} | |
app.controller('JobController',JobController); | |
function JobController(){ | |
this.variable1 = 123; | |
this.variable2 = 456; | |
this.sayHello = function() {return "i am controller2"}; | |
} | |
app.factory('testFactory', function(){ | |
return { | |
sayHello: function(text){ | |
return "Factory says \"Hello " + text + "\""; | |
}, | |
sayGoodbye: function(text){ | |
return "Factory says \"Goodbye " + text + "\""; | |
} | |
}}); | |
app.directive('d3Chart', function(){ | |
return { | |
scope: { data: '=' , radius: '=', color:'='}, | |
restrict: 'E', | |
controller:'d3ChartController', | |
template: "<p>hello world</p>", | |
link: link | |
}; | |
function link(scope, element,attributes) { | |
// the D3 bits.. | |
var margin = {top: 50, right: 40, bottom: 20, left: 120}, | |
width = 960 - margin.left - margin.right, | |
height = 480 - margin.top - margin.bottom; | |
var color2 = d3.scale.category10(); | |
var el = element[0]; | |
//var width = el.clientWidth; | |
//var height = el.clientHeight; | |
var svg = d3.select(el).append('svg') | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var circles = svg.selectAll("circle") | |
.data(scope.data) | |
.enter().append("circle") | |
.attr("cx", function(d){return d.x;}) | |
.attr("cy", function(d){return d.y;}) | |
.attr("r", scope.radius) | |
.style("fill", attributes.color) | |
console.log(width); | |
console.log(scope.variable1[0]); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment