glutamatt HF Staff commited on
Commit
6814b6b
·
verified ·
1 Parent(s): 1db0278

gradient opacity for acwr

Browse files
Files changed (1) hide show
  1. src/components/charts.ts +40 -4
src/components/charts.ts CHANGED
@@ -67,6 +67,19 @@ const acwrGradientPlugin = {
67
  const data = chart.data.datasets[acwrDatasetIndex].data as (number | null)[];
68
  const chartArea = chart.chartArea;
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  ctx.save();
71
 
72
  // First, fill the area under the curve with gradient colors
@@ -82,12 +95,22 @@ const acwrGradientPlugin = {
82
  // Skip if both values are null
83
  if (value1 === null && value2 === null) continue;
84
 
 
 
 
85
  // Use the second point value (most recent/rightward)
86
  const segmentValue = value2 ?? value1;
87
 
 
 
 
 
 
 
 
88
  const color = getACWRColor(segmentValue);
89
- // Make the fill more transparent
90
- const fillColor = color.replace(/[\d.]+\)$/, '0.25)');
91
 
92
  ctx.fillStyle = fillColor;
93
  ctx.beginPath();
@@ -104,7 +127,7 @@ const acwrGradientPlugin = {
104
  ctx.lineCap = 'round';
105
  ctx.lineJoin = 'round';
106
 
107
- // Draw line segments with colors based on values
108
  for (let i = 0; i < meta.data.length - 1; i++) {
109
  const point1 = meta.data[i];
110
  const point2 = meta.data[i + 1];
@@ -117,10 +140,23 @@ const acwrGradientPlugin = {
117
  // Skip if both values are null
118
  if (value1 === null && value2 === null) continue;
119
 
 
 
 
120
  // Use the second point value (most recent/rightward)
121
  const segmentValue = value2 ?? value1;
122
 
123
- ctx.strokeStyle = getACWRColor(segmentValue);
 
 
 
 
 
 
 
 
 
 
124
  ctx.beginPath();
125
  ctx.moveTo(point1.x, point1.y);
126
  ctx.lineTo(point2.x, point2.y);
 
67
  const data = chart.data.datasets[acwrDatasetIndex].data as (number | null)[];
68
  const chartArea = chart.chartArea;
69
 
70
+ // Find the range of non-null ACWR values for opacity gradient
71
+ let firstNonNullIndex = -1;
72
+ let lastNonNullIndex = -1;
73
+ for (let i = 0; i < data.length; i++) {
74
+ if (data[i] !== null) {
75
+ if (firstNonNullIndex === -1) firstNonNullIndex = i;
76
+ lastNonNullIndex = i;
77
+ }
78
+ }
79
+
80
+ // Calculate total span of ACWR data
81
+ const acwrSpan = lastNonNullIndex - firstNonNullIndex;
82
+
83
  ctx.save();
84
 
85
  // First, fill the area under the curve with gradient colors
 
95
  // Skip if both values are null
96
  if (value1 === null && value2 === null) continue;
97
 
98
+ // Skip if the first value is null (start of ACWR curve)
99
+ if (value1 === null) continue;
100
+
101
  // Use the second point value (most recent/rightward)
102
  const segmentValue = value2 ?? value1;
103
 
104
+ // Calculate opacity based on position: start at 0.05, end at 0.25
105
+ let opacity = 0.25;
106
+ if (acwrSpan > 0 && i >= firstNonNullIndex && i <= lastNonNullIndex) {
107
+ const progress = (i - firstNonNullIndex) / acwrSpan;
108
+ opacity = 0.05 + (progress * 0.20); // 0.05 to 0.25
109
+ }
110
+
111
  const color = getACWRColor(segmentValue);
112
+ // Apply variable opacity to the fill
113
+ const fillColor = color.replace(/[\d.]+\)$/, `${opacity})`);
114
 
115
  ctx.fillStyle = fillColor;
116
  ctx.beginPath();
 
127
  ctx.lineCap = 'round';
128
  ctx.lineJoin = 'round';
129
 
130
+ // Draw line segments with colors based on values and variable opacity
131
  for (let i = 0; i < meta.data.length - 1; i++) {
132
  const point1 = meta.data[i];
133
  const point2 = meta.data[i + 1];
 
140
  // Skip if both values are null
141
  if (value1 === null && value2 === null) continue;
142
 
143
+ // Skip if the first value is null (start of ACWR curve)
144
+ if (value1 === null) continue;
145
+
146
  // Use the second point value (most recent/rightward)
147
  const segmentValue = value2 ?? value1;
148
 
149
+ // Calculate opacity based on position: start at 0.20, end at 0.99
150
+ let opacity = 0.99;
151
+ if (acwrSpan > 0 && i >= firstNonNullIndex && i <= lastNonNullIndex) {
152
+ const progress = (i - firstNonNullIndex) / acwrSpan;
153
+ opacity = 0.20 + (progress * 0.79); // 0.20 to 0.99
154
+ }
155
+
156
+ const color = getACWRColor(segmentValue);
157
+ const strokeColor = color.replace(/[\d.]+\)$/, `${opacity})`);
158
+
159
+ ctx.strokeStyle = strokeColor;
160
  ctx.beginPath();
161
  ctx.moveTo(point1.x, point1.y);
162
  ctx.lineTo(point2.x, point2.y);