-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
129 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { clearCanvas, scaleCanvas } from '@/utils'; | ||
|
||
export interface PieItemConfig { | ||
value: number; | ||
color: string; | ||
} | ||
|
||
export interface IPieChartProps { | ||
radius: number; | ||
data: PieItemConfig[]; | ||
} | ||
|
||
const PieChart: React.FC<Readonly<IPieChartProps>> = (props) => { | ||
const { radius, data } = props; | ||
|
||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
const ctxRef = useRef<CanvasRenderingContext2D | null>(); | ||
|
||
useEffect(() => { | ||
if (!ctxRef.current) { | ||
ctxRef.current = scaleCanvas(canvasRef.current!); | ||
|
||
clearCanvas(ctxRef.current); | ||
} | ||
|
||
const ctx = ctxRef.current; | ||
|
||
const circle = Math.PI * 2; | ||
|
||
const total = data.reduce((p, c) => p + c.value, 0); | ||
|
||
let ratio = 0; | ||
for (let i = 0; i < data.length; i++) { | ||
const sector = data[i]; | ||
|
||
const nextRatio = circle * (sector.value / total || 0); | ||
|
||
ctx.beginPath(); | ||
ctx.moveTo(radius, radius); | ||
ctx.arc(radius, radius, radius, ratio, nextRatio); | ||
ctx.fillStyle = sector.color; | ||
ctx.fill(); | ||
|
||
ratio = nextRatio; | ||
} | ||
}, [radius, data]); | ||
|
||
const size = radius * 2; | ||
|
||
return <canvas ref={canvasRef} width={size} height={size}></canvas>; | ||
}; | ||
|
||
export default PieChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export { default as Canvas } from './Canvas/Canvas'; | ||
export * from './Canvas/Canvas'; | ||
export { default as ContextMenu } from './ContextMenu/ContextMenu'; | ||
export { default as Dialog } from './Dialog/Dialog'; | ||
export * from './Dialog/Dialog'; | ||
export { default as Icon } from './Icon/Icon'; | ||
export { default as Image } from './Image/Image'; | ||
export { default as Loading } from './Loading/Loading'; | ||
export { default as PieChart } from './PieChart/PieChart'; | ||
export * from './PieChart/PieChart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export const scaleCanvas = (canvas: HTMLCanvasElement) => { | ||
const ctx = canvas.getContext('2d')!; | ||
|
||
const w = canvas.width; | ||
const h = canvas.height; | ||
|
||
const ratio = window.devicePixelRatio || 1; | ||
canvas.width = w * ratio; | ||
canvas.height = h * ratio; | ||
canvas.style.width = `${w}px`; | ||
canvas.style.height = `${h}px`; | ||
|
||
ctx.imageSmoothingEnabled = true; | ||
ctx.imageSmoothingQuality = 'high'; | ||
ctx.scale(ratio, ratio); | ||
|
||
return ctx; | ||
}; | ||
|
||
export const clearCanvas = (ctx: CanvasRenderingContext2D) => { | ||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters