Skip to content

Commit 3d7c843

Browse files
Fix Graph Resizing
- Enabled window resizing (no constraints yet) - Graphs will hide until the user selects a frame size and then it will populate
1 parent cceab8c commit 3d7c843

2 files changed

Lines changed: 108 additions & 100 deletions

File tree

src/graphs.js

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
async function getLast12Months() {
24
var currentDate = new Date();
35
var last12Months = [];
@@ -55,62 +57,59 @@ async function convertToMonthLabels(last12Months) {
5557

5658

5759

58-
let annualBarGraph; // Declare the chart variable outside the function
60+
let annualBarGraph;
61+
const annualGraph_el = document.getElementById('monthlyLineGraph');
62+
const ctxAnnual = annualGraph_el.getContext('2d');
63+
5964

6065
async function plotAnnualBarGraph(last12Months, last12MonthHours) {
61-
const currentMonth = getCurrentMonth();
62-
if (!annualBarGraph) {
63-
// If the chart doesn't exist, create a new one
64-
annualBarGraph = new Chart("annualBarGraph", {
65-
type: "bar",
66-
data: {
66+
const currentMonth = getCurrentMonth();
67+
// Create a new Chart object for the annual bar graph
68+
return new Chart("annualBarGraph", {
69+
type: "bar",
70+
data: {
6771
labels: last12Months,
6872
datasets: [{
69-
backgroundColor: last12Months.map(month => (month === currentMonth) ? '#F1F1F1' : '#9593D9'),
70-
data: last12MonthHours,
73+
backgroundColor: last12Months.map(month => (month === currentMonth) ? '#F1F1F1' : '#9593D9'),
74+
data: last12MonthHours,
7175
}]
72-
},
73-
options: {
76+
},
77+
options: {
7478
legend: { display: false },
7579
responsive: true, // Make the chart responsive
7680
maintainAspectRatio: false, // Allow the aspect ratio to change
7781
scales: {
78-
yAxes: [{
79-
gridLines: {
80-
display: false,
81-
},
82-
ticks: {
83-
beginAtZero: true,
84-
display: true,
85-
fontColor: '#9593D9',
86-
stepSize: 1,
87-
}
88-
}],
89-
xAxes: [{
90-
gridLines: {
91-
display: false
92-
},
93-
ticks: {
94-
display: true,
95-
fontColor: '#9593D9',
96-
callback: function (value, index, values) {
97-
return value.substring(0, 3);
98-
},
99-
maxRotation: 0,
100-
},
101-
barPercentage: 0.8,
102-
}]
82+
yAxes: [{
83+
gridLines: {
84+
display: false,
85+
},
86+
ticks: {
87+
beginAtZero: true,
88+
display: true,
89+
fontColor: '#9593D9',
90+
stepSize: 1,
91+
}
92+
}],
93+
xAxes: [{
94+
gridLines: {
95+
display: false
96+
},
97+
ticks: {
98+
display: true,
99+
fontColor: '#9593D9',
100+
callback: function (value, index, values) {
101+
return value.substring(0, 3);
102+
},
103+
maxRotation: 0,
104+
},
105+
barPercentage: 0.8,
106+
}]
103107
}
104-
}
105-
});
106-
} else {
107-
// If the chart exists, update its data
108-
annualBarGraph.data.labels = last12Months;
109-
annualBarGraph.data.datasets[0].data = last12MonthHours;
110-
annualBarGraph.update(); // Update the chart
111-
}
108+
}
109+
});
112110
}
113111

112+
114113
async function grabCurrentDate(){
115114
const currentDate = new Date();
116115
var date = currentDate.toLocaleDateString('en-US', { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone });
@@ -121,59 +120,54 @@ async function grabCurrentDate(){
121120
return date;
122121
}
123122

124-
123+
const monthlyLineGraph_el = document.getElementById('monthlyLineGraph');
124+
const ctxLineGraph = monthlyLineGraph_el.getContext('2d');
125125
let monthlyLineGraph; // Declare the chart variable outside the function
126126

127127
async function plotMonthlyLineGraph(last30Days, last30DayCompletedTasks) {
128-
const today = await grabCurrentDate();
129-
if (!monthlyLineGraph) {
130-
// If the chart doesn't exist, create a new one
131-
monthlyLineGraph = new Chart("monthlyLineGraph", {
132-
type: "bar",
133-
data: {
128+
const today = await grabCurrentDate();
129+
// Create a new Chart object for the monthly line graph
130+
return new Chart("monthlyLineGraph", {
131+
type: "bar",
132+
data: {
134133
labels: last30Days,
135134
datasets: [{
136-
backgroundColor: last30Days.map(date => (date === today) ? '#F1F1F1' : '#9593D9'),
137-
data: last30DayCompletedTasks,
135+
backgroundColor: last30Days.map(date => (date === today) ? '#F1F1F1' : '#9593D9'),
136+
data: last30DayCompletedTasks,
138137
}]
139-
},
140-
options: {
138+
},
139+
options: {
141140
legend: { display: false },
142141
responsive: true, // Make the chart responsive
143142
maintainAspectRatio: false, // Allow the aspect ratio to change
144143
scales: {
145-
yAxes: [{
146-
gridLines: {
147-
display: false,
148-
},
149-
ticks: {
150-
beginAtZero: true,
151-
display: true,
152-
fontColor: '#9593D9',
153-
stepSize: 1,
154-
}
155-
}],
156-
xAxes: [{
157-
gridLines: {
158-
display: false
159-
},
160-
ticks: {
161-
display: false
162-
}
163-
}]
144+
yAxes: [{
145+
gridLines: {
146+
display: false,
147+
},
148+
ticks: {
149+
beginAtZero: true,
150+
display: true,
151+
fontColor: '#9593D9',
152+
stepSize: 1,
153+
}
154+
}],
155+
xAxes: [{
156+
gridLines: {
157+
display: false
158+
},
159+
ticks: {
160+
display: false
161+
}
162+
}]
164163
}
165-
}
166-
});
167-
} else {
168-
// If the chart exists, update its data
169-
monthlyLineGraph.data.labels = last30DayCompletedTasks;
170-
monthlyLineGraph.data.datasets[0].data = last30DayCompletedTasks;
171-
monthlyLineGraph.update(); // Update the chart
172-
}
164+
}
165+
});
173166
}
174167

175168

176169

170+
177171
async function getLast30Days() {
178172
var currentDate = new Date();
179173
var last30Days = [];
@@ -199,32 +193,48 @@ async function getLast30Days() {
199193
}
200194
return last30Days; // Reverse the array to get dates in descending order
201195
}
202-
203-
204-
205-
206196

197+
document.addEventListener('DOMContentLoaded', async () => {
198+
annualBarGraph = await populateAnnualBarGraph();
199+
monthlyLineGraph = await populateMonthlyLineGraph();
200+
});
207201

202+
async function populateAnnualBarGraph(){
203+
var last12Months = await getLast12Months();
204+
var last12MonthsTaskCount = await api.graphCounts({request: 'MonthlyCompleteTaskCount', months: last12Months});
205+
var monthLabels = await convertToMonthLabels(last12Months);
206+
last12MonthsTaskCount.reverse();
207+
monthLabels.reverse();
208+
return await plotAnnualBarGraph(monthLabels, last12MonthsTaskCount);
209+
}
208210

211+
async function populateMonthlyLineGraph(){
212+
var last30Days = await getLast30Days();
213+
var last30DaysTaskCount = await api.graphCounts({request: 'DailyCompleteTaskCount', days: last30Days});
214+
return await plotMonthlyLineGraph(last30Days, last30DaysTaskCount);
215+
}
209216

217+
let resizeTimer;
210218

211-
212-
document.addEventListener('DOMContentLoaded', async () => {
213-
var last12Months = await getLast12Months();
214-
var last12MonthsTaskCount = await api.graphCounts({request: 'MonthlyCompleteTaskCount', months: last12Months});
215-
var monthLabels = await convertToMonthLabels(last12Months);
216-
last12MonthsTaskCount.reverse();
217-
monthLabels.reverse();
218-
await plotAnnualBarGraph(monthLabels, last12MonthsTaskCount);
219-
220-
var last30Days = await getLast30Days();
221-
var last30DaysTaskCount = await api.graphCounts({request: 'DailyCompleteTaskCount', days: last30Days});
222-
await plotMonthlyLineGraph(last30Days, last30DaysTaskCount);
219+
window.addEventListener('resize', () => {
220+
clearTimeout(resizeTimer);
221+
console.log(annualBarGraph);
222+
if (annualBarGraph){
223+
annualBarGraph.destroy();
224+
}
225+
if (monthlyLineGraph){
226+
monthlyLineGraph.destroy();
227+
}
228+
resizeTimer = setTimeout(async () => {
229+
annualBarGraph = await populateAnnualBarGraph();
230+
monthlyLineGraph = await populateMonthlyLineGraph();
231+
}, 500);
223232
});
224233

225234

226235

227236

237+
228238
/*
229239
async function measureExecutionTime(callback) {
230240
const startTime = performance.now();

src/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ const createWindow = () => {
6363
width: windowWidth,
6464
height: windowHeight,
6565
frame: false,
66-
resizable: false, // Graphs break resizing...need to fix graphs
67-
maximizable: false,
6866
webPreferences: {
6967
preload: path.join(__dirname, 'preload.js'),
7068
},

0 commit comments

Comments
 (0)