Last active
May 9, 2016 15:57
-
-
Save anbnyc/437bb84c3497bff6772d68b202433144 to your computer and use it in GitHub Desktop.
Drag-and-drop Student Planning demo
This file contains hidden or 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
<html ng-app="plan-app"> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-aria.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-messages.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.6/angular-material.js"></script> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.6/angular-material.css"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.6/angular-material.layouts.css"> | |
<style> | |
.vizContainer{ | |
width: 100%; | |
height: 100%; | |
font-size: 11px; | |
} | |
.node{ | |
fill: rgba(0,0,0,1); | |
} | |
rect{ | |
color: green; | |
opacity: .2; | |
} | |
</style> | |
</head> | |
<body ng-controller="PlanAppController as ctrl"> | |
<div layout="row"> | |
<div><pre>{{data | json}}</pre></div> | |
<div class="vizContainer"> | |
<plan-directive | |
data="data" | |
viz-width="vizWidth" | |
viz-height="vizHeight"> | |
</plan-directive> | |
</div> | |
</div> | |
</body> | |
<script> | |
(function(){ | |
'use strict'; | |
var app = angular.module('plan-app', ['ngMaterial']); | |
app.controller('PlanAppController', function($scope, $http){ | |
$scope.data = [ | |
{ | |
'osis': 5001 | |
}, | |
{ | |
'osis': 5002 | |
}, | |
{ | |
'osis': 5003 | |
} | |
]; | |
$scope.vizWidth = angular.element(document.querySelector(".vizContainer"))[0].offsetWidth; | |
$scope.vizHeight = angular.element(document.querySelector(".vizContainer"))[0].offsetHeight; | |
}) | |
.directive('planDirective', function(){ | |
return { | |
link: link, | |
restrict: 'E', | |
scope: { | |
data: '=', | |
vizWidth: '=', | |
vizHeight: '=' | |
} | |
} | |
function link(scope,element,attr){ | |
var width = scope.vizWidth, | |
height = scope.vizHeight; | |
var force = d3.layout.force() | |
.size([width, height]) | |
.charge(-400) | |
.linkDistance(40) | |
.on("tick", tick); | |
var drag = force.drag() | |
.on("dragstart", dragstart) | |
.on("dragend",dragend); | |
var svg = d3.select(".vizContainer") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var startbox = svg.append("rect") | |
.attr("class","startbox") | |
.attr("width", width/2) | |
.attr("height", height); | |
force | |
.nodes(scope.data) | |
.links([]) | |
.start(); | |
var node = svg.selectAll(".node") | |
.data(scope.data) | |
.enter().append("circle") | |
.attr("class", "node") | |
.attr("r", 12) | |
.on("dblclick", dblclick) | |
.call(drag); | |
function tick() { | |
node.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
} | |
function dblclick(d) { | |
d3.select(this).classed("fixed", d.fixed = false); | |
} | |
function dragstart(d) { | |
d3.select(this).classed("fixed", d.fixed = true); | |
} | |
function dragend(d){ | |
d.newX = +d3.select(this).attr("cx"); | |
scope.$apply(); | |
} | |
} | |
}); | |
})(); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment