Skip to content

Instantly share code, notes, and snippets.

@yansusanto
Created March 13, 2023 01:37
Show Gist options
  • Save yansusanto/c5ff0932621465d69175b50fba5e308f to your computer and use it in GitHub Desktop.
Save yansusanto/c5ff0932621465d69175b50fba5e308f to your computer and use it in GitHub Desktop.
const data = [
{
name: 'Cluster 1',
value: 210,
},
{
name: 'Cluster 2',
value: 30,
},
{
name: 'Cluster 3',
value: 180,
},
{
name: 'Cluster 4',
value: 260,
},
{
name: 'Cluster 5',
value: 60,
},
].sort((a, b) => a.value - b.value);
const total_value = data.reduce((a, b) => a + b.value, 0);
// => 740
const convertToPercent = (num) => Math.round((num / total_value) * 100);
// convertToPercent(210) => 28
const convertToDegrees = (num) => Math.round((num / 100) * 360);
// convertToDegrees(28) => 101
@yansusanto
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment