Skip to content

Commit abb5b27

Browse files
committed
Added customizable label
1 parent cb8c8d2 commit abb5b27

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

dist/gauge.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@
142142
start: getCartesian(cx, cy, radius, startAngle)
143143
};
144144
}
145+
146+
function defaultLabelRenderer(theValue) {
147+
return Math.round(theValue);
148+
}
145149

146150
/**
147151
* Creates a Gauge object. This should be called without the 'new' operator. Various options
@@ -152,6 +156,7 @@
152156
* radius: The gauge's radius. Default 400
153157
* max: The maximum value of the gauge. Default 100
154158
* value: The starting value of the gauge. Default 0
159+
* label: The function on how to render the center label (Should return a value)
155160
* }
156161
* @param {Element} elem The DOM into which to render the gauge
157162
* @param {Object} opts The gauge options
@@ -163,6 +168,7 @@
163168
value = normalize(opts.value || 0, limit),
164169
radius = opts.radius || 400,
165170
displayValue = opts.showValue === false ? false : true,
171+
valueLabelRender = typeof opts.label === "function" ? opts.label : defaultLabelRenderer,
166172
startAngle = typeof(opts.dialStartAngle) === "undefined" ? 135 : opts.dialStartAngle,
167173
endAngle = typeof(opts.dialEndAngle) === "undefined" ? 45 : opts.dialEndAngle,
168174
gaugeTextElem,
@@ -226,7 +232,7 @@
226232
angle = getAngle(val, 360 - Math.abs(startAngle - endAngle)),
227233
// this is because we are using arc greater than 180deg
228234
flag = angle <= 180 ? 0 : 1;
229-
(displayValue && (gaugeTextElem.textContent = Math.round(theValue)));
235+
(displayValue && (gaugeTextElem.textContent = valueLabelRender.call(opts, theValue)));
230236
gaugeValuePath.setAttribute("d", pathString(radius, startAngle, angle + startAngle, flag));
231237
}
232238

dist/gauge.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
padding: 0;
4141
margin: 0;
4242
}
43+
44+
#gauge1 .value-text {
45+
font-size: 6em;
46+
}
4347

4448
@media only screen and (min-device-width: 600px) {
4549
.example > .code {
@@ -81,7 +85,10 @@ <h4>Minimalistic, zero dependency animated gauge widget</h4>
8185
max: 100,
8286
dialStartAngle: -90,
8387
dialEndAngle: -90.001,
84-
value: 100
88+
value: 100,
89+
label: function(value) {
90+
return Math.round(value) + "/" + this.max;
91+
}
8592
}
8693
);</code>
8794
</pre>
@@ -235,7 +242,10 @@ <h4>Minimalistic, zero dependency animated gauge widget</h4>
235242
max: 100,
236243
dialStartAngle: -90,
237244
dialEndAngle: -90.001,
238-
value: 100
245+
value: 100,
246+
label: function(value) {
247+
return Math.round(value) + "/" + this.max;
248+
}
239249
}
240250
);
241251

src/gauge.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@
142142
start: getCartesian(cx, cy, radius, startAngle)
143143
};
144144
}
145+
146+
function defaultLabelRenderer(theValue) {
147+
return Math.round(theValue);
148+
}
145149

146150
/**
147151
* Creates a Gauge object. This should be called without the 'new' operator. Various options
@@ -152,6 +156,7 @@
152156
* radius: The gauge's radius. Default 400
153157
* max: The maximum value of the gauge. Default 100
154158
* value: The starting value of the gauge. Default 0
159+
* label: The function on how to render the center label (Should return a value)
155160
* }
156161
* @param {Element} elem The DOM into which to render the gauge
157162
* @param {Object} opts The gauge options
@@ -163,6 +168,7 @@
163168
value = normalize(opts.value || 0, limit),
164169
radius = opts.radius || 400,
165170
displayValue = opts.showValue === false ? false : true,
171+
valueLabelRender = typeof opts.label === "function" ? opts.label : defaultLabelRenderer,
166172
startAngle = typeof(opts.dialStartAngle) === "undefined" ? 135 : opts.dialStartAngle,
167173
endAngle = typeof(opts.dialEndAngle) === "undefined" ? 45 : opts.dialEndAngle,
168174
gaugeTextElem,
@@ -226,7 +232,7 @@
226232
angle = getAngle(val, 360 - Math.abs(startAngle - endAngle)),
227233
// this is because we are using arc greater than 180deg
228234
flag = angle <= 180 ? 0 : 1;
229-
(displayValue && (gaugeTextElem.textContent = Math.round(theValue)));
235+
(displayValue && (gaugeTextElem.textContent = valueLabelRender.call(opts, theValue)));
230236
gaugeValuePath.setAttribute("d", pathString(radius, startAngle, angle + startAngle, flag));
231237
}
232238

0 commit comments

Comments
 (0)