diff --git a/src/roast.ts b/src/roast.ts index c245219..d72668d 100644 --- a/src/roast.ts +++ b/src/roast.ts @@ -1,4 +1,4 @@ - +import { loadGraph } from './tempgraph.ts'; let timer = 0; let isRunning = false; @@ -164,6 +164,7 @@ function exportCSV(): void { function showGraph(): void { const graphContainer = document.getElementById("graph-container"); if (graphContainer) { + loadGraph(null); graphContainer.classList.remove("hidden"); graphContainer.classList.add("graph-display-flex"); } diff --git a/src/tempgraph.ts b/src/tempgraph.ts index 0c2e0c1..de82b1b 100644 --- a/src/tempgraph.ts +++ b/src/tempgraph.ts @@ -1,12 +1,10 @@ import { Chart, registerables } from 'chart.js'; import annotationPlugin from 'chartjs-plugin-annotation'; -import {AnnotationOptions} from 'chartjs-plugin-annotation'; +import { AnnotationOptions } from 'chartjs-plugin-annotation'; -// Register all Chart.js components -Chart.register(...registerables); -Chart.register(annotationPlugin); - -const sampleData = `Time,Event,Temperature +/* Test data if nothing is provided */ +function getTestData(): string[] { + const sampleData = `Time,Event,Temperature 0:00,Charge, 0:00,Temp,139 0:03,Temp,90 @@ -18,90 +16,105 @@ const sampleData = `Time,Event,Temperature 0:24,First Crack, 0:30,Temp,208 0:31,Drop,`; + return sampleData.split('\n'); +} + +// Register all Chart.js components +Chart.register(...registerables); +Chart.register(annotationPlugin); -const lines = sampleData.split('\n'); -const data = lines.slice(1).map(line => { - const [time, event, temp] = line.split(','); - return { time, event, temp }; -}); function parseTime(time: string): number { const [mins, secs] = time.split(':').map(Number); return mins * 60 + secs; } -const tempData = data - .filter(entry => entry.event === 'Temp') - .map(entry => { - const timeInSeconds = parseTime(entry.time); - const temp = parseFloat(entry.temp); - return { x: timeInSeconds, y: temp }; +export function loadGraph(dataArray?: string[]): void { + const lines = dataArray || getTestData(); + + // Further code using `lines`... + const data = lines.slice(1).map(line => { + const [time, event, temp] = line.split(','); + return { time, event, temp }; }); -const ctx = document.getElementById('myChart') as HTMLCanvasElement; -ctx.width = 800; -ctx.height = 400; + const tempData = data + .filter(entry => entry.event === 'Temp') + .map(entry => { + const timeInSeconds = parseTime(entry.time); + const temp = parseFloat(entry.temp); + return { x: timeInSeconds, y: temp }; + }); -const annotationsArray: AnnotationOptions<'line'>[] = data - .filter(entry => entry.event !== 'Temp') - .map(entry => { - const timeInSeconds = parseTime(entry.time); - return { - type: 'line', - mode: 'vertical', - scaleID: 'x', - value: timeInSeconds, - borderColor: 'red', - borderWidth: 2, - label: { - content: entry.event, - enabled: true, - position: 'center', // Use a valid predefined position - yAdjust: -10 - } - }; - }); + const ctx = document.getElementById('myChart') as HTMLCanvasElement; + ctx.width = 800; + ctx.height = 400; -// Your Chart.js configuration remains the same: -new Chart(ctx, { - type: 'line', - data: { - datasets: [{ - label: 'Temperature (°C)', - data: tempData, - borderColor: 'blue', - fill: false, - pointRadius: 5, - pointBackgroundColor: 'blue' - }] - }, - options: { - responsive: true, - scales: { - x: { - type: 'linear', - position: 'bottom', - ticks: { - callback: function(value) { - const mins = Math.floor(Number(value) / 60); - const secs = Number(value) % 60; - return `${mins}:${secs.toString().padStart(2, '0')}`; - } + const annotationsArray: AnnotationOptions<'line'>[] = data + .filter(entry => entry.event !== 'Temp') + .map(entry => { + const timeInSeconds = parseTime(entry.time); + return { + type: 'line', + mode: 'vertical', + scaleID: 'x', + value: timeInSeconds, + borderColor: 'red', + borderWidth: 2, + label: { + content: entry.event, + enabled: true, + position: 'center', // Use a valid predefined position + yAdjust: -10 + } + }; + }); + + // Clear previous chart instance if exists + const oldChart = (ctx as any).chart; + if (oldChart) { + oldChart.destroy(); + } + new Chart(ctx, { + type: 'line', + data: { + datasets: [{ + label: 'Temperature (°C)', + data: tempData, + borderColor: 'blue', + fill: false, + pointRadius: 5, + pointBackgroundColor: 'blue' + }] + }, + options: { + responsive: true, + scales: { + x: { + type: 'linear', + position: 'bottom', + ticks: { + callback: function (value) { + const mins = Math.floor(Number(value) / 60); + const secs = Number(value) % 60; + return `${mins}:${secs.toString().padStart(2, '0')}`; + } + }, + min: 0, + max: 31 }, - min: 0, - max: 31 + y: { + title: { + display: true, + text: 'Temperature (°C)' + } + } }, - y: { - title: { - display: true, - text: 'Temperature (°C)' + plugins: { + annotation: { + annotations: annotationsArray } } - }, - plugins: { // Correctly nested as per the previous answer - annotation: { - annotations: annotationsArray - } } - } -}); + }); +}