@cryptotaxi247 / netdata-1 / commits / b88d7cee8

legends now have uniform but dynamic number of fraction digits

Costa Tsaousis (ktsaou) committed Feb 5, 2017 at 22:30 UTC b88d7cee8220ec068e7024f6ed868ef9e3a92ccf
3 files changed +60 -14
src/web_buffer_svg.c
+1 -1
@@ -389,7 +389,7 @@ static inline char *format_value_with_precision_and_unit(char *value_string, siz
389 len = snprintfz(value_string, value_string_len, "%0.0Lf", (long double) value);
390 trim_zeros = 0;
391 }
392 - else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, value_string_len, "%0.1Lf", (long double) value);
392 + else if(isgreaterequal(abs, 10)) len = snprintfz(value_string, value_string_len, "%0.1Lf", (long double) value);
393 else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, value_string_len, "%0.2Lf", (long double) value);
394 else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, value_string_len, "%0.3Lf", (long double) value);
395 else len = snprintfz(value_string, value_string_len, "%0.4Lf", (long double) value);
web/dashboard.js
+58 -12
@@ -2247,19 +2247,53 @@ var NETDATA = window.NETDATA || {};
2247 return ret;
2248 };
2249
2250 + this.legendFormatValueChartDecimals = -1;
2251 + this.legendFormatValueDecimalsFromMinMax = function(min, max) {
2252 + var delta;
2253 +
2254 + if(min === max)
2255 + delta = Math.abs(min);
2256 + else
2257 + delta = Math.abs(max - min);
2258 +
2259 + if(delta > 1000) this.legendFormatValueChartDecimals = 0;
2260 + else if(delta > 10 ) this.legendFormatValueChartDecimals = 1;
2261 + else if(delta > 1 ) this.legendFormatValueChartDecimals = 2;
2262 + else if(delta > 0.1 ) this.legendFormatValueChartDecimals = 3;
2263 + else this.legendFormatValueChartDecimals = 4;
2264 + };
2265 +
2266 this.legendFormatValue = function(value) {
2251 - if(value === null || value === 'undefined') return '-';
2252 - if(typeof value !== 'number') return value;
2253 -
2254 - if(this.value_decimal_detail !== -1)
2255 - return (Math.round(value * this.value_decimal_detail) / this.value_decimal_detail).toLocaleString();
2256 -
2257 - var abs = Math.abs(value);
2258 - if(abs >= 1000) return (Math.round(value)).toLocaleString();
2259 - if(abs >= 100 ) return (Math.round(value * 10) / 10).toLocaleString();
2260 - if(abs >= 1 ) return (Math.round(value * 100) / 100).toLocaleString();
2261 - if(abs >= 0.1 ) return (Math.round(value * 1000) / 1000).toLocaleString();
2262 - return (Math.round(value * 10000) / 10000).toLocaleString();
2267 + if(typeof value !== 'number') return '-';
2268 +
2269 + var dmin, dmax;
2270 +
2271 + if(this.value_decimal_detail !== -1) {
2272 + dmin = dmax = this.value_decimal_detail;
2273 + }
2274 +
2275 + if(this.legendFormatValueChartDecimals < 0) {
2276 + dmin = 0;
2277 + var abs = value;
2278 + if(abs > 1000) dmax = 0;
2279 + else if(abs > 10 ) dmax = 1;
2280 + else if(abs > 1) dmax = 2;
2281 + else if(abs > 0.1) dmax = 3;
2282 + else dmax = 4;
2283 + }
2284 + else {
2285 + dmin = dmax = this.legendFormatValueChartDecimals;
2286 + }
2287 +
2288 + return value.toLocaleString(undefined, {
2289 + // style: 'decimal',
2290 + // minimumIntegerDigits: 1,
2291 + // minimumSignificantDigits: 1,
2292 + // maximumSignificantDigits: 1,
2293 + useGrouping: true,
2294 + minimumFractionDigits: dmin,
2295 + maximumFractionDigits: dmax
2296 + });
2297 };
2298
2299 this.legendSetLabelValue = function(label, value) {
@@ -4212,6 +4246,12 @@ var NETDATA = window.NETDATA || {};
4246 dygraph.updateOptions(options);
4247 }
4248
4249 + // decide the decimal points on the legend of the chart
4250 + state.legendFormatValueDecimalsFromMinMax(
4251 + state.dygraph_instance.axes_[0].extremeRange[0],
4252 + state.dygraph_instance.axes_[0].extremeRange[1]
4253 + );
4254 +
4255 state.dygraph_last_rendered = Date.now();
4256 return true;
4257 };
@@ -4819,6 +4859,12 @@ var NETDATA = window.NETDATA || {};
4859 state.__commonMax = null;
4860 }
4861
4862 + // decide the decimal points on the legend of the chart
4863 + state.legendFormatValueDecimalsFromMinMax(
4864 + state.dygraph_instance.axes_[0].extremeRange[0],
4865 + state.dygraph_instance.axes_[0].extremeRange[1]
4866 + );
4867 +
4868 return true;
4869 };
4870
web/index.html
+1 -1
@@ -3312,4 +3312,4 @@
3312 </div>
3313 </body>
3314 </html>
3315 -<script type="text/javascript" src="dashboard.js?v20170205-10"></script>
3315 +<script type="text/javascript" src="dashboard.js?v20170205-28"></script>