forked from KohoVolit/widget-hemicycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3.orloj.js
114 lines (102 loc) · 3.97 KB
/
d3.orloj.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
d3.orloj = function() {
//defaults
var width = 400,
rows = 1,
icons = [],
iconHeight = null,
line = true;
//note: other defaults - for individual icons - are defined inside the code
function orloj(selection) {
selection.each(function(d,i) {
//parameters may be functions and we MUST to rename them
var width_val = (typeof(width) === "function" ? width(d) : width),
rows_val = (typeof(rows) === "function" ? rows(d) : rows),
iconHeight_val = (typeof(iconHeight) === "function" ? iconHeight(d) : iconHeight),
icons_val = (typeof(icons) === "function" ? icons(d) : icons),
line_val = (typeof(line) === "function" ? line(d) : line);
var nicons = icons_val.length,
maxinrow = Math.ceil(nicons/rows_val)
//scales
var xScale = d3.scale.linear()
.domain([0, 1])
.range([0, width_val]),
yScale = d3.scale.linear()
.domain([0, 1])
.range([0,rows_val*width_val/16]);
var element = d3.select(this);
if (line_val)
element.append("line")
.attr("x1",xScale(0))
.attr("y1",yScale(0))
.attr("x2",xScale(1))
.attr("y2",yScale(0))
.attr("stroke-width",1)
.attr("stroke","gray")
.attr("stroke-opacity",0.5);
element.selectAll('.orloj-icon')
.data(icons_val)
.enter()
.append("text")
.attr('x', function(d,i) {
row = Math.floor(i/maxinrow)+1;
if (row == rows_val) inrow = nicons - (rows_val-1)*maxinrow;
else inrow = maxinrow;
posinrow = i % maxinrow;
return xScale((posinrow+0.5)/(inrow));
})
.attr('y', function(d,i) {
row = Math.floor(i/maxinrow)+1;
return yScale(0.05+0.9/rows_val*row);
})
.attr('font-size',function(d) {
if (!iconHeight_val) return yScale(0.9/rows_val)
else return iconHeight_val;
})
.attr("text-anchor","middle")
.classed("orloj-icon",true)
.append("tspan")
.attr("font-family","FontAwesome")
.text(function(d) {
if (typeof(d.icon) === 'undefined') return '\uf007';
else return d.icon;
})
.attr("fill", function(d) {
if (typeof(d.color) === 'undefined') return 'gray';
else return d.color;
})
.append("tspan")
.attr('font-family', "'Helvetica Neue', Helvetica, Arial, sans-serif")
.text(function(d) {return ' ' + d.text})
.attr('fill', function(d) {
if (typeof(d.color) === 'undefined') return 'gray';
else return d.color;
});
})
}
orloj.width = function(value) {
if (!arguments.length) return value;
width = value;
return orloj;
};
orloj.rows = function(value) {
if (!arguments.length) return value;
rows = value;
return orloj;
};
orloj.icons = function(value) {
if (!arguments.length) return value;
icons = value;
return orloj;
};
orloj.iconHeight = function(value) {
if (!arguments.length) return value;
iconHeight = value;
return orloj;
};
orloj.line = function(value) {
if (!arguments.length) return value;
line = value;
return orloj;
};
return orloj;
}