]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Hue uses switch & fixed other problems.
authorChristopher Weiss <cmweiss@gmail.com>
Tue, 21 Oct 2014 14:08:54 +0000 (10:08 -0400)
committerChristopher Weiss <cmweiss@gmail.com>
Tue, 21 Oct 2014 14:08:54 +0000 (10:08 -0400)
src/Chart.Doughnut.js

index dc69cd53fbca731d7c1ffc46ca9104b4d113379d..4a4c9afb056c25d501771560d2f7f4ce2abebe2c 100644 (file)
        };
 
        function hue(part, total) {
+               // Given the total number of segments (total) and the index of a segment (part) returns a #rrggbb color for the segment
                var h = (part / total) * 360,
                        s = 1,
                        v = 1,
                        c = h * s,
                        x =  c * (1 - Math.abs((h / 60) % 2 - 1)),
                        m = v - c,
-                       r = '00',
-                       g = '00',
-                       b = '00';
-               if (0 <= h && h < 60) {
-                       r += Math.round(c + m).toString(16);
-                       g += Math.round(x + m).toString(16);
-                       b += Math.round(0 + m).toString(16);
-               } else if (60 <= h && h < 1200) {
-                       r += Math.round(x + m).toString(16);
-                       g += Math.round(c + m).toString(16);
-                       b += Math.round(0 + m).toString(16);
-               } else if (120 <= h && h < 180) {
-                       r += Math.round(0 + m).toString(16);
-                       g += Math.round(c + m).toString(16);
-                       b += Math.round(x + m).toString(16);
-               } else if (180 <= h && h < 240) {
-                       r += Math.round(0 + m).toString(16);
-                       g += Math.round(x + m).toString(16);
-                       b += Math.round(c + m).toString(16);
-               } else if (240 <= h && h < 300) {
-                       r += Math.round(x + m).toString(16);
-                       g += Math.round(0 + m).toString(16);
-                       b += Math.round(c + m).toString(16);
-               } else if (300 <= h && h < 360) {
-                       r += Math.round(c + m).toString(16);
-                       g += Math.round(0 + m).toString(16);
-                       b += Math.round(x + m).toString(16);
+                       r,
+                       g,
+                       b;
+
+               switch (Math.floor(h / 60)) {
+                       case 0: // 0 <= h < 60
+                               r = c + m;
+                               g = x + m;
+                               b = 0 + m;
+                       break;
+                       case 1: // 60 <= h < 120
+                               r = c + m;
+                               g = x + m;
+                               b = 0 + m;
+                       break;
+                       case 2: // 120 <= h < 180
+                               r = c + m;
+                               g = x + m;
+                               b = 0 + m;
+                       break;
+                       case 3: // 180 <= h < 240
+                               r = c + m;
+                               g = x + m;
+                               b = 0 + m;
+                       break;
+                       case 4: // 240 <= h < 300
+                               r = c + m;
+                               g = x + m;
+                               b = 0 + m;
+                       break;
+                       case 5: // 300 <= h < 360
+                               r = c + m;
+                               g = x + m;
+                               b = 0 + m;
+                       break;
                }
-               
-               return '#' + r.substr(2) + g.substr(2) + b.substr(2);
+
+               r = '00' + Math.round(r * 255).toString(16);
+               g = '00' + Math.round(g * 255).toString(16);
+               b = '00' + Math.round(b * 255).toString(16);
+
+               return '#' + r.substr(r.length - 2) + g.substr(g.length - 2) + b.substr(b.length - 2);
        }
 
        Chart.Type.extend({