|
21 | 21 | <br> |
22 | 22 | <button id="randomizeData">Randomize Data</button> |
23 | 23 | <script> |
24 | | - var chartColors = window.chartColors; |
25 | | - var gradient = null; |
| 24 | + var cache = new Map(); |
26 | 25 | var width = null; |
27 | 26 | var height = null; |
| 27 | + |
| 28 | + function createRadialGradient3(context, c1, c2, c3) { |
| 29 | + var chartArea = context.chart.chartArea; |
| 30 | + if (!chartArea) { |
| 31 | + // This case happens on initial chart load |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + var chartWidth = chartArea.right - chartArea.left; |
| 36 | + var chartHeight = chartArea.bottom - chartArea.top; |
| 37 | + if (width !== chartWidth || height !== chartHeight) { |
| 38 | + cache.clear(); |
| 39 | + } |
| 40 | + var gradient = cache.get(c1 + c2 + c3); |
| 41 | + if (!gradient) { |
| 42 | + // Create the gradient because this is either the first render |
| 43 | + // or the size of the chart has changed |
| 44 | + width = chartWidth; |
| 45 | + height = chartHeight; |
| 46 | + var centerX = (chartArea.left + chartArea.right) / 2; |
| 47 | + var centerY = (chartArea.top + chartArea.bottom) / 2; |
| 48 | + var r = Math.min( |
| 49 | + (chartArea.right - chartArea.left) / 2, |
| 50 | + (chartArea.bottom - chartArea.top) / 2 |
| 51 | + ); |
| 52 | + var ctx = context.chart.ctx; |
| 53 | + gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, r); |
| 54 | + gradient.addColorStop(0, c1); |
| 55 | + gradient.addColorStop(0.5, c2); |
| 56 | + gradient.addColorStop(1, c3); |
| 57 | + cache.set(c1 + c2 + c3, gradient); |
| 58 | + } |
| 59 | + |
| 60 | + return gradient; |
| 61 | + } |
| 62 | + |
| 63 | + var chartColors = window.chartColors; |
| 64 | + var colors = [chartColors.red, chartColors.orange, chartColors.yellow, chartColors.green, chartColors.blue]; |
28 | 65 | var config = { |
29 | 66 | type: 'polarArea', |
30 | 67 | data: { |
|
37 | 74 | randomScalingFactor(), |
38 | 75 | ], |
39 | 76 | backgroundColor: function(context) { |
40 | | - var chartArea = context.chart.chartArea; |
41 | | - |
42 | | - if (!chartArea) { |
43 | | - // This case happens on initial chart load |
44 | | - return null; |
| 77 | + var c = colors[context.dataIndex]; |
| 78 | + if (context.active) { |
| 79 | + c = Chart.helpers.getHoverColor(c); |
45 | 80 | } |
46 | | - |
47 | | - var chartWidth = chartArea.right - chartArea.left; |
48 | | - var chartHeight = chartArea.bottom - chartArea.top; |
49 | | - if (gradient === null || width !== chartWidth || height !== chartHeight) { |
50 | | - // Create the gradient because this is either the first render |
51 | | - // or the size of the chart has changed |
52 | | - width = chartWidth; |
53 | | - height = chartHeight; |
54 | | - var centerX = (chartArea.left + chartArea.right) / 2; |
55 | | - var centerY = (chartArea.top + chartArea.bottom) / 2; |
56 | | - var r = Math.min( |
57 | | - (chartArea.right - chartArea.left) / 2, |
58 | | - (chartArea.bottom - chartArea.top) / 2 |
59 | | - ); |
60 | | - var ctx = context.chart.ctx; |
61 | | - gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, r); |
62 | | - gradient.addColorStop(0, chartColors.red); |
63 | | - gradient.addColorStop(0.5, chartColors.green); |
64 | | - gradient.addColorStop(1, chartColors.purple); |
65 | | - } |
66 | | - |
67 | | - return gradient; |
| 81 | + var mid = Chart.helpers.color(c).desaturate(0.2).darken(0.2).rgbString(); |
| 82 | + var start = Chart.helpers.color(c).lighten(0.2).rotate(270).rgbString(); |
| 83 | + var end = Chart.helpers.color(c).lighten(0.1).rgbString(); |
| 84 | + return createRadialGradient3(context, start, mid, end); |
68 | 85 | }, |
69 | 86 | label: 'My dataset' // for legend |
70 | 87 | }], |
|
0 commit comments