-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChart Setup Function.R
130 lines (109 loc) · 3.28 KB
/
Chart Setup Function.R
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
# Chart set up function
chart_funct =
"
<script src='https://d3js.org/d3.v4.min.js'>
</script>
<script>
function chart(name, values, labels, color, mode) {
var data = {
labels: labels,
series: values};
var barHeight = 20,
groupHeight = barHeight * data.series.length,
gapBetweenGroups = 10,
spaceForLabels = 180,
spaceForLegends = 75;
var zippedData = [];
for (var i=0; i<data.labels.length; i++) {
for (var j=0; j<data.series.length; j++) {
if(data.series[j].values[i] != null)
zippedData.push(data.series[j].values[i]);
}
}
var color = d3.scaleOrdinal()
.range(color);
if(mode == 'gender_multiple'){
color = d3.scaleOrdinal()
.range(['#D2CBB8','#58585A']);
}
var chartHeight = barHeight * zippedData.length + gapBetweenGroups * data.labels.length;
var x = d3.scaleLinear()
.domain([0, d3.max(zippedData)])
.range([0, d3.max(zippedData) * 2]);
var y = d3.scaleLinear()
.range([chartHeight + gapBetweenGroups, 0]);
var yAxis = d3.axisLeft()
.scale(y)
.tickFormat('')
.tickSize(0);
var chart = d3.select(name)
.attr('width', 500)
.attr('height', chartHeight);
var bar = chart.selectAll('g')
.data(zippedData)
.enter().append('g')
.attr('transform', function(d, i) {
return 'translate(' + spaceForLabels + ',' + (i * barHeight + gapBetweenGroups * (0.5 + Math.floor(i/data.series.length))) + ')';
});
bar.append('rect')
.attr('fill', function(d,i) { return color(i % data.series.length); })
.attr('class', 'bar')
.attr('width', 0)
.transition()
.duration(500)
.delay((d, i) => i * 100)
.attr('width', (d) => x(d))
.attr('height', barHeight - 1)
bar.append('text')
.attr('class', 'label')
.attr('x', function(d) { return - 10; })
.attr('y', groupHeight / 2)
.attr('dy', '.35em')
.text(function(d,i) {
if (i % data.series.length === 0)
return data.labels[Math.floor(i/data.series.length)];
else
return ''});
bar.append('text')
.attr('class', 'label2')
.transition()
.duration(450)
.delay((d, i) => i * 100)
.attr('x', function(d) {
return (d * 2) + 5;
})
.attr('y', 14)
.text(function(d,i) {
return d + '%'
});
chart.append('g')
.attr('class', 'y axis')
.attr('transform', 'translate(' + spaceForLabels + ', ' + -gapBetweenGroups/2 + ')')
.call(yAxis);
if(mode == 'multiple' || mode == 'multiple'){
var legendRectSize = 10,
legendSpacing = 4;
var legend = chart.selectAll('.legend')
.data(data.series)
.enter()
.append('g')
.attr('transform', function (d, i) {
var height = legendRectSize + legendSpacing;
var offset = -gapBetweenGroups/2;
var horz = spaceForLabels + d3.max(zippedData) * 2 + 60;
var vert = i * height - offset;
return 'translate(' + horz + ',' + vert + ')';
});
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', function (d, i) { return color(i); })
.style('stroke', function (d, i) { return color(i); });
legend.append('text')
.attr('class', 'legend')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function (d) { return d.label; });
}
}
</script>"