forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart-core.js
More file actions
225 lines (188 loc) · 9.17 KB
/
chart-core.js
File metadata and controls
225 lines (188 loc) · 9.17 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function donutChart() {
var width,
height,
margin = { top: 10, right: 10, bottom: 10, left: 10 },
colour = d3.scaleOrdinal(d3.schemeCategory20), // colour scheme
variable, // value in data that will dictate proportions on chart
category, // compare data by
padAngle, // effectively dictates the gap between slices
floatFormat = d3.format('.4r'),
cornerRadius, // sets how rounded the corners are on each slice
percentFormat = d3.format(',.2%');
function chart(selection) {
selection.each(function (data) {
// generate chart
// ===========================================================================================
// Set up constructors for making donut. See https://github.com/d3/d3-shape/blob/master/README.md
var radius = 135;
// creates a new pie generator
var pie = d3.pie()
.value(function (d) { return floatFormat(d[variable]); })
.sort(null);
// contructs and arc generator. This will be used for the donut. The difference between outer and inner
// radius will dictate the thickness of the donut
var arc = d3.arc()
.outerRadius(radius * 0.9)
.innerRadius(radius * 0.6)
.cornerRadius(cornerRadius)
.padAngle(padAngle);
// this arc is used for aligning the text labels
var outerArc = d3.arc()
.outerRadius(radius * 0.9)
.innerRadius(radius * 0.9);
// ===========================================================================================
var tempAngle;
var tempOffset = {
x: 1,
y: 0.95
};
// ===========================================================================================
// append the svg object to the selection
var svg = selection.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.attr('class', 'backColorChart')
.append('g')
.attr('transform', 'translate(' + (width / 2 - 105) + ',' + (height / 2 + 12) + ')');
// ===========================================================================================
// ===========================================================================================
// g elements to keep elements within svg modular
svg.append('g').attr('class', 'slices');
svg.append('g').attr('class', 'labelName');
svg.append('g').attr('class', 'lines');
// ===========================================================================================
// ===========================================================================================
// add and colour the donut slices
var path = svg.select('.slices')
.datum(data).selectAll('path')
.data(pie)
.enter().append('path')
.attr('fill', function (d) { return colour(d.data[category]); })
.attr('d', arc)
.attr('id', function (d) { return d.data[category]; });
// ===========================================================================================
var legendG = svg.selectAll(".legend") // note appending it to mySvg and not svg to make positioning easier
.data(pie(data))
.enter().append("g")
.attr("transform", function (d, i) {
return "translate(" + (130) + "," + (i * 20 - 40) + ")"; // place each legend on the right and bump each one down 15 pixels
})
.attr("class", "legend");
legendG.append("rect") // make a matching color rect
.attr("width", 10)
.attr("height", 10)
.attr("fill", function (d, i) {
return colour(d.data[category]);
});
legendG.append("text") // add the text
.text(function (d) {
return d.data.url;
})
.style("font-size", 13)
.attr("y", 10)
.attr("x", 11);
// ===========================================================================================
// add tooltip to mouse events on slices and labels
d3.selectAll('.labelName text, .slices path').call(toolTip);
// ===========================================================================================
// ===========================================================================================
// Functions
// calculates the angle for the middle of a slice
function midAngle(d) { return d.startAngle + (d.endAngle - d.startAngle) / 2; }
// function that creates and adds the tool tip to a selected element
function toolTip(selection) {
// add tooltip (svg circle element) when mouse enters label or slice
selection.on('mouseenter', function (data) {
d3.selectAll('.toolCircle').remove();
svg.append('text')
.attr('class', 'toolCircle')
.attr('dy', -15) // hard-coded. can adjust this to adjust text vertical alignment in tooltip
.html(toolTipHTML(data)) // add text to the circle.
.style('font-size', '.9em')
.style('text-anchor', 'middle'); // centres text in tooltip
svg.append('circle')
.attr('class', 'toolCircle')
.attr('r', radius * 0.55) // radius of tooltip circle
.style('fill', colour(data.data[category])) // colour based on category mouse is over
.style('fill-opacity', 0.35);
});
// remove the tooltip when mouse leaves the slice/label
selection.on('mouseout', function () {
d3.selectAll('.toolCircle').remove();
});
}
// function to create the HTML string for the tool tip. Loops through each key in data object
// and returns the html string key: value
function toolTipHTML(data) {
var tip = '',
i = 0;
for (var key in data.data) {
// if value is a number, format it as a percentage
var value = (!isNaN(parseFloat(data.data[key]))) ? percentFormat(data.data[key]) : data.data[key];
if (key === 'summary')
value = convertSummaryTimeToString(data.data[key]);
var className = '';
if (key === 'percentage')
className = 'class="percentageValue"';
// leave off 'dy' attr for first tspan so the 'dy' attr on text element works. The 'dy' attr on
// tspan effectively imitates a line break.
if (i === 0) tip += '<tspan x="0">' + value + '</tspan>';
else tip += '<tspan x="0" dy="1.2em"' + className + '>' + value + '</tspan>';
i++;
}
return tip;
}
function angleIsInRangeDifference(tempAngle, currentAngle, difference) {
return currentAngle < (tempAngle + difference) && currentAngle > (tempAngle - difference);
}
// ===========================================================================================
});
}
// getter and setter functions. See Mike Bostocks post "Towards Reusable Charts" for a tutorial on how this works.
chart.width = function (value) {
if (!arguments.length) return width;
width = value;
return chart;
};
chart.height = function (value) {
if (!arguments.length) return height;
height = value;
return chart;
};
chart.margin = function (value) {
if (!arguments.length) return margin;
margin = value;
return chart;
};
chart.radius = function (value) {
if (!arguments.length) return radius;
radius = value;
return chart;
};
chart.padAngle = function (value) {
if (!arguments.length) return padAngle;
padAngle = value;
return chart;
};
chart.cornerRadius = function (value) {
if (!arguments.length) return cornerRadius;
cornerRadius = value;
return chart;
};
chart.colour = function (value) {
if (!arguments.length) return colour;
colour = value;
return chart;
};
chart.variable = function (value) {
if (!arguments.length) return variable;
variable = value;
return chart;
};
chart.category = function (value) {
if (!arguments.length) return category;
category = value;
return chart;
};
return chart;
}