From e21147cbb9e93797b1b3949a722b4958eed05a32 Mon Sep 17 00:00:00 2001 From: Christopher Weiss Date: Mon, 20 Oct 2014 21:49:41 -0400 Subject: [PATCH 01/11] Prototype for hue() & default fillColor to use it. --- src/Chart.Doughnut.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Chart.Doughnut.js b/src/Chart.Doughnut.js index 35de93c7f..0dab405c8 100644 --- a/src/Chart.Doughnut.js +++ b/src/Chart.Doughnut.js @@ -36,6 +36,14 @@ }; + function hue(part, total) { + var h, s, v, c, x, m, + r = '00', + g = '00', + b = '00'; + + return '#' + r + g + b; + } Chart.Type.extend({ //Passing in a name registers this chart in the Chart namespace @@ -94,7 +102,7 @@ value : segment.value, outerRadius : (this.options.animateScale) ? 0 : this.outerRadius, innerRadius : (this.options.animateScale) ? 0 : (this.outerRadius/100) * this.options.percentageInnerCutout, - fillColor : segment.color, + fillColor : segment.color || hue(index, this.segments.length), highlightColor : segment.highlight || segment.color, showStroke : this.options.segmentShowStroke, strokeWidth : this.options.segmentStrokeWidth, @@ -181,4 +189,4 @@ defaults : helpers.merge(defaultConfig,{percentageInnerCutout : 0}) }); -}).call(this); \ No newline at end of file +}).call(this); From ae9eaa95c7634fcdb7684f650a050692a3f96b3c Mon Sep 17 00:00:00 2001 From: Christopher Weiss Date: Mon, 20 Oct 2014 22:18:50 -0400 Subject: [PATCH 02/11] hue() complete but undebugged. --- src/Chart.Doughnut.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Chart.Doughnut.js b/src/Chart.Doughnut.js index 0dab405c8..dc69cd53f 100644 --- a/src/Chart.Doughnut.js +++ b/src/Chart.Doughnut.js @@ -37,12 +37,42 @@ }; function hue(part, total) { - var h, s, v, c, x, m, + 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); + } - return '#' + r + g + b; + return '#' + r.substr(2) + g.substr(2) + b.substr(2); } Chart.Type.extend({ From 686ea499534ce1640492192de49197af5fa38a94 Mon Sep 17 00:00:00 2001 From: Christopher Weiss Date: Tue, 21 Oct 2014 10:08:54 -0400 Subject: [PATCH 03/11] Hue uses switch & fixed other problems. --- src/Chart.Doughnut.js | 71 +++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/src/Chart.Doughnut.js b/src/Chart.Doughnut.js index dc69cd53f..4a4c9afb0 100644 --- a/src/Chart.Doughnut.js +++ b/src/Chart.Doughnut.js @@ -37,42 +37,55 @@ }; 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({ From 8984e8c2c4aa42cbd216862d70b5eb25f38fe16e Mon Sep 17 00:00:00 2001 From: Christopher Weiss Date: Tue, 21 Oct 2014 10:16:30 -0400 Subject: [PATCH 04/11] Create doughnut.color.html To test the new version of doughnut. --- samples/doughnut.color.html | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 samples/doughnut.color.html diff --git a/samples/doughnut.color.html b/samples/doughnut.color.html new file mode 100644 index 000000000..00b55038c --- /dev/null +++ b/samples/doughnut.color.html @@ -0,0 +1,57 @@ + + + + Doughnut Chart + + + + +
+ +
+ + + + + From 7e21cb4e04807ab55f4ab61b41fcf6788f66a023 Mon Sep 17 00:00:00 2001 From: Christopher Weiss Date: Tue, 21 Oct 2014 10:19:58 -0400 Subject: [PATCH 05/11] Pointed to core and doughnut. --- samples/doughnut.color.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/doughnut.color.html b/samples/doughnut.color.html index 00b55038c..5457fd094 100644 --- a/samples/doughnut.color.html +++ b/samples/doughnut.color.html @@ -2,7 +2,8 @@ Doughnut Chart - + +