fix graph visuals
This commit is contained in:
@@ -2,6 +2,13 @@ import { Chart, registerables } from 'chart.js';
|
|||||||
import annotationPlugin from 'chartjs-plugin-annotation';
|
import annotationPlugin from 'chartjs-plugin-annotation';
|
||||||
import { AnnotationOptions } from 'chartjs-plugin-annotation';
|
import { AnnotationOptions } from 'chartjs-plugin-annotation';
|
||||||
|
|
||||||
|
// Register Chart.js components and plugins
|
||||||
|
Chart.register(...registerables);
|
||||||
|
Chart.register(annotationPlugin);
|
||||||
|
|
||||||
|
// External chart instance tracker
|
||||||
|
let chartInstance: Chart | null = null;
|
||||||
|
|
||||||
/* Test data if nothing is provided */
|
/* Test data if nothing is provided */
|
||||||
function getTestData(): string[] {
|
function getTestData(): string[] {
|
||||||
const sampleData = `Time,Event,Temperature
|
const sampleData = `Time,Event,Temperature
|
||||||
@@ -19,11 +26,6 @@ function getTestData(): string[] {
|
|||||||
return sampleData.split('\n');
|
return sampleData.split('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register all Chart.js components
|
|
||||||
Chart.register(...registerables);
|
|
||||||
Chart.register(annotationPlugin);
|
|
||||||
|
|
||||||
|
|
||||||
function parseTime(time: string): number {
|
function parseTime(time: string): number {
|
||||||
const [mins, secs] = time.split(':').map(Number);
|
const [mins, secs] = time.split(':').map(Number);
|
||||||
return mins * 60 + secs;
|
return mins * 60 + secs;
|
||||||
@@ -32,7 +34,6 @@ function parseTime(time: string): number {
|
|||||||
export function loadGraph(dataArray?: string[]): void {
|
export function loadGraph(dataArray?: string[]): void {
|
||||||
const lines = dataArray || getTestData();
|
const lines = dataArray || getTestData();
|
||||||
|
|
||||||
// Further code using `lines`...
|
|
||||||
const data = lines.slice(1).map(line => {
|
const data = lines.slice(1).map(line => {
|
||||||
const [time, event, temp] = line.split(',');
|
const [time, event, temp] = line.split(',');
|
||||||
return { time, event, temp };
|
return { time, event, temp };
|
||||||
@@ -63,19 +64,23 @@ export function loadGraph(dataArray?: string[]): void {
|
|||||||
borderWidth: 2,
|
borderWidth: 2,
|
||||||
label: {
|
label: {
|
||||||
content: entry.event,
|
content: entry.event,
|
||||||
enabled: true,
|
display: true,
|
||||||
position: 'center', // Use a valid predefined position
|
rotation: -90, // rotates text counter-clockwise
|
||||||
yAdjust: -10
|
position: 'end', // anchors label near bottom of chart
|
||||||
|
yAdjust: -10, // nudges it downward (positive = down)
|
||||||
|
font: {
|
||||||
|
size: 10
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clear previous chart instance if exists
|
// Clean up previous chart if it exists
|
||||||
const oldChart = (ctx as any).chart;
|
if (chartInstance) {
|
||||||
if (oldChart) {
|
chartInstance.destroy();
|
||||||
oldChart.destroy();
|
|
||||||
}
|
}
|
||||||
new Chart(ctx, {
|
|
||||||
|
chartInstance = new Chart(ctx, {
|
||||||
type: 'line',
|
type: 'line',
|
||||||
data: {
|
data: {
|
||||||
datasets: [{
|
datasets: [{
|
||||||
@@ -88,6 +93,9 @@ export function loadGraph(dataArray?: string[]): void {
|
|||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
|
animation: {
|
||||||
|
duration: 0
|
||||||
|
},
|
||||||
responsive: true,
|
responsive: true,
|
||||||
scales: {
|
scales: {
|
||||||
x: {
|
x: {
|
||||||
|
|||||||
Reference in New Issue
Block a user