const {
createCanvas,
loadImage
} = require('canvas');
const fs = require('fs');
class Gauge {
constructor({
name,
description,
type = "gauge",
unit,
range,
value,
step,
precision,
ticks = 5, // default to 5 ticks
brackets
}) {
this.name = name;
this.description = description;
this.type = type;
this.unit = unit;
this.range = range;
this.value = value;
this.step = step;
this.precision = precision;
this.ticks = ticks;
this.brackets = brackets;
}
setValue(newValue) {
const [min, max] = this.range;
if (newValue < min) this.value = min;
else if (newValue > max) this.value = max;
else this.value = parseFloat(newValue.toFixed(this.precision));
}
getCurrentBracket() {
return this.brackets.find(
b => this.value >= b.min && this.value <= b.max
);
}
toString() {
const bracket = this.getCurrentBracket();
const label = bracket ? bracket.label : "Unknown";
return `${this.name}: ${this.value}${this.unit} (${label})`;
}
generateImage(filePath = 'gauge.png', width = 400, height = 300) {
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const centerX = width / 2;
const centerY = height * 0.8; // Lower center for gauge arc
const radius = Math.min(width, height) * 0.6;
const startAngle = Math.PI * 0.85; // Start at -135 degrees
const endAngle = Math.PI * 0.15; // End at 45 degrees (270 degree sweep)
// Draw background
ctx.fillStyle = '#f0f0f0';
ctx.fillRect(0, 0, width, height);
// Draw brackets
this.brackets.forEach(bracket => {
const bracketStartValue = bracket.min;
const bracketEndValue = bracket.max;
const startAngleBracket = startAngle + (bracketStartValue - this.range[0]) / (this.range[1] - this.range[0]) * (endAngle - startAngle);
const endAngleBracket = startAngle + (bracketEndValue - this.range[0]) / (this.range[1] - this.range[0]) * (endAngle - startAngle);
ctx.beginPath();
ctx.arc(centerX, centerY, radius, startAngleBracket, endAngleBracket, false);
ctx.arc(centerX, centerY, radius * 0.8, endAngleBracket, startAngleBracket, true);
ctx.closePath();
ctx.fillStyle = bracket.color;
ctx.fill();
});
// Draw gauge arc (base)
ctx.beginPath();
ctx.arc(centerX, centerY, radius, startAngle, endAngle, false);
ctx.lineWidth = 5;
ctx.strokeStyle = '#333';
ctx.stroke();
// Draw ticks
const angleIncrement = (endAngle - startAngle) / (this.ticks - 1); // Distribute ticks evenly
for (let i = 0; i < this.ticks; i++) {
const angle = startAngle + i * angleIncrement;
const x1 = centerX + radius * Math.cos(angle);
const y1 = centerY + radius * Math.sin(angle);
const x2 = centerX + (radius * 0.9) * Math.cos(angle); // Slightly shorter ticks
const y2 = centerY + (radius * 0.9) * Math.sin(angle);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = '#333';
ctx.lineWidth = 2;
ctx.stroke();
// Add tick labels (optional)
const tickValue = this.range[0] + (this.range[1] - this.range[0]) * (i / (this.ticks - 1));
const tickLabelX = centerX + (radius * 1.1) * Math.cos(angle);
const tickLabelY = centerY + (radius * 1.1) * Math.sin(angle) + 5; // Adjust for text height
ctx.font = '12px Arial';
ctx.fillStyle = '#333';
ctx.textAlign = 'center';
ctx.fillText(tickValue.toFixed(0), tickLabelX, tickLabelY);
}
// Draw needle
const valueAngle = startAngle + (this.value - this.range[0]) / (this.range[1] - this.range[0]) * (endAngle - startAngle);
const needleX = centerX + (radius * 0.7) * Math.cos(valueAngle);
const needleY = centerY + (radius * 0.7) * Math.sin(valueAngle);
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.lineTo(needleX, needleY);
ctx.strokeStyle = 'red';
ctx.lineWidth = 3;
ctx.stroke();
// Draw center circle
ctx.beginPath();
ctx.arc(centerX, centerY, 5, 0, 2 * Math.PI);
ctx.fillStyle = 'red';
ctx.fill();
// Add name and value text
ctx.font = '16px Arial';
ctx.fillStyle = '#000';
ctx.textAlign = 'center';
ctx.fillText(this.name, centerX, height * 0.2);
ctx.font = 'bold 20px Arial';
ctx.fillText(`${this.value}${this.unit}`, centerX, height * 0.35);
// Save the image
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync(filePath, buffer);
console.log(`Gauge image saved to ${filePath}`);
}
}
// Example usage:
const myGauge = new Gauge({
name: "Temperature",
description: "Room Temperature Monitor",
unit: "°C",
range: [0, 100],
step: 0.5,
precision: 1,
value: 37.2,
ticks: 6,
brackets: [{
min: 0,
max: 25,
color: "lightblue",
label: "Cold"
},
{
min: 26,
max: 50,
color: "yellow",
label: "Warm"
},
{
min: 51,
max: 75,
color: "orange",
label: "Hot"
},
{
min: 76,
max: 100,
color: "red",
label: "Very Hot"
}
]
});
console.log(myGauge.toString());
myGauge.generateImage(); // Saves gauge.png
module.exports = Gauge;