OLD | NEW |
| (Empty) |
1 // | |
2 // See ..\Samples\issue_102.html | |
3 // | |
4 // Module intially written by Ole Kroger | |
5 // Change 1 : Omar Sedki - possibility to specify a percentage | |
6 // Change 2 : Vancoppenolle François - generalized for all graph types | |
7 // | |
8 function gradientColor(area, data, config, i, j, currentAnimPc, value, typegraph
, ctx, v0, v1, v2, v3) { | |
9 // v0 = left xAxis of rectangle or xAxis of center | |
10 // v1 = top yAxis of rectangle or yAxis of center | |
11 // v2 = right xAxis of rectangle or internal radius (=0 for PolarArea, P
ie and Radar) | |
12 // v3 = bottom yAxis or rectangle or external radius | |
13 var grd; | |
14 if (typegraph == "Line" || typegraph == "Bar" || typegraph == "StackedBa
r") { | |
15 grd = ctx.createLinearGradient(v0, v1, v0, v3); | |
16 var gradientColors = data.datasets[i].gradientColors; | |
17 } else if (typegraph == "HorizontalStackedBar" || typegraph == "Horizont
alBar") { | |
18 grd = ctx.createLinearGradient(v0, v1, v2, v1); | |
19 var gradientColors = data.datasets[i].gradientColors; | |
20 } else if (typegraph == "Pie" || typegraph == "Doughnut" || typegraph ==
"PolarArea" || typegraph == "Radar") { | |
21 if (area == "COLOR" || (typegraph == "Radar" && area == "FILLCOL
OR")) { | |
22 var grd = ctx.createRadialGradient(v0, v1, v2, v0, v1, v
3); | |
23 } else { | |
24 var grd = ctx.createRadialGradient(v0 + (v2 - v0) / 2, v
1 + (v3 - v1) / 2, 0, v0 + (v2 - v0) / 2, v1 + (v3 - v1) / 2, Math.max((v2 - v0)
/ 2, (v3 - v1) / 2)); | |
25 } | |
26 if (typegraph == "Radar") var gradientColors = data.datasets[i].
gradientColors; | |
27 else var gradientColors = data[i].gradientColors; | |
28 } | |
29 var steps = gradientColors.length; | |
30 var currentStepValue = 0; | |
31 var stepValues = []; | |
32 var PERCENT_REGEX = /(\d{1,2}|100)%\s*?$/g | |
33 for (var iter = 0; iter < steps; iter++) { | |
34 var userStepValue = gradientColors[iter].match(PERCENT_REGEX); | |
35 if (!userStepValue) { | |
36 stepValues[iter] = false; | |
37 continue; | |
38 } | |
39 userStepValue = parseFloat(userStepValue) / 100.0; | |
40 stepValues[iter] = userStepValue; | |
41 } | |
42 for (var iter = 0; iter < steps; iter++) { | |
43 if (stepValues[iter] === false) { | |
44 if (iter == 0) { | |
45 stepValues[iter] = 0; | |
46 } else if (iter == steps - 1) { | |
47 stepValues[iter] = 1; | |
48 } else { | |
49 // found next stepValue which isn't false | |
50 var s = iter + 1; | |
51 while (s < steps - 1 && stepValues[s] === false)
{ | |
52 s++; | |
53 } | |
54 var lastStep = ((iter == 0) ? 0 : stepValues[ite
r - 1]); | |
55 stepValues[iter] = ((s >= steps - 1) ? 1 : stepV
alues[s + 1]) - lastStep; | |
56 stepValues[iter] = lastStep + stepValues[iter] /
(s - iter + 1); | |
57 } | |
58 } | |
59 GradientcolorsWithoutStep = gradientColors[iter].replace(PERCENT
_REGEX, "").trim(); | |
60 grd.addColorStop(stepValues[iter], GradientcolorsWithoutStep); | |
61 } | |
62 return grd; | |
63 } | |
OLD | NEW |