Skip to content

Commit

Permalink
added PieChart
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeystylle committed Jun 18, 2024
1 parent 1e73e66 commit b13398f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/PieChart/PieChart.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const getDegreeRotation = (percentage: number): number => {
// Normalize the percentage to be within 0 to 100
const clampedPercentage = Math.min(100, Math.max(0, percentage));
// Convert the percentage to degrees
let degrees = clampedPercentage * 3.6;
// If the degrees exceed 180, subtract 180 from it
if (degrees > 180) {
degrees = degrees - 180;
}
return degrees;
};
45 changes: 45 additions & 0 deletions components/PieChart/PieChart.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Meta, StoryObj } from '@storybook/react';

import PieChart from './PieChart';

const meta: Meta<typeof PieChart> = {
title: 'Components/PieChart',
component: PieChart,
tags: ['autodocs'],
parameters: {
layout: 'centered',
},
};

export default meta;
type Story = StoryObj<typeof PieChart>;

export const Default: Story = {
args: {
fillPercentage: 13,
},
};

export const Quarter: Story = {
args: {
fillPercentage: 25,
},
};

export const Half: Story = {
args: {
fillPercentage: 50,
},
};

export const Full: Story = {
args: {
fillPercentage: 100,
},
};

export const Empy: Story = {
args: {
fillPercentage: 0,
},
};
35 changes: 35 additions & 0 deletions components/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { getDegreeRotation } from './PieChart.helpers';
import { cn } from '@/lib/utils';

type PieChartProps = {
/**
* percentage of the pie chart , from 1 to 100
*/
fillPercentage: number;
};

const PieChart = (props: PieChartProps) => {
const { fillPercentage } = props;

// Define the degree circle rotation class with conditional coloring
const degreeCircle2 = cn(
'absolute top-0 h-1/2 w-full bg-gray-300 origin-[center_bottom] ',
{
'bg-blue-600': fillPercentage > 50,
'bg-gray-300': fillPercentage <= 50,
}
);

return (
<div className="bg-gray-300 rounded-full relative overflow-hidden h-[192px] w-[192px] ">
<div className="absolute top-0 h-1/2 w-full bg-blue-600 origin-[center_bottom] " />
<div
className={degreeCircle2}
style={{ transform: `rotate(${getDegreeRotation(fillPercentage)}deg)` }}
/>
</div>
);
};

export default PieChart;
Empty file added components/PieChart/index.tsx
Empty file.

0 comments on commit b13398f

Please sign in to comment.