-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
289 lines (251 loc) · 9.18 KB
/
code.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Show the UI
figma.showUI(__html__, { width: 400, height: 780 });
// Listen to messages from the UI
figma.ui.onmessage = msg => {
if (msg.type === 'submitData') {
const inputData: RadarGraphInput = {
color: msg.data.color, // Updated to get the color from the UI
rounding: msg.data.rounding, // New field
minValue: msg.data.minValue,
maxValue: msg.data.maxValue,
dataSets: msg.data.dataSets,
showDataValue: msg.data.showDataValue,
showDataPoints: msg.data.showDataPoints
};
createRadarGraph(inputData);
}
}
// Define the input types
type DataSet = {
name: string;
value: number;
};
type RadarGraphInput = {
color: string;
rounding: number; // New field for the rounding input
minValue: number;
maxValue: number;
dataSets: DataSet[];
showDataValue: boolean;
showDataPoints: boolean;
};
// Create the radar graph based on the given input
async function createRadarGraph(input: RadarGraphInput) {
//Awaiting font load for text
await figma.loadFontAsync({ family: "Inter", style: "Bold" });
await figma.loadFontAsync({ family: "Inter", style: "Medium" });
await figma.loadFontAsync({ family: "Inter", style: "Regular" });
const color = hexToRgb(input.color);
const cornerRadius = input.rounding; // Get the rounding value
const spiderPolygonColor = hexToRgb("F1F1F1");
const frameSize = 500;
const centerX = 250; //frameSize / 2;
const centerY = 250; //frameSize / 2;
const maxRadius = 200 //frameSize / 2;
const scaleDivision = (input.maxValue - input.minValue) / 5;
// Create a new frame for the radar graph
// Create a new frame for the radar graph
const frame = figma.createFrame();
frame.name = 'Radar Chart';
frame.resize(frameSize, frameSize);
if (figma.editorType === 'figma') {
frame.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
}
else if(figma.editorType === 'figjam'){
frame.fills = [];
}
// Get the current viewport center
const viewportCenterX = figma.viewport.center.x;
const viewportCenterY = figma.viewport.center.y;
// Adjust the position of the radar chart frame to the viewport center
frame.x = viewportCenterX - frame.width / 2;
frame.y = viewportCenterY - frame.height / 2;
// Other settings and appending the frame
frame.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
figma.currentPage.appendChild(frame);
const totalDataSets = input.dataSets.length;
const angleIncrement = (2 * Math.PI) / totalDataSets;
// Helper function to get a point on a circle given an angle and radius
function getPointOnCircle(angle: number, radius: number) {
return {
x: centerX + radius * Math.cos(angle),
y: centerY + radius * Math.sin(angle)
};
}
const radarPolygonPoints: string[] = [];
for (let i = 0; i < totalDataSets; i++) {
const dataSet = input.dataSets[i];
const radius = ((dataSet.value - input.minValue) / (input.maxValue - input.minValue)) * maxRadius;
const angle = i * angleIncrement + (3 * Math.PI / 2);
const point = getPointOnCircle(angle, radius);
radarPolygonPoints.push(`${point.x},${point.y}`);
}
let divisionMultipler = 1;
// Draw spider-web pattern
for (let i = 0.2; i <= 1; i += 0.2) {
const polygonPoints: string[] = [];
for (let j = 0; j < totalDataSets; j++) {
const angle = j * angleIncrement + (3 * Math.PI / 2);
const point = getPointOnCircle(angle, i * maxRadius);
// Create text labels for the data scale
if(input.showDataValue) {
if(j==0){
const labelOffset = -15; // Adjust as necessary
const labelText = figma.createText();
if(i==0.2){
labelText.characters = String(input.minValue + scaleDivision);
}
else if(i==1){
labelText.characters = String(input.maxValue);
}
else{
// limiting the decimal place of the values to 1
const labelValue = Number((input.minValue +(scaleDivision * divisionMultipler)).toFixed(1));
labelText.characters = String(labelValue);
}
//labelText.characters = String(input.dataSets[j].value);
labelText.fontName = { family: "Inter", style: "Bold" };
labelText.fontSize = 12;
labelText.fills = [{ type: 'SOLID', color: spiderPolygonColor }];
labelText.x = point.x + Math.cos(angle) * labelOffset - labelText.width / 2;
labelText.y = point.y + Math.sin(angle) * labelOffset - labelText.height / 2;
labelText.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
frame.appendChild(labelText);
divisionMultipler++;
}
}
if (i == 1) {
// Creating text labels
const labelOffset = 25; // Adjust as necessary
const labelText = figma.createText();
labelText.characters = input.dataSets[j].name;
labelText.fontName = { family: "Inter", style: "Medium" };
labelText.fontSize = 12;
labelText.x = point.x + Math.cos(angle) * labelOffset - labelText.width / 2;
labelText.y = point.y + Math.sin(angle) * labelOffset - labelText.height / 2;
labelText.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
frame.appendChild(labelText);
}
polygonPoints.push(`${point.x},${point.y}`);
// Creating lines from center to the circle
if(!input.showDataValue){
if (i === 1) {
const vector = figma.createVector();
vector.vectorNetwork = {
vertices: [
{ x: centerX, y: centerY },
{ x: point.x, y: point.y }
],
segments: [{ start: 0, end: 1 }]
};
vector.strokes = [{ type: 'SOLID', color: spiderPolygonColor }];
vector.strokeWeight = 2;
frame.appendChild(vector);
vector.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
}
}
}
//Create the spider polygon
const vectorPolygon = figma.createVector();
vectorPolygon.vectorNetwork = {
vertices: polygonPoints.map(point => {
const [x, y] = point.split(',').map(Number);
return { x, y };
}),
segments: Array(polygonPoints.length - 1).fill(0).map((_, i) => {
return { start: i, end: i + 1 };
}).concat({ start: polygonPoints.length - 1, end: 0 }) // To close the polygon
};
vectorPolygon.strokes = [{ type: 'SOLID', color: spiderPolygonColor }];
vectorPolygon.strokeWeight = 2;
vectorPolygon.strokeJoin = "ROUND";
frame.appendChild(vectorPolygon);
vectorPolygon.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
}
// Draw radar chart polygon for fill
const radarFillPolygon = figma.createVector();
radarFillPolygon.vectorNetwork = {
vertices: radarPolygonPoints.map(point => {
const [x, y] = point.split(',').map(Number);
return { x, y };
}),
segments: Array(radarPolygonPoints.length - 1).fill(0).map((_, i) => {
return { start: i, end: i + 1 };
}).concat({ start: radarPolygonPoints.length - 1, end: 0 }) // To close the polygon
};
radarFillPolygon.fills = [{ type: 'SOLID', color: color }];
radarFillPolygon.opacity = 0.3;
radarFillPolygon.cornerRadius = cornerRadius;
frame.appendChild(radarFillPolygon);
radarFillPolygon.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
// Draw radar chart polygon for stroke
const radarStrokePolygon = figma.createVector();
//radarStrokePolygon.vectorNetwork = radarFillPolygon.vectorNetwork; // Use the same network
radarStrokePolygon.vectorNetwork = {
vertices: radarPolygonPoints.map(point => {
const [x, y] = point.split(',').map(Number);
return { x, y };
}),
segments: Array(radarPolygonPoints.length - 1).fill(0).map((_, i) => {
return { start: i, end: i + 1 };
}).concat({ start: radarPolygonPoints.length - 1, end: 0 }) // To close the polygon
};
radarStrokePolygon.strokes = [{ type: 'SOLID', color: color }];
radarStrokePolygon.strokeWeight = 4;
radarStrokePolygon.strokeJoin = "ROUND";
radarStrokePolygon.cornerRadius = cornerRadius;
frame.appendChild(radarStrokePolygon);
radarStrokePolygon.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
if(input.showDataPoints){
// Draw circles on data points
for (let i = 0; i < totalDataSets; i++) {
const dataSet = input.dataSets[i];
const radius = ((dataSet.value - input.minValue) / (input.maxValue - input.minValue)) * maxRadius;
const angle = i * angleIncrement + (3 * Math.PI / 2);
const point = getPointOnCircle(angle, radius);
// Draw circle on each data point
const circle = figma.createEllipse();
circle.resize(12, 12);
circle.x = point.x - 6; // Adjust for circle's size
circle.y = point.y - 6;
circle.fills = [{ type: 'SOLID', color: color }];
frame.appendChild(circle);
circle.constraints = {
horizontal: "SCALE",
vertical: "SCALE"
};
}
}
}
function hexToRgb(hex: string): { r: number, g: number, b: number } {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (!result) {
throw new Error("Invalid HEX color.");
}
return {
r: parseInt(result[1], 16) / 255,
g: parseInt(result[2], 16) / 255,
b: parseInt(result[3], 16) / 255
};
}