To generate a visual representation of the gauge, we can use the node-canvas library, which provides a Canvas API similar to the one in browsers. This allows us to draw arcs, ticks, labels, and color-coded brackets.
Here’s how we can enhance the Gauge class:
- Add a drawGaugeImage method that creates a 2D canvas image.
- Use the brackets to color segments of the arc.
- Automatically place ticks and labels.
- Save the image to a file or return it as a buffer.
📦 First, install the required dependency:
npm install canvas
🧠 Updated Gauge class with drawGaugeImage:
const { createCanvas } = require('canvas');
const fs = require('fs');
class Gauge {
constructor({
name,
description,
type = "gauge",
unit,
range,
value,
step,
precision,
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});
}
drawGaugeImage(filename = 'gauge.png', width = 400, height = 400) {
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const centerX = width / 2;
const centerY = height / 2;
const radius = Math.min(width, height) * 0.4;
const startAngle = Math.PI * 0.75; // 135 degrees
const endAngle = Math.PI * 0.25; // 45 degrees
const totalAngle = 1.5 * Math.PI; // 270 degrees
const [min, max] = this.range;
// Draw brackets as colored arcs
for (const bracket of this.brackets) {
const start = (bracket.min - min) / (max - min);
const end = (bracket.max - min) / (max - min);
const angleStart = startAngle + start * totalAngle;
const angleEnd = startAngle + end * totalAngle;
ctx.beginPath();
ctx.strokeStyle = bracket.color;
ctx.lineWidth = 20;
ctx.arc(centerX, centerY, radius, angleStart, angleEnd, false);
ctx.stroke();
}
// Draw ticks
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
ctx.fillStyle = '#000';
const tickCount = this.ticks;
for (let i = 0; i <= tickCount; i++) {
const value = min + (i / tickCount) * (max - min);
const angle = startAngle + (i / tickCount) * totalAngle;
const x1 = centerX + Math.cos(angle) * (radius - 10);
const y1 = centerY + Math.sin(angle) * (radius - 10);
const x2 = centerX + Math.cos(angle) * (radius + 10);
const y2 = centerY + Math.sin(angle) * (radius + 10);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
// Draw tick labels
const labelX = centerX + Math.cos(angle) * (radius + 25);
const labelY = centerY + Math.sin(angle) * (radius + 25);
ctx.font = '12px sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(value.toFixed(this.precision), labelX, labelY);
}
// Draw needle
const valueRatio = (this.value - min) / (max - min);
const needleAngle = startAngle + valueRatio * totalAngle;
const needleX = centerX + Math.cos(needleAngle) * (radius - 30);
const needleY = centerY + Math.sin(needleAngle) * (radius - 30);
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.lineTo(needleX, needleY);
ctx.strokeStyle = 'black';
ctx.lineWidth = 3;
ctx.stroke();
// Draw center circle
ctx.beginPath();
ctx.arc(centerX, centerY, 5, 0, 2 * Math.PI);
ctx.fillStyle = 'black';
ctx.fill();
// Draw label
ctx.font = '16px sans-serif';
ctx.textAlign = 'center';
ctx.fillText(`${this.name}: ${this.value}${this.unit}`, centerX, centerY + radius + 40);
// Save to file
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync(filename, buffer);
console.log(`Gauge image saved to ${filename}`);