]> arthur.barton.de Git - netdata.git/commitdiff
legends now have uniform but dynamic number of fraction digits
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sun, 5 Feb 2017 20:30:42 +0000 (22:30 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sun, 5 Feb 2017 20:30:42 +0000 (22:30 +0200)
src/web_buffer_svg.c
web/dashboard.js
web/index.html

index bdc1608ed2889087d90f214d41e417d12bf4c6c3..9537780d7a803cc9bd615dc0ac02ac11c093091c 100644 (file)
@@ -389,7 +389,7 @@ static inline char *format_value_with_precision_and_unit(char *value_string, siz
             len = snprintfz(value_string, value_string_len, "%0.0Lf", (long double) value);
             trim_zeros = 0;
         }
-        else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, value_string_len, "%0.1Lf", (long double) value);
+        else if(isgreaterequal(abs, 10))  len = snprintfz(value_string, value_string_len, "%0.1Lf", (long double) value);
         else if(isgreaterequal(abs, 1))   len = snprintfz(value_string, value_string_len, "%0.2Lf", (long double) value);
         else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, value_string_len, "%0.3Lf", (long double) value);
         else                              len = snprintfz(value_string, value_string_len, "%0.4Lf", (long double) value);
index 4947426f2d010372d94dc0c95591628636f99043..6dcbce7f8085a3845328b453a6349191d7250dcf 100644 (file)
@@ -2247,19 +2247,53 @@ var NETDATA = window.NETDATA || {};
             return ret;
         };
 
+        this.legendFormatValueChartDecimals = -1;
+        this.legendFormatValueDecimalsFromMinMax = function(min, max) {
+            var delta;
+
+            if(min === max)
+                delta = Math.abs(min);
+            else
+                delta = Math.abs(max - min);
+
+            if(delta > 1000)      this.legendFormatValueChartDecimals = 0;
+            else if(delta > 10  ) this.legendFormatValueChartDecimals = 1;
+            else if(delta > 1   ) this.legendFormatValueChartDecimals = 2;
+            else if(delta > 0.1 ) this.legendFormatValueChartDecimals = 3;
+            else                  this.legendFormatValueChartDecimals = 4;
+        };
+
         this.legendFormatValue = function(value) {
-            if(value === null || value === 'undefined') return '-';
-            if(typeof value !== 'number') return value;
-
-            if(this.value_decimal_detail !== -1)
-                return (Math.round(value * this.value_decimal_detail) / this.value_decimal_detail).toLocaleString();
-
-            var abs = Math.abs(value);
-            if(abs >= 1000) return (Math.round(value)).toLocaleString();
-            if(abs >= 100 ) return (Math.round(value * 10) / 10).toLocaleString();
-            if(abs >= 1   ) return (Math.round(value * 100) / 100).toLocaleString();
-            if(abs >= 0.1 ) return (Math.round(value * 1000) / 1000).toLocaleString();
-            return (Math.round(value * 10000) / 10000).toLocaleString();
+            if(typeof value !== 'number') return '-';
+
+            var dmin, dmax;
+
+            if(this.value_decimal_detail !== -1) {
+                dmin = dmax = this.value_decimal_detail;
+            }
+
+            if(this.legendFormatValueChartDecimals < 0) {
+                dmin = 0;
+                var abs = value;
+                if(abs > 1000)      dmax = 0;
+                else if(abs > 10 )  dmax = 1;
+                else if(abs > 1)    dmax = 2;
+                else if(abs > 0.1)  dmax = 3;
+                else                dmax = 4;
+            }
+            else {
+                dmin = dmax = this.legendFormatValueChartDecimals;
+            }
+
+            return value.toLocaleString(undefined, {
+                // style: 'decimal',
+                // minimumIntegerDigits: 1,
+                // minimumSignificantDigits: 1,
+                // maximumSignificantDigits: 1,
+                useGrouping: true,
+                minimumFractionDigits: dmin,
+                maximumFractionDigits: dmax
+            });
         };
 
         this.legendSetLabelValue = function(label, value) {
@@ -4212,6 +4246,12 @@ var NETDATA = window.NETDATA || {};
             dygraph.updateOptions(options);
         }
 
+        // decide the decimal points on the legend of the chart
+        state.legendFormatValueDecimalsFromMinMax(
+            state.dygraph_instance.axes_[0].extremeRange[0],
+            state.dygraph_instance.axes_[0].extremeRange[1]
+        );
+
         state.dygraph_last_rendered = Date.now();
         return true;
     };
@@ -4819,6 +4859,12 @@ var NETDATA = window.NETDATA || {};
             state.__commonMax = null;
         }
 
+        // decide the decimal points on the legend of the chart
+        state.legendFormatValueDecimalsFromMinMax(
+            state.dygraph_instance.axes_[0].extremeRange[0],
+            state.dygraph_instance.axes_[0].extremeRange[1]
+        );
+
         return true;
     };
 
index 860cd0307e9a796f0cc8a5a9fb5de5d2d5b1060a..e7caad9fe28f2b8350fb44e5bbc237e4b969802a 100644 (file)
     </div>
 </body>
 </html>
-<script type="text/javascript" src="dashboard.js?v20170205-10"></script>
+<script type="text/javascript" src="dashboard.js?v20170205-28"></script>