Skip to content

Commit

Permalink
Fixed issue Mikhus#90 - taking into account font size when drawing ti…
Browse files Browse the repository at this point in the history
…ck labels on radial gauge
  • Loading branch information
Mikhus committed Dec 28, 2016
1 parent 5a29178 commit 3516f40
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 10 deletions.
130 changes: 130 additions & 0 deletions examples/adoptive-tick-labels.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Gauge Test</title>
<script src="../gauge.min.js"></script>
<style>body {
padding: 0;
margin: 0;
background: #fff
}</style>
</head>
<body>

<button onclick="animateGauges()">Animate</button>
<button onclick="stopGaugesAnimation()">Stop animation</button>

<hr>

<canvas data-type="radial-gauge"
data-min-value="13"
data-max-value="87"
data-exact-ticks="true"
data-major-ticks="13,20,25,38,50,62,80,87"
data-minor-ticks="2"
data-highlights="0"
data-width="500"
data-height="500"
></canvas>

<canvas data-type="radial-gauge"
data-min-value="13"
data-max-value="87"
data-exact-ticks="true"
data-major-ticks="13,20,25,38,50,62,80,87"
data-minor-ticks="2"
data-highlights="0"
data-width="500"
data-height="500"
data-font-numbers-size="10"
></canvas>

<canvas data-type="radial-gauge"
data-min-value="13"
data-max-value="87"
data-exact-ticks="true"
data-major-ticks="13,20,25,38,50,62,80,87"
data-minor-ticks="2"
data-highlights="0"
data-width="500"
data-height="500"
data-font-numbers-size="20"
></canvas>

<canvas data-type="radial-gauge"
data-min-value="13"
data-max-value="87"
data-exact-ticks="true"
data-major-ticks="13,20,25,38,50,62,80,87"
data-minor-ticks="2"
data-highlights="0"
data-width="500"
data-height="500"
data-font-numbers-size="30"
></canvas>

<canvas data-type="radial-gauge"
data-min-value="13"
data-max-value="87"
data-exact-ticks="true"
data-major-ticks="13,20,25,38,50,62,80,87"
data-minor-ticks="2"
data-highlights="0"
data-width="500"
data-height="500"
data-font-numbers-size="40"
></canvas>

<canvas data-type="radial-gauge"
data-min-value="13"
data-max-value="87"
data-exact-ticks="true"
data-major-ticks="13,20,25,38,50,62,80,87"
data-minor-ticks="2"
data-highlights="0"
data-width="500"
data-height="500"
data-font-numbers-size="50"
></canvas>

<script>
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(cb) {
var i = 0, s = this.length;
for (; i < s; i++) {
cb && cb(this[i], i, this);
}
}
}

document.fonts && document.fonts.forEach(function(font) {
font.loaded.then(function() {
if (font.family.match(/Led/)) {
document.gauges.forEach(function(gauge) {
gauge.update();
});
}
});
});

var timers = [];

function animateGauges() {
document.gauges.forEach(function(gauge) {
timers.push(setInterval(function() {
gauge.value = Math.random() *
(gauge.options.maxValue - gauge.options.minValue) +
gauge.options.minValue;
}, gauge.animation.duration + 50));
});
}

function stopGaugesAnimation() {
timers.forEach(function(timer) {
clearInterval(timer);
});
}
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions gauge.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gauge.min.js.map

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions lib/RadialGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ function closeStrokedPath(context) {
* @param {RadialGaugeOptions} options
*/
function drawRadialNumbers(context, options) {
let radius = radialTicksRadius(context, options) - context.max * 0.25;
let textWidth = Math.max.apply(null, options.majorTicks.map(text =>
context.measureText(text).width));
let textHeight = options.fontNumbersSize;
let radius = radialTicksRadius(context, options) - context.max * 0.2 -
Math.sqrt(textWidth * textWidth + textHeight * textHeight) / 2;
let points = {};
let i = 0;
let s = options.majorTicks.length;
Expand All @@ -392,10 +396,8 @@ function drawRadialNumbers(context, options) {
}

for (; i < s; ++i) {
let angle = plateValueAngle + radialNextAngle(
options,
options.exactTicks ? options.majorTicks[i] : i,
s);
let angle = plateValueAngle + radialNextAngle(options,
options.exactTicks ? options.majorTicks[i] : i, s);
let point = drawings.radialPoint(radius, drawings.radians(angle));

if (angle === 360) angle = 0;
Expand All @@ -410,7 +412,8 @@ function drawRadialNumbers(context, options) {
context.fillStyle = colors[i];
context.lineWidth = 0;
context.textAlign = 'center';
context.fillText(options.majorTicks[i], point.x, point.y + 3);
context.textBaseline = 'middle';
context.fillText(options.majorTicks[i], point.x, point.y);
}

isAnimated && context.restore();
Expand Down
2 changes: 1 addition & 1 deletion test-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3516f40

Please sign in to comment.