-
Notifications
You must be signed in to change notification settings - Fork 2
/
outof.js
119 lines (96 loc) · 2.94 KB
/
outof.js
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
function outof() {
var margin = {top: 5, right: 5, bottom: 5, left: 5},
width = 400,
height = 400,
max = 100,
xValue = function(d, i) { return i; },
yValue = function(d) { return d; },
symbol = d3.svg.symbol(),
xScale = d3.time.scale(),
yScale = d3.scale.linear(),
area = d3.svg.area().x(X).y1(Y),
line = d3.svg.line().x(X).y(Y),
interpolate = 'linear';
function chart(selection) {
selection.each(function(data) {
var total_space = (width - margin.top - margin.bottom) *
(height - margin.left - margin.right);
var per_feature = total_space / max;
var feature_size = Math.floor(Math.sqrt(per_feature));
var xmax = Math.floor(width / feature_size);
var ymax = Math.floor(height / feature_size);
// Select the svg element, if it exists.
var svg = d3.select(this).selectAll("svg").data([data]);
// Otherwise, create the skeletal chart.
var gEnter = svg.enter().append("svg").append("g");
// Update the outer dimensions.
svg .attr("width", width)
.attr("height", height);
// Update the inner dimensions.
var g = svg.select("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var filled_vs = d3.range(max).map(function(m) {
return {
x: m % xmax,
y: Math.floor(m / xmax),
on: m <= data,
m: m
};
});
var pts = g.selectAll('path.out-of')
.data(filled_vs, function(d) {
return d.m;
});
symbol.size((feature_size * feature_size) * 0.6);
pts.exit();
pts.enter()
.append('path')
.attr('class', 'out-of');
pts
.attr('d', symbol)
.classed('on', function(d) { return d.on; })
.attr('transform', function(d) {
return 'translate(' + [d.x * feature_size, d.y * feature_size] + ')';
});
});
}
// The x-accessor for the path generator; xScale ∘ xValue.
function X(d) {
return xScale(d[0]);
}
// The x-accessor for the path generator; yScale ∘ yValue.
function Y(d) {
return yScale(d[1]);
}
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.max = function(_) {
if (!arguments.length) return max;
max = _;
return chart;
};
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
return chart;
};
chart.height = function(_) {
if (!arguments.length) return height;
height = _;
return chart;
};
chart.x = function(_) {
if (!arguments.length) return xValue;
xValue = _;
return chart;
};
chart.y = function(_) {
if (!arguments.length) return yValue;
yValue = _;
return chart;
};
return chart;
}