Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add targetLine #398

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions justgage.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@
dataset,
">"
),
// targetLine : float
// value for the target line (optional)
targetLine: kvLookup("targetLine", config, dataset, null, "float"),

// targetLineColor : string
// color of the target line
targetLineColor: kvLookup("targetLineColor", config, dataset, "#000000"),

// targetLineWidth : float
// width of the target line
targetLineWidth: kvLookup("targetLineWidth", config, dataset, 1.5),

// donutStartAngle : int
// angle to start from when in donut mode
Expand Down Expand Up @@ -765,6 +776,9 @@
],
});

// Draw the Target Line
obj.drawTargetLine();

if (obj.config.donut) {
obj.level.transform(
"r" +
Expand Down Expand Up @@ -1376,6 +1390,62 @@
}
};

JustGage.prototype.drawTargetLine = function() {
const obj = this;

if (obj.config.targetLine === null) {
return;
}

let path;
const w = obj.params.widgetW;
const h = obj.params.widgetH;
const dx = obj.params.dx;
const dy = obj.params.dy;
const gws = obj.config.gaugeWidthScale;
const donut = obj.config.donut;

let alpha = (1 - (obj.config.targetLine - obj.config.min) / (obj.config.max - obj.config.min)) * Math.PI;
let Ro = w / 2 - w / 10;
let Ri = Ro - w / 6.666666666666667 * gws;

let Cx, Cy, Xo, Yo, Xi, Yi;

if (donut) {
Ro = w / 2 - w / 30;
Ri = Ro - w / 6.666666666666667 * gws;

Cx = w / 2 + dx;
Cy = h / 2 + dy;

Xo = Cx + Ro * Math.cos(alpha);
Yo = Cy - Ro * Math.sin(alpha);
Xi = Cx + Ri * Math.cos(alpha);
Yi = Cy - Ri * Math.sin(alpha);

path = "M" + Xi + "," + Yi + " L" + Xo + "," + Yo;
} else {
Cx = w / 2 + dx;
Cy = h / 1.25 + dy;

Xo = Cx + Ro * Math.cos(alpha);
Yo = Cy - Ro * Math.sin(alpha);
Xi = Cx + Ri * Math.cos(alpha);
Yi = Cy - Ri * Math.sin(alpha);

path = "M" + Xi + "," + Yi + " L" + Xo + "," + Yo;
}

obj.targetLine = obj.canvas.path(path).attr({
"stroke": obj.config.targetLineColor,
"stroke-width": obj.config.targetLineWidth
});

if (donut) {
obj.targetLine.transform("r" + obj.config.donutStartAngle + "," + (w / 2 + dx) + "," + (h / 2 + dy));
}
};

//
// tiny helper function to lookup value of a key from two hash tables
// if none found, return defaultvalue
Expand Down