-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscatterdirective.js
More file actions
144 lines (114 loc) · 4.51 KB
/
scatterdirective.js
File metadata and controls
144 lines (114 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(function(angular) {
'use strict';
angular.module('scatterchart', [])
.directive("scatterChart",function(){
return {
restrict:'E',
scope: {
data: '='
},
link: function(scope, element, attrs) {
var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = 700 - margin.left - margin.right,
height = 680 - margin.top - margin.bottom;
var xAxis,yAxis;
var i = 4;
var rectangle,rectangle1,label1, label2, label0, labely0, labely1, labely2;
var xScale = d3.scale.linear().range([0, width]);
var yScale = d3.scale.linear().range([height, 0]);
xScale.domain([0, 100]);
yScale.domain([0, 100]);
// tooltip
var tip = d3.tip()
.attr("class", "d3-tip")
.offset([-10, 0])
.html(function(d) {
return d.Subject + "<br>" + Math.round(d.Marks1*10000)/10000 + ", " + Math.round(d.Marks2*10000)/10000;
});
var zoomBeh = d3.behavior.zoom()
.x(xScale)
.y(yScale)
.scaleExtent([-10, 100])
.on("zoom", zoom);
var svg = d3.select("#scatter").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 + ")")
.call(zoomBeh);
svg.call(tip);
scope.$watch('data', function(newVals, oldVals) {
scope.render(newVals);
}, true);
scope.render = function(data){
// setup x
svg.selectAll("*").remove();
var xValue = function(d) { return d.Marks1;}, // data -> value
xMap = function(d) { return xScale(xValue(d));}; // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom").tickSize(-height);;
// setup y
var yValue = function(d) { return d["Marks2"];}, // data -> value
yMap = function(d) { return yScale(yValue(d));} // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left").tickSize(-width);
// setup fill color
var cValue = function(d) { return d.Subject;},
color = d3.scale.category10();
// rectangle for smooth zoom
var rect2 = svg.append("rect")
.attr("width", width)
.attr("height", height)
// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.attr("text-anchor", "end")
.text("Marks1")
// y-axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Marks2")
var objects = svg.append("svg")
.classed("objects", true)
.attr("width", width)
.attr("height", height);
// draw dots
objects.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
// .attr("cx", function (d) { console.log(d.MArks1)return d.Marks1; })
// .attr("cy", function (d) { return d.Marks2; })
.attr("r", 5)
.attr("transform", transform)
.style("fill", function(d) { return color(cValue(d));})
.on("mouseover", tip.show)
.on("mouseout", tip.hide);
}
// zoom function
function zoom() {
console.log("here3")
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
svg.selectAll(".dot")
.attr("transform", transform);
}
// transale the dots when zoom
function transform(d) {
return "translate(" + xScale(d.Marks1) + "," + yScale(d.Marks2) + ")";
}
}
}
});
})(window.angular);