summaryrefslogtreecommitdiff
path: root/src/components/RadarChart.astro
blob: efc0bb0cb2290fe02f2d46af5a9a7ee5d8972b29 (plain)
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
---
// const { data } = Astro.props;

const data = [
    { name: "React", total: 10 },
    { name: "GraphQL", total: 5 },
    { name: "JavaScript", total: 5 },
    { name: "SVG", total: 1 },
    { name: "TypeScript", total: 5 },
];

const chartPadding = 100;
const chartSize = 400;
const chartCenter = chartSize / 2;

const chartData = data.sort((a, b) => b.total - a.total).slice(0, data.length);
const pointPadding = 0;
const max = chartData.reduce(
    (max, current) => (current.total > max ? current.total : max),
    0,
);
const rings = [...Array(6).keys()];

const getX = (angle: number, value: number) =>
    Math.cos(angle - Math.PI / 2) * value;
const getY = (angle: number, value: number) =>
    Math.sin(angle - Math.PI / 2) * value;

const axes = chartData.map((_, index) => {
    const angle = (Math.PI * 2 * index) / chartData.length;
    const x = getX(angle, chartSize / 2);
    const y = getY(angle, chartSize / 2);
    const points = [
        [0, 0],
        [x, y],
    ]
        .map((point) => point[0] + "," + point[1])
        .join(" ");
    return {
        points: points,
    };
});

const properties = chartData.map((item, index) => {
    const { name, total } = item;
    const clamp = Number(total / (max + pointPadding));
    const angle = (Math.PI * 2 * index) / chartData.length;
    const x = getX(angle, (clamp * chartSize) / 2);
    const y = getY(angle, (clamp * chartSize) / 2);
    return { name: name, total: total, x: x, y: y };
});

const shape =
    properties.reduce((items, item, index) => {
        const { x, y } = item;
        const string = `${index === 0 ? "M" : "L"}${x},${y}`;
        return items + string;
    }, "") + "z";

const bgColor = "bg-blue-500";
const borderColor = "border-gray-100";
const strokeColor = "stroke-gray-200";
const shapeClass = "stroke-gray-900 fill-gray-900/[0.5]";
---

<div class=`w-[25%] m-0 p-0 ${bgColor} border rounded ${borderColor} shadow-lg`>
    <div class="w-full grid md:grid-cols-1 items-center gap-16 p-0">
        <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox={`-${chartPadding / 2} -${chartPadding / 2} ${chartSize + chartPadding} ${chartSize + chartPadding}`}
            role="presentation"
        >
            <g transform={`translate(${chartCenter},${chartCenter})`}>
                {
                    rings.map((_, index) => {
                        return (
                            <circle
                                cx={0}
                                cy={0}
                                r={
                                    ((index / rings.length) * chartSize) /
                                    Number((Math.PI / 2 + 0.1).toFixed(2))
                                }
                                class={strokeColor}
                                fill="none"
                                stroke-width={1}
                            />
                        );
                    })
                }
                {
                    axes.map((axis) => {
                        const { points } = axis;
                        return (
                            <polyline
                                points={points}
                                class={strokeColor}
                                fill="none"
                                stroke-width={1}
                            />
                        );
                    })
                }
                <path d={shape} class=`${shapeClass}` stroke-width={1.5}></path>
                {
                    properties.map((property, index) => {
                        const length = 1.1;
                        const angle = (Math.PI * 2) * index / chartData.length;
                        const maxX = getX(angle, (length * chartSize) / 2);
                        const maxY = getY(angle, (length * chartSize) / 2);
                        const textAngle = (angle * (180 / Math.PI))
                        return (
                            <g transform={`translate(${maxX},${maxY})`}>
                                <text
                                    x={0}
                                    y={0}
                                fill="#000000"
                                font-size="16px"
                                    text-anchor="middle"
                                    dominant-baseline="middle"
                                    transform=`rotate(${textAngle})`
                                >
                                    {property.name}
                                </text>
                            </g>
                        );
                    })
                }
            </g>
        </svg>
    </div>
</div>