1
0
Fork 0
This repository has been archived on 2023-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
chellaris-galaxy-political-.../components/charts/radar.tsx

134 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2023-05-31 22:10:59 +00:00
'use client';
import '@/app/globals.css';
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'
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());
export const RadarChart = (props: { weighted: boolean }) => {
const {data: tmpData} = useSWR('/api/v1/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: [{
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',
radius: 3
}, {
label: "Regular Ethics",
2023-06-03 01:01:38 +00:00
data: sumRegularArray,
fill: true,
2023-06-01 17:00:33 +00:00
backgroundColor: '#000000AA',
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);
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',
radius: 3
2023-05-31 22:10:59 +00:00
}]
}
}
const config = {
type: 'radar',
data: data,
options: {
plugins: {
tooltip: {
callbacks: {
label: (ctx: any) => (`${ctx.dataset.label}: ${ctx.dataset.data[ctx.dataIndex]} ${props.weighted ? "Points" : "Picks"}`)
}
},
datalabels: {
display: false,
},
},
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'
}
}
}
},
};
ChartJS.register( ...registerables )
2023-05-31 22:10:59 +00:00
return (
<div className='chart-container'>
<Radar className=' chart' {...config} />
2023-05-31 22:10:59 +00:00
</div>
);
};