-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
525 lines (451 loc) · 21 KB
/
index.html
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="An interactive hill chart with draggable tasks." />
<title>Hill Chart</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css"
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.9.0/d3.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style>
.task {
cursor: pointer;
font-size: 12px;
}
.task-label {
fill: black;
font-size: 12px;
font-weight: bold;
text-align: center;
}
.task-point {
cursor: grab;
}
.hill-path {
stroke: gray;
stroke-width: 1;
fill: none;
}
@media print {
@media (min-width: 624px) {
svg {
scale: 0.5;
}
}
}
</style>
</head>
<body class="font-sans m-2 bg-gray-100 min-h-screen flex flex-col items-center print:bg-transparent">
<!-- Navbar -->
<nav class="w-full p-4 flex flex-col md:flex-row print:flex-row justify-between items-center">
<input
id="chartTitle"
class="bg-transparent focus:outline-none text-2xl md:text-3xl font-bold mb-4 md:mb-0 text-center md:text-left"
type="text"
placeholder="Hill Chart"
/>
<div class="flex space-x-2">
<!-- Print button -->
<button id="print" class="py-1 px-2 rounded hover:text-blue-600 print:hidden text-sm">
<i class="fa-solid fa-print"></i>
Print Page
</button>
<!-- Export PNG button -->
<button id="exportPNG" class="py-1 px-2 rounded hover:text-blue-600 print:hidden text-sm">
<i class="fa-solid fa-file-arrow-down"></i>
Export PNG
</button>
<!-- Clear tasks button -->
<button id="deleteAll" class="py-1 px-2 rounded hover:text-red-600 print:hidden text-sm">
<i class="fa-solid fa-circle-exclamation"></i>
Reset Chart
</button>
</div>
</nav>
<svg
id="chart"
class="bg-white shadow-md rounded-lg print:shadow-none print:bg-transparent"
role="img"
aria-labelledby="chart-title chart-desc"
>
<title id="chart-title">Hill Chart</title>
<desc id="chart-desc">A dynamic hill chart with draggable tasks</desc>
</svg>
<button id="addTask" class="py-1 px-8 mt-4 bg-green-500 text-white rounded hover:bg-green-600 print:hidden">
<i class="fa-regular fa-square-plus"></i>
</button>
<section
class="tasks-container mt-4 bg-white p-6 rounded-lg shadow-md w-full max-w-2xl print:shadow-none print:bg-transparent"
aria-labelledby="tasks-title"
>
<ul id="taskList" class="space-y-2">
<!-- Dynamic task items will be inserted here -->
</ul>
</section>
<script>
// Constants
const width = screen.width * 0.9 > 800 ? 1200 : screen.width * 0.9;
const height = width / 3;
const xMin = 20;
const xMax = width - 20;
const xMid = (xMin + xMax) / 2;
// const amplitude = 150; // Amplitude: the peak deviation of the function from its central value
// const period = 1200; // Period: the length of one complete cycle of the sine wave
// const phaseShift = 300; // Phase Shift: horizontal shift of the sine wave
// const verticalShift = 200; // Vertical Shift: vertical displacement of the sine wave
const amplitude = height / 2.5;
const period = width;
const phaseShift = width / 4;
const verticalShift = height / 2;
// Calculate the coefficient for the period
const B = (2 * Math.PI) / period; // Coefficient for period: determines the frequency of the sine wave
// Define the function using the parameters
const hillFn = (x) => amplitude * Math.sin(B * (x + phaseShift)) + verticalShift;
const colorPalette = ["#3498db", "#e74c3c", "#2ecc71", "#9b59b6", "#f1c40f"];
const chartTitleElement = document.getElementById("chartTitle");
const taskList = document.getElementById("taskList");
const svg = d3.select("#chart").attr("width", width).attr("height", height);
let demoTasks = [
{ id: 1, label: "Feature A", x: xMin + 50, color: "#1f77b4", completed: false },
{ id: 2, label: "Feature B", x: xMid - xMid / 2, color: "#ff7f0e", completed: false },
{ id: 3, label: "Feature C", x: xMid - xMid / 3, color: "#2ca02c", completed: false },
].map((d) => ({ ...d, y: hillFn(d.x) }));
let tasks = [...demoTasks];
let selectedTask = null;
let completedTasksPoint;
let isDragging = false;
const maxTaskTextLength = 64;
// Initialize the hill chart
function initHillChart() {
const hillPoints = d3.range(xMin, xMax).map((x) => ({ x, y: hillFn(x) }));
svg.append("path")
.datum(hillPoints)
.attr("class", "hill-path")
.attr(
"d",
d3
.line()
.curve(d3.curveBasis)
.x((d) => d.x)
.y((d) => d.y),
);
svg.append("line")
.attr("x1", xMid)
.attr("x2", xMid)
.attr("y1", hillFn(xMid) + height / 8)
.attr("y2", amplitude + verticalShift - height / 8)
.attr("stroke", "gray")
.attr("stroke-dasharray", "5,5");
const labels = [
{ x: xMid - xMid / 2, text: "RESEARCHING" },
{ x: xMid + xMid / 2, text: "DEVELOPING" },
];
labels.forEach(({ x, text }) => {
svg.append("text")
.attr("x", x)
.attr("y", amplitude + verticalShift + 10)
.attr("text-anchor", "middle")
.attr("font-size", screen.width > 640 ? "14" : "10")
.text(text);
});
}
// Function to load tasks from localStorage
function loadTasks() {
const tasksJson = localStorage.getItem("tasks");
if (tasksJson) {
tasks = JSON.parse(tasksJson);
}
}
// Function to save tasks to localStorage
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
renderTaskList();
}
function resetPage() {
if (
confirm("Are you sure you want to delete ALL tasks on this page?\n\nThis action cannot be undone.")
) {
localStorage.removeItem("chartTitle");
localStorage.removeItem("tasks");
location.reload();
}
}
function loadChartTitle() {
const title = localStorage.getItem("chartTitle");
chartTitleElement.value = title;
}
function updateChartTitle() {
const chartTitle = chartTitleElement.value;
localStorage.setItem("chartTitle", chartTitle);
}
function removeChartTitle() {
chartTitleElement.value = "";
updateChartTitle();
loadChartTitle();
}
// Render all task points on the chart
function renderTasksOnChart() {
const taskPoints = svg.selectAll(".task").data(
tasks.filter((d) => !d.completed),
(d) => d.id,
);
const taskGroup = taskPoints
.enter()
.append("g")
.attr("class", "task")
.attr("transform", (d) => `translate(${d.x},${d.y})`)
.style("cursor", "grab")
.call(d3.drag().on("start", dragStarted).on("drag", dragged).on("end", dragEnded))
.on("mouseover", highlightTaskPoint)
.on("mouseout", unhighlightTaskPoint);
taskGroup
.append("circle")
.attr("r", width / 100)
.attr("class", "task-point")
.attr("fill", (d) => d.color);
taskGroup
.append("text")
.attr("class", "task-label")
.attr("x", (d) => (width / 100 + 5) * (d.x > xMid ? -1 : 1)) // Adjust x position based on the point's position relative to xMid
.attr("y", 5) // Center the text vertically
.attr("text-anchor", (d) => (d.x > xMid ? "end" : "start")) // Align text to the end or start based on the point's position
.text((d) => (d.label.length > 12 ? d.label.slice(0, 12) + "..." : d.label));
taskPoints.exit().remove();
}
function highlightTaskPoint(d) {
if (isDragging) return;
d3.select(this)
.select("circle")
.attr("r", width / 80);
}
function unhighlightTaskPoint(d) {
if (isDragging) return;
d3.select(this)
.select("circle")
.attr("r", width / 100);
d3.select(this).select("text");
}
// Render task list in the DOM
function renderTaskList() {
taskList.innerHTML = ""; // Clear existing tasks
// Sort tasks to place completed tasks at the bottom
const sortedTasks = tasks.slice().sort((a, b) => {
if (a.completed === b.completed) {
return a.x - b.x;
}
return a.completed - b.completed;
});
sortedTasks.forEach((task) => {
const taskItem = createTaskListItem(task);
taskList.appendChild(taskItem);
});
renderCompletedTasksPoint();
}
// Create a single task list item
function createTaskListItem(task) {
const taskItem = document.createElement("li");
taskItem.innerHTML = `
<div class="flex items-center justify-between px-2 hover:bg-gray-200 active:bg-gray-200 group">
<label class="color-indicator mr-5 bg-transparent text-xl" style="color: ${task.completed ? "#c2c2c2" : task.color};">
<input class="task-color-input hidden" type="color" value="${task.color}" data-task-id="${task.id}" ${task.completed ? "disabled" : ""}/>
<i class="fa-solid fa-circle"></i>
</label>
<div class="flex items-center flex-1">
<i class="task-status-icon hover:cursor-pointer fa-regular mr-2 ${task.completed ? "fa-check-square text-green-500" : "fa-square text-blue-500"}" data-task-id="${task.id}"></i>
<input class="task-label-input flex-1 bg-transparent focus:outline-none p-1 ${task.completed ? "line-through text-gray-300" : ""}" type="text" value="${task.label}" data-task-id="${task.id}" maxlength=${maxTaskTextLength} ${task.completed ? "disabled" : ""}/>
</div>
<button class="removeTask px-3 ml-2 text-red-300 hover:text-red-500 print:hidden">
<i class="fa-solid fa-trash hidden group-hover:block"></i>
</button>
</div>
`;
addTaskItemEventListeners(taskItem, task);
return taskItem;
}
// Add event listeners to a task list item
function addTaskItemEventListeners(taskItem, task) {
taskItem.querySelector(".task-label-input").addEventListener("change", (event) => {
task.label = event.target.value;
saveTasks();
updateTaskDisplay(task);
});
taskItem.querySelector(".task-color-input").addEventListener("input", (event) => {
task.color = event.target.value;
saveTasks();
updateTaskDisplay(task);
});
taskItem.querySelector(".task-status-icon").addEventListener("click", () => {
toggleTaskCompletion(task.id);
saveTasks();
renderTaskList();
});
taskItem.querySelector(".removeTask").addEventListener("click", () => {
removeTask(task.id);
saveTasks();
renderTaskList();
});
}
// Update the task display on the chart
function updateTaskDisplay(task) {
const taskElement = svg.selectAll(".task").filter((d) => d.id === task.id);
taskElement.select("text").text((d) => (d.label.length > 12 ? d.label.slice(0, 12) + "..." : d.label));
taskElement.select("circle").attr("fill", (d) => d.color);
}
// Handle task dragging
function dragStarted(event, d) {
isDragging = true;
d3.select(this).select("circle").attr("cursor", "grabbing");
}
function dragged(event, d) {
d.x = Math.max(xMin, Math.min(xMax, event.x));
d.y = hillFn(d.x);
d3.select(this).attr("transform", `translate(${d.x},${d.y})`);
d3.select(this)
.select("text")
.attr("text-anchor", (d) => (d.x > xMid ? "end" : "start")) // Align text to the end or start based on the point's position
.attr("x", (d) => (width / 100 + 5) * (d.x > xMid ? -1 : 1)); // Adjust x position based on the point's position relative to xMid
}
function dragEnded(event, d) {
isDragging = false;
d3.select(this)
.select("circle")
.attr("r", width / 100);
// If task is dragged to the end (+/- 5), mark it as completed
if (d.x >= xMax - 5) {
d.x = xMax - 20;
d.y = hillFn(d.x);
toggleTaskCompletion(d.id);
}
saveTasks();
}
// Toggle task completion
function toggleTaskCompletion(taskId) {
const task = tasks.find((t) => t.id === taskId);
task.completed = !task.completed;
saveTasks();
if (task.completed) {
svg.selectAll(".task")
.filter((d) => d.id === taskId)
.remove();
} else {
renderTasksOnChart();
}
}
// Remove a task
function removeTask(taskId) {
tasks = tasks.filter((t) => t.id !== taskId);
saveTasks();
svg.selectAll(".task")
.filter((d) => d.id === taskId)
.remove();
}
// Render completed tasks point
function renderCompletedTasksPoint() {
const completedCount = tasks.filter((task) => task.completed).length;
if (completedTasksPoint) {
completedTasksPoint.remove();
}
if (completedCount === 0) {
return;
}
completedTasksPoint = svg.append("g").attr("transform", `translate(${xMax}, ${hillFn(xMax)})`);
completedTasksPoint.append("circle").attr("r", 15).attr("fill", "#c2c2c2");
completedTasksPoint
.append("text")
.attr("class", "task-label")
.attr("x", 0)
.attr("y", 5)
.attr("text-anchor", "middle")
.attr("font-weight", "bold")
.text(completedCount);
}
// Add a new task
function addNewTask() {
const newId = tasks.length ? Math.max(...tasks.map((t) => t.id)) + 1 : 1;
const newTask = {
id: newId,
label: `Task ${newId}`,
x: xMin,
color: colorPalette[(newId - 1) % colorPalette.length],
y: hillFn(xMin),
completed: false,
};
tasks.push(newTask);
saveTasks();
renderTasksOnChart();
renderTaskList();
}
// Export chart as PNG
function exportChartAsPNG() {
const svgNode = document.getElementById("chart");
const serializer = new XMLSerializer();
const svgString = serializer.serializeToString(svgNode);
const image = new Image();
// Include styles for correct rendering
const style = `
<style>
.hill-path {
fill: none; /* No fill */
stroke: black; /* Stroke color */
stroke-width: 2; /* Stroke width */
}
text {
font-family: sans-serif;
font-size: 12px;
}
</style>
`;
// Insert styles directly into the SVG source
const svgWithStyles = svgString.replace(/<\/svg>/, `${style}</svg>`);
image.src = "data:image/svg+xml;base64," + window.btoa(unescape(encodeURIComponent(svgWithStyles)));
image.onload = function () {
const canvas = document.createElement("canvas");
const scaleFactor = 4;
canvas.width = svgNode.clientWidth * scaleFactor;
canvas.height = svgNode.clientHeight * scaleFactor;
const context = canvas.getContext("2d");
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
context.imageSmoothingEnabled = true; // Enable image smoothing
context.setTransform(scaleFactor, 0, 0, scaleFactor, 0, 0); // Use setTransform for scaling
context.drawImage(image, 0, 0);
const png = canvas.toDataURL("image/png");
const a = document.createElement("a");
a.href = png;
a.download = "hillchart.png";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
}
// Event Listeners
document.getElementById("addTask").addEventListener("click", addNewTask);
document.getElementById("exportPNG").addEventListener("click", exportChartAsPNG);
document.getElementById("deleteAll").addEventListener("click", resetPage);
document.getElementById("print").addEventListener("click", () => {
renderTasksOnChart();
renderTaskList();
window.print();
});
chartTitleElement.addEventListener("input", () => {
updateChartTitle();
loadChartTitle();
});
// Initial rendering
initHillChart();
// Load tasks when the page loads
document.addEventListener("DOMContentLoaded", () => {
loadTasks();
renderTasksOnChart();
loadChartTitle();
renderTaskList();
});
</script>
</body>
</html>