2023-05-31 22:10:59 +00:00
|
|
|
'use client';
|
2023-06-03 01:01:38 +00:00
|
|
|
import { Ethics, EthicsDataG15, Scale } from '../../types/stellaris';
|
2023-05-31 22:10:59 +00:00
|
|
|
import { Radar } from 'react-chartjs-2'
|
2023-06-01 16:55:36 +00:00
|
|
|
import { Chart as ChartJS, registerables } from 'chart.js';
|
2023-05-31 22:10:59 +00:00
|
|
|
import useSWR from 'swr';
|
|
|
|
|
|
|
|
const fetcher = async (url:any) => await fetch(url).then((res) => res.json());
|
|
|
|
|
2023-06-01 16:55:36 +00:00
|
|
|
export const RadarChart = (props: { weighted: boolean }) => {
|
2023-06-03 01:01:38 +00:00
|
|
|
const {data: tmpData} = useSWR('/api/ethics',fetcher);
|
2023-05-31 22:10:59 +00:00
|
|
|
|
|
|
|
const parseData = (data: Array<Array<any>>) => {
|
2023-06-03 01:01:38 +00:00
|
|
|
let parsedData: EthicsDataG15[] = new Array<EthicsDataG15>;
|
|
|
|
Object.keys(Ethics).forEach((ethic: number | string) => {
|
|
|
|
if (Number.isInteger(Number(ethic))) {
|
|
|
|
parsedData[Number(ethic)] = new EthicsDataG15(data[Number(ethic)])
|
|
|
|
}
|
|
|
|
});
|
2023-05-31 22:10:59 +00:00
|
|
|
return parsedData;
|
|
|
|
}
|
2023-06-03 01:01:38 +00:00
|
|
|
let ethicsData: EthicsDataG15[];
|
2023-05-31 22:10:59 +00:00
|
|
|
let data;
|
|
|
|
let tEntries = 0;
|
|
|
|
let scaleLimit = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (tmpData) {
|
|
|
|
let sheetData = tmpData.sheetData
|
|
|
|
ethicsData = parseData(sheetData);
|
2023-06-03 01:01:38 +00:00
|
|
|
|
|
|
|
let labelsArray = [ Ethics.Egalitarian, Ethics.Militarist, Ethics.Xenophobe,
|
|
|
|
Ethics.Competitive, Ethics.Elitist, Ethics.Materialist, Ethics.Ecologist,
|
|
|
|
Ethics.Authoritarian, Ethics.Pacifist, Ethics.Xenophile,
|
|
|
|
Ethics.Cooperative, Ethics.Pluralist, Ethics.Spiritualist, Ethics.Industrialist]
|
|
|
|
|
|
|
|
let sumArray = new Array<number>
|
|
|
|
let sumRegularArray = new Array<number>
|
|
|
|
|
|
|
|
labelsArray.forEach((ethic: Ethics, index: number) => {
|
|
|
|
sumArray[index] = ethicsData[ethic].sum(props.weighted);
|
|
|
|
sumRegularArray[index] = ethicsData[ethic].sumRegular();
|
|
|
|
})
|
|
|
|
|
2023-05-31 22:10:59 +00:00
|
|
|
data = {
|
2023-06-03 01:01:38 +00:00
|
|
|
labels: labelsArray.map(ethic => Ethics[ethic]),
|
2023-05-31 22:10:59 +00:00
|
|
|
datasets: [{
|
2023-06-01 16:55:36 +00:00
|
|
|
label: "Total Ethics",
|
2023-06-03 01:01:38 +00:00
|
|
|
data: sumArray,
|
2023-05-31 22:10:59 +00:00
|
|
|
fill: true,
|
|
|
|
backgroundColor: '#254A6FAA',
|
|
|
|
borderColor: '#356A9F',
|
2023-06-01 16:55:36 +00:00
|
|
|
radius: 3
|
|
|
|
}, {
|
|
|
|
label: "Regular Ethics",
|
2023-06-03 01:01:38 +00:00
|
|
|
data: sumRegularArray,
|
2023-06-01 16:55:36 +00:00
|
|
|
fill: true,
|
2023-06-01 17:00:33 +00:00
|
|
|
backgroundColor: '#000000AA',
|
2023-06-01 16:55:36 +00:00
|
|
|
borderColor: '#254A6F',
|
|
|
|
radius: 3
|
2023-05-31 22:10:59 +00:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ethicsData.forEach(elem => {
|
2023-06-03 01:01:38 +00:00
|
|
|
tEntries = tEntries + elem.sum(false);
|
2023-06-01 16:55:36 +00:00
|
|
|
if (elem.sum(props.weighted) >= scaleLimit) {
|
|
|
|
scaleLimit = elem.sum(props.weighted) + 1;
|
2023-05-31 22:10:59 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = {
|
|
|
|
labels: [],
|
|
|
|
datasets: [{
|
|
|
|
data: [],
|
|
|
|
fill: true,
|
|
|
|
backgroundColor: '#254A6FAA',
|
|
|
|
borderColor: '#356A9F',
|
2023-06-01 16:55:36 +00:00
|
|
|
radius: 3
|
2023-05-31 22:10:59 +00:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
type: 'radar',
|
|
|
|
data: data,
|
|
|
|
options: {
|
2023-06-01 16:55:36 +00:00
|
|
|
plugins: {
|
|
|
|
tooltip: {
|
|
|
|
callbacks: {
|
|
|
|
label: (ctx: any) => (`${ctx.dataset.label}: ${ctx.dataset.data[ctx.dataIndex]} ${props.weighted ? "Points" : "Picks"}`)
|
|
|
|
}
|
2023-06-02 07:44:34 +00:00
|
|
|
},
|
|
|
|
datalabels: {
|
|
|
|
display: false,
|
|
|
|
},
|
2023-06-01 16:55:36 +00:00
|
|
|
},
|
|
|
|
legend: { display: true },
|
2023-05-31 22:10:59 +00:00
|
|
|
elements: {
|
|
|
|
line: {
|
|
|
|
borderWidth: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
r: {
|
|
|
|
max: scaleLimit,
|
|
|
|
min: 0,
|
|
|
|
ticks: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: 'grey'
|
|
|
|
},
|
|
|
|
angleLines: {
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-06-01 16:55:36 +00:00
|
|
|
ChartJS.register( ...registerables )
|
2023-05-31 22:10:59 +00:00
|
|
|
|
|
|
|
return (
|
2023-06-01 23:41:58 +00:00
|
|
|
<div className="chart-container h-full aspect-square p-2">
|
2023-05-31 22:10:59 +00:00
|
|
|
<Radar {...config} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|