This repository was archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphs.js
More file actions
205 lines (179 loc) · 6.63 KB
/
graphs.js
File metadata and controls
205 lines (179 loc) · 6.63 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(function(d3) {
var containerWidth = function(selector) {
return parseInt(d3.select(selector).style('width'))
},
truncate = function(s, max) {
if (s.length > max) {
s = s.substring(0, max-1) + '.';
}
return s;
};
d3.graph = {};
d3.graph.bar = function(selector, data, options) {
var defaults = {
barHeight: 30,
barPadding: 8,
labelRatio: 4,
labelPadding: 6,
valueRatio: 7.5,
valueOffset: 10,
barCount: data.length
};
if ('undefined' !== typeof options && null !== options) {
for (o in options) defaults[o] = options[o];
}
options = defaults;
var getLink = function(d) {
return d.link ? d.link : null;
};
// responsive dimensions
var chartWidth = containerWidth(selector),
labelWidth = chartWidth / options.labelRatio,
valuelWidth = chartWidth / options.valueRatio,
barWidth = chartWidth - labelWidth,
chartHeight = options.barHeight * options.barCount,
// scales, texts, offsets
maxVal = d3.max(data, function(d) { return d.value }),
wscale = d3.scale.linear().domain([0, maxVal]).range(['0px', barWidth - valuelWidth + 'px'])
title = function(d) {return d.key + ': ' + d.value},
y = function(d, i) {return i * options.barHeight},
valueY = function(d, i) {return i * options.barHeight + (options.barHeight/2)},
// labels
labelClick = function(d) {
if (href = getLink(d)) {
document.location.href = href;
}
},
labelClass = function(d) {
return getLink(d) ? 'barlabel link' : 'barlabel';
},
labelTitle = function(d) {
var title = d.key,
href = getLink(d);
if (href) {
title = 'Click to open ' + href;
}
return title;
},
// formats
formatValue = d3.format(',d'),
// bar and graph objects
bar = {
selector: selector,
options: options,
data: data.slice(0, options.barCount)
},
graph = d3.select(selector);
graph.selectAll('svg').remove();
var svg = graph.append('svg')
.attr('class', 'bar')
.attr('width', chartWidth)
.attr('height', chartHeight - options.barPadding);
bar.values = function() {
var values = svg.selectAll('text').data(bar.data).enter();
values.append('text')
.attr('class', 'barvalue')
.attr('x', chartWidth - valuelWidth + options.valueOffset)
.attr('y', valueY)
.attr('text-anchor', 'start')
.text(function(d) {return formatValue(d.value)});
};
bar.horizontal = function() {
svg.selectAll('rect')
.data(bar.data)
.enter().append('rect')
.attr('x', labelWidth)
.attr('y', function(d, i) {return i * options.barHeight})
.attr('width', function(d) {return isNaN(d.value) ? 0 : wscale(d.value)})
.attr('height', options.barHeight - options.barPadding)
.append('title').text(title);
var labels = svg.selectAll('text').data(bar.data).enter();
labels.append('text')
.attr('class', labelClass)
.attr('x', 0)
.attr('y', valueY)
.attr('text-anchor', 'start')
.text(function(d) {return truncate(d.key, 16)})
.on('click', labelClick)
.append('title')
.text(labelTitle);
// FIXME calling bar.values doesn't show values, why?
labels.append('text')
.attr('class', 'barvalue')
.attr('x', chartWidth - valuelWidth + options.valueOffset)
.attr('y', valueY)
.attr('text-anchor', 'start')
.text(function(d) {return formatValue(d.value)});
};
bar.horizontalImage = function() {
svg.selectAll('rect')
.data(bar.data)
.enter().append('rect')
.attr('width', function(d) {return isNaN(d.value) ? 0 : wscale(d.value)})
.attr('height', options.barHeight - options.barPadding)
.attr('x', options.imageWidth)
.attr('y', y)
.append('title').text(title);
var labels = svg.selectAll('image').data(bar.data).enter();
labels.append('svg:image')
.attr('xlink:href', function(d) { return d.image.src })
.attr('class', labelClass)
.attr('width', options.imageWidth - options.barPadding)
.attr('height', options.barHeight - options.barPadding)
.attr('x', 0)
.attr('y', y)
.on('click', labelClick)
.append('title')
.text(labelTitle);
bar.values();
};
// no data
if (bar.data.length == 0) {
svg.append('text')
.attr('class', 'no-data')
.attr('x', chartWidth/2)
.attr('y', chartHeight/2)
.attr('text-anchor', 'middle')
.text('No Data');
}
return bar;
}
d3.graph.pie = function(selector, data) {
var chartWidth = containerWidth(selector),
chartHeight = chartWidth,
radius = Math.min(chartWidth, chartHeight) / 2,
color = d3.scale.category10().domain(d3.keys(data)),
arc = d3.svg.arc().outerRadius(radius - 10).innerRadius(0);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.value; });
var svg = d3.select(selector).append('svg')
.attr('width', chartWidth)
.attr('height', chartHeight)
.append('g')
.attr('transform', 'translate(' + chartWidth / 2 + ',' + chartHeight / 2 + ')');
var g = svg.selectAll('.arc')
.data(pie(data))
.enter().append('g')
.attr('class', 'arc');
g.append('path')
.attr('d', arc)
.style('fill', function(d) { return d.data.color || color(d.data.key); });
var glabels = d3.select(selector).append('svg')
.attr('width', chartWidth)
.attr('height', 50);
glabels.selectAll('text').data(data).enter()
.append('text')
.attr('x', 30)
.attr('y', function(d, i) {return (1 + i) * 20})
.style('text-anchor', 'start')
.text(function(d) { return d.key + ': ' + d.value });
glabels.selectAll('rect').data(data).enter()
.append('rect')
.attr('x', 10)
.attr('y', function(d, i) {return ((1 + i) * 20) - 10})
.attr('width', 10)
.attr('height', 10)
.style('fill', function(d) { return d.color || color(d.key); });
}
})(d3);