From 70daa9d3fab04d8cd1146a6d3505639c23ad1e7f Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 21 Apr 2016 15:23:46 +0100 Subject: [PATCH] =?UTF-8?q?Round=20metric=20graphs=20to=20metric.places=20?= =?UTF-8?q?=E2=80=94=20closes=20#1704?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/views/partials/metrics.blade.php | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/resources/views/partials/metrics.blade.php b/resources/views/partials/metrics.blade.php index 5e782084..0fde99d3 100644 --- a/resources/views/partials/metrics.blade.php +++ b/resources/views/partials/metrics.blade.php @@ -98,7 +98,30 @@ yAxes: [{ ticks: { beginAtZero: true, - suggestedMax: 0.1 + suggestedMax: 0.1, + callback: function(tickValue, index, ticks) { + var delta = ticks[1] - ticks[0]; + + // If we have a number like 2.5 as the delta, figure out how many decimal places we need + if (Math.abs(delta) > 1) { + if (tickValue !== Math.floor(tickValue)) { + delta = tickValue - Math.floor(tickValue); + } + } + + var logDelta = Chart.helpers.log10(Math.abs(delta)); + var tickString = ''; + + if (tickValue !== 0) { + var numDecimal = -1 * Math.floor(logDelta); + numDecimal = Math.max(Math.min(numDecimal, {{ $metric->places }}), 0); // Use as many places as the metric defines + tickString = tickValue.toFixed(numDecimal); + } else { + tickString = '0'; // Never show decimal places for 0 + } + + return tickString; + } } }] },