@@ -23,13 +23,14 @@ function fetchDataAndRenderCharts() {
2323 const dashboardMemoryUsageData = data . dashboard_memory_usage ;
2424 const cpuFrequencyData = data . cpu_frequency ;
2525 const currentTempData = data . current_temp ;
26+
27+ // current time from backend to display
2628 const currentTime = data . current_time ;
2729 const timeZoneName = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ;
30+ displayTimeAndTimeZone ( currentTime , timeZoneName ) ;
2831
2932 // Format the time data using the currentTime from backend
30- const timeData = data . time . map ( time => formatDate ( time , currentTime ) ) ;
31-
32- displayTimeAndTimeZone ( currentTime , timeZoneName ) ;
33+ const timeData = data . time . map ( time => formatDate ( time , timeZoneName ) ) ; // Use timeZoneName from displayTimeAndTimeZone function
3334
3435 createCharts ( cpuData , timeData , memoryData , batteryData , networkSentData , networkReceivedData , dashboardMemoryUsageData , cpuFrequencyData , currentTempData ) ;
3536 } )
@@ -82,52 +83,25 @@ document.getElementById('refreshCurrentTempTime').addEventListener('click', () =
8283 fetchDataAndRenderCharts ( ) ;
8384} ) ;
8485
85-
86- function formatDate ( dateString , currentTime ) {
87- const date = new Date ( dateString ) ;
88- const now = new Date ( currentTime ) ; // Use currentTime from backend
89-
90- // Helper function to format with leading zeros
91- const pad = ( num ) => String ( num ) . padStart ( 2 , '0' ) ;
92-
93- // Manually extract UTC components
94- const day = pad ( date . getUTCDate ( ) ) ; // e.g., 09
95- const month = pad ( date . getUTCMonth ( ) + 1 ) ; // e.g., 04
96- const year = date . getUTCFullYear ( ) ; // e.g., 2021
97- const hours = pad ( date . getUTCHours ( ) ) ; // e.g., 11
98- const minutes = pad ( date . getUTCMinutes ( ) ) ; // e.g., 33
99-
100-
101- // Calculate time differences
102- const diffDays = Math . floor ( ( now - date ) / ( 1000 * 60 * 60 * 24 ) ) ;
103- const diffWeeks = Math . floor ( diffDays / 7 ) ;
104- const diffMonths = now . getMonth ( ) - date . getUTCMonth ( ) + ( 12 * ( now . getFullYear ( ) - date . getUTCFullYear ( ) ) ) ;
105- const diffYears = now . getFullYear ( ) - date . getUTCFullYear ( ) ;
106-
107- // Determine the label based on time differences
108- // // Reset the time to 12am for the date comparison
109- // date.setUTCHours(0, 0, 0, 0);
110- // now.setUTCHours(0, 0, 0, 0);
111-
112- // if (diffDays === 0) {
113- // return `Today ${hours}:${minutes}`;
114- // } else if (diffDays === 1) {
115- // return `Yesterday ${hours}:${minutes}`;
116- // } else if (diffDays <= 3) {
117- // return `${diffDays} Days Ago ${hours}:${minutes}`;
118- // } else if (diffDays <= 7) {
119- // return `${Math.ceil(diffDays / 7)} Week${diffDays > 7 ? 's' : ''} Ago ${hours}:${minutes}`;
120- // } else if (diffDays <= 30) {
121- // return `${Math.ceil(diffDays / 7)} Weeks Ago ${hours}:${minutes}`;
122- // } else if (diffMonths < 12) {
123- // return `${diffMonths} Month${diffMonths > 1 ? 's' : ''} Ago ${hours}:${minutes}`;
124- // } else if (diffYears < 2) {
125- // return `Last Year ${hours}:${minutes}`;
126- // } else {
127- // return `${year}/${month}/${day} ${hours}:${minutes}`;
128- // }
129-
130- return `${ year } /${ month } /${ day } ${ hours } :${ minutes } ` ;
86+ function formatDate ( utcTime , timeZone ) {
87+ const date = new Date ( utcTime ) ;
88+
89+ // Format options can be adjusted for your needs
90+ const options = {
91+ timeZone : timeZone ,
92+ year : 'numeric' ,
93+ month : '2-digit' ,
94+ day : '2-digit' ,
95+ hour : '2-digit' ,
96+ minute : '2-digit' ,
97+ hour12 : false // Change to true if you prefer 12-hour format
98+ } ;
99+
100+ // Generate formatted string
101+ const formattedDate = date . toLocaleString ( 'en-US' , options ) ;
102+
103+ // For better graph display, you might want just the date and hour
104+ return formattedDate . replace ( / , ( \d { 2 } : \d { 2 } ) / , ' $1' ) ; // Example: "09/22/2024 14:30"
131105}
132106
133107function displayTimeAndTimeZone ( currentTime , timeZoneName ) {
@@ -155,13 +129,27 @@ function createChart(ctx, labels, datasets, yLabel) {
155129 ctx . chart . destroy ( ) ; // Destroy the existing chart if it exists
156130 }
157131
132+ const allDataPoints = datasets . flatMap ( dataset => dataset . data ) ;
133+ const minY = Math . min ( ...allDataPoints . filter ( value => typeof value === 'number' ) ) ;
134+ const maxY = Math . max ( ...allDataPoints . filter ( value => typeof value === 'number' ) ) ;
135+
158136 ctx . chart = new Chart ( ctx , {
159137 type : 'line' ,
160138 data : {
161- labels : labels , // Use your timeData directly as labels
162- datasets : datasets
139+ labels : labels ,
140+ datasets : datasets . map ( dataset => ( {
141+ ...dataset ,
142+ borderWidth : 2 ,
143+ fill : false ,
144+ tension : 0.3 ,
145+ pointRadius : 5 ,
146+ pointHoverRadius : 7 ,
147+ backgroundColor : dataset . backgroundColor || 'rgba(75, 192, 192, 0.2)' ,
148+ borderColor : dataset . borderColor || 'rgba(75, 192, 192, 1)' ,
149+ } ) ) ,
163150 } ,
164151 options : {
152+
165153 scales : {
166154 x : {
167155 type : 'category' ,
@@ -171,17 +159,17 @@ function createChart(ctx, labels, datasets, yLabel) {
171159 } ,
172160 ticks : {
173161 autoSkip : true ,
174- maxTicksLimit : 10 ,
175- maxRotation : 20 ,
162+ maxTicksLimit : 6 ,
163+ maxRotation : 0 ,
176164 minRotation : 0 ,
177165 }
178166 } ,
179167 y : {
180- beginAtZero : true ,
168+ beginAtZero : minY < 0 ? false : true , // Adjust y-axis based on data
181169 title : {
182170 display : true ,
183- text : yLabel
184- }
171+ text : yLabel ,
172+ } ,
185173 }
186174 }
187175 }
0 commit comments