-
Notifications
You must be signed in to change notification settings - Fork 0
/
bathtiles.umd.js
225 lines (208 loc) · 9.63 KB
/
bathtiles.umd.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
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 (root, factory) {
// CommonJS
if (typeof exports === 'object' && typeof module === 'object') {
console.log('Bathtiles - CommonJS')
module.exports = factory(require('d3'), require('tippy.js'));
}
// AMD
else if (typeof define === 'function' && define.amd) {
console.log('Bathtiles - AMD')
define(['d3', 'tippy.js'], factory);
}
// Global variable
else {
console.log('Bathtiles - browser')
root.Bathtiles = factory(root.d3, root.ippy);
}
}(this, function (d3, tippy) {
'use strict';
var dx = 35;
var CELL_SIZE = 14;
var NUMBER_OF_COLORS = 7;
var WIDTH = 900
var HEIGHT = 100
var LEGEND_WIDTH = 800
var LEGEND_HEIGHT = 25
var FONT_SIZE = 12
class Bathtiles {
constructor(json = null, mainColor = '#44a340') {
console.log('Bathtiles.js - Constructing...')
this.data = formatData(json);
this.mainColor = mainColor;
this.colors = this.generateColorScheme(this.mainColor, NUMBER_OF_COLORS);
}
injectTooltipStyles() {
const styles = `
.tippy-box[data-animation=fade][data-state=hidden]{opacity:0}
[data-tippy-root]{max-width:calc(100vw - 10px)}
.tippy-box{position:relative;background-color:#333;color:#fff;border-radius:4px;font-size:14px;line-height:1.4;outline:0;transition-property:transform,visibility,opacity}
.tippy-box[data-placement^=top]>.tippy-arrow{bottom:0}
.tippy-box[data-placement^=top]>.tippy-arrow:before{bottom:-7px;left:0;border-width:8px 8px 0;border-top-color:initial;transform-origin:center top}
.tippy-box[data-placement^=bottom]>.tippy-arrow{top:0}
.tippy-box[data-placement^=bottom]>.tippy-arrow:before{top:-7px;left:0;border-width:0 8px 8px;border-bottom-color:initial;transform-origin:center bottom}
.tippy-box[data-placement^=left]>.tippy-arrow{right:0}
.tippy-box[data-placement^=left]>.tippy-arrow:before{border-width:8px 0 8px 8px;border-left-color:initial;right:-7px;transform-origin:center left}
.tippy-box[data-placement^=right]>.tippy-arrow{left:0}
.tippy-box[data-placement^=right]>.tippy-arrow:before{left:-7px;border-width:8px 8px 8px 0;border-right-color:initial;transform-origin:center right}
.tippy-box[data-inertia][data-state=visible]{transition-timing-function:cubic-bezier(.54,1.5,.38,1.11)}
.tippy-arrow{width:16px;height:16px;color:#333}
.tippy-arrow:before{content:"";position:absolute;border-color:transparent;border-style:solid}
.tippy-content{position:relative;padding:5px 9px;z-index:1;}
`;
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
}
generateColorScheme(baseColor, numColors) {
const colorScale = d3.scaleSequential(d3.interpolateRgb("#eee", baseColor)).domain([0, numColors-1]);
return Array.from({ length: numColors }, (_, i) => colorScale(i));
}
load(target) {
console.log('Bathtiles.js - Loading...')
// Clear existing heatmap elements
d3.select('#js-heatmap').selectAll('*').remove();
d3.select('#js-months').selectAll('*').remove();
d3.select('#js-legend').selectAll('*').remove();
// Set Tippy.js styles
this.injectTooltipStyles();
// Heapmap
const bathtilesHeatmap = document.createElement('div');
bathtilesHeatmap.id = 'js-heatmap';
target.appendChild(bathtilesHeatmap);
// Months Label
const bathtilesMonths = document.createElement('div');
bathtilesMonths.id = 'js-months';
target.appendChild(bathtilesMonths);
// Legend
const bathtilesLegend = document.createElement('div');
bathtilesLegend.id = 'js-legend';
target.appendChild(bathtilesLegend);
// Setup Range
var yearFormat = d3.utcFormat('%Y');
var startYear = yearFormat(this.data.startDate);
var endYear = Number(yearFormat(new Date())) + 1;
// Get Colors based on value
var formatColor = d3.scaleQuantize()
.domain([1, this.data.maxCount])
.range(this.colors.slice(1, NUMBER_OF_COLORS));
// Construct heatmap
var heatmapSvg = d3.select('#js-heatmap')
.selectAll('svg')
.data(d3.range(startYear, endYear))
.enter()
.append('svg')
.attr('width', WIDTH)
.attr('height', HEIGHT)
.style('background-color', 'transparent');
var rect = heatmapSvg.append('g')
.attr('transform', `translate(${dx},0)`);
rect.append('text')
.attr('transform', `translate(-9,${CELL_SIZE * 3.5})rotate(-90)`)
.style('text-anchor', 'middle')
.text((d) => d);
// Create a Day square
rect.selectAll('rect')
.data((d) => d3.timeDays(new Date(d, 0, 1), new Date(d + 1, 0, 1)))
.enter()
.append('rect')
.attr('width', CELL_SIZE)
.attr('height', CELL_SIZE)
.attr('x', (d) => d3.utcFormat('%U')(d) * CELL_SIZE)
.attr('y', (d) => d.getDay() * CELL_SIZE)
.style('stroke', '#fff')
.style('stroke-width', 3)
.style('fill', (d) => {
const formattedDate = d3.utcFormat('%Y-%m-%d')(d); // Format the date correctly
const countData = this.data.dates[formattedDate]; // Use the formatted date to get count
console.log(`Date: ${formattedDate}, Count: ${countData}`); // Debugging line
return countData && countData > 0 ? formatColor(countData) : '#eee';
})
.attr('data-tippy-content', (d) => {
const formattedDate = d3.utcFormat('%Y-%m-%d')(d); // Format date for tooltip
const countData = this.data.dates[formattedDate];
const date = d3.utcFormat('%b %d, %Y')(new Date(d));
return !countData ? `No posts on ${date}` : `${countData} post${countData > 1 ? 's' : ''} on ${date}`;
})
.call((s) => tippy(s.nodes(), { offset: [0, 0] }));
// Construct Months label
d3.select('#js-months').selectAll('svg.months')
.data([1])
.enter()
.append('svg')
.attr('width', LEGEND_WIDTH)
.attr('height', LEGEND_HEIGHT)
.attr('dominant-baseline', 'middle')
.append('g')
.attr('transform', 'translate(0,10)')
.selectAll('text')
.data(() => d3.range(12))
.enter()
.append('text')
.attr('x', (d) => d * (4.5 * CELL_SIZE) + dx)
.text((d) => d3.utcFormat('%b')(new Date(0, d + 1, 0)))
// Construct Legend
var legendSvg = d3.select('#js-legend').selectAll('svg.legend')
.enter()
.append('svg')
.data([1])
.enter()
.append('svg')
.attr('width', LEGEND_WIDTH)
.attr('height', LEGEND_HEIGHT)
.append('g')
.attr('transform', 'translate(644,0)')
.selectAll('.legend-grid')
.data(() => d3.range(NUMBER_OF_COLORS))
.enter()
.append('rect')
.attr('width', CELL_SIZE)
.attr('height', CELL_SIZE)
.attr('x', (d) => d * CELL_SIZE + dx)
.style('stroke', '#fff')
.style('stroke-width', 3)
.style('fill', (d) => this.colors[d]);
console.log('Bathtiles.js - Finished!')
}
import(json = null) {
if (json) {
this.data = formatData(json); // Assuming formatData processes the JSON and returns the formatted data
console.log('Data updated:', this.data);
} else {
console.log('No data to import.');
}
}
};
// Formats the JSON data to be easily ready by d3.
function formatData(data) {
console.log('Bathtiles.js - Formatting Data...', data);
if (!data || !data['submissionCalendar']) {
const defaultStartDate = new Date();
return {
startDate: defaultStartDate,
dates: {},
maxCount: 0
};
}
const parsedData = JSON.parse(data['submissionCalendar']);
const dateTable = {};
let oldestDate = new Date();
let maxCount = 0;
const formatDate = d3.utcFormat('%Y-%m-%d');
for (const [timestamp, count] of Object.entries(parsedData)) {
const date = new Date(timestamp * 1000);
const formattedDate = formatDate(date);
dateTable[formattedDate] = count;
if (timestamp < oldestDate) {
oldestDate = formattedDate;
}
maxCount = Math.max(maxCount, dateTable[formattedDate]);
}
return {
startDate: new Date(oldestDate),
dates: dateTable,
maxCount
};
}
return Bathtiles;
}));