-
Notifications
You must be signed in to change notification settings - Fork 23
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
11 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as d3 from 'd3'; | ||
import { data, DataItem } from './data'; | ||
import { useRef } from 'react'; | ||
import styles from './graph.module.css'; | ||
|
||
const MARGIN = 30; | ||
const width = 400; | ||
const height = 400; | ||
const colors = ['#98abc5', '#8a89a6', '#7b6888', '#6b486b', '#a05d56']; | ||
|
||
export const Graph = () => { | ||
const containerRef = useRef(null); | ||
|
||
const radius = Math.min(width, height) / 2 - MARGIN; | ||
|
||
const pieGenerator = d3.pie<any, DataItem>().value((d) => d.value); | ||
const pie = pieGenerator(data); | ||
|
||
const arcPathGenerator = d3.arc(); | ||
const arcs = pie.map((p) => | ||
arcPathGenerator({ | ||
innerRadius: 0, | ||
outerRadius: radius, | ||
startAngle: p.startAngle, | ||
endAngle: p.endAngle, | ||
}) | ||
); | ||
|
||
return ( | ||
<svg width={width} height={height}> | ||
<g transform={`translate(${width / 2}, ${height / 2})`}> | ||
{arcs.map((arc, i) => { | ||
return ( | ||
<path key={i} d={arc} fill={colors[i]} className={styles.slice} /> | ||
); | ||
})} | ||
</g> | ||
</svg> | ||
); | ||
}; |
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,12 @@ | ||
export type DataItem = { | ||
name: string; | ||
value: number; | ||
}; | ||
|
||
export const data: DataItem[] = [ | ||
{ name: "Mark", value: 90 }, | ||
{ name: "Robert", value: 12 }, | ||
{ name: "Emily", value: 34 }, | ||
{ name: "Marion", value: 53 }, | ||
{ name: "Nicolas", value: 98 }, | ||
] |
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,12 @@ | ||
.container .slice { | ||
opacity: 0.8; | ||
cursor: pointer; | ||
} | ||
|
||
.container.hasHighlight .slice { | ||
opacity: 0.2; | ||
} | ||
|
||
.container.hasHighlight .slice:hover { | ||
opacity: 1; | ||
} |
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,6 @@ | ||
// File used to render something in codesandbox only | ||
import ReactDOM from 'react-dom'; | ||
import { Graph } from './Graph'; | ||
|
||
const rootElement = document.getElementById('root'); | ||
ReactDOM.render(<Graph />, rootElement); |
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,29 @@ | ||
{ | ||
"name": "pie-chart-basic", | ||
"version": "1.0.0", | ||
"description": "", | ||
"keywords": [], | ||
"main": "index.js", | ||
"dependencies": { | ||
"react": "17.0.2", | ||
"d3": "7.1.1", | ||
"react-dom": "17.0.2", | ||
"react-scripts": "4.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/runtime": "7.13.8", | ||
"typescript": "4.1.3" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test --env=jsdom", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not ie <= 11", | ||
"not op_mini all" | ||
] | ||
} |
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,60 @@ | ||
import * as d3 from 'd3'; | ||
import { data, DataItem } from './data'; | ||
import { useRef } from 'react'; | ||
import styles from './graph.module.css'; | ||
|
||
const MARGIN = 30; | ||
const width = 400; | ||
const height = 400; | ||
const colors = ['#98abc5', '#8a89a6', '#7b6888', '#6b486b', '#a05d56']; | ||
|
||
export const Graph = () => { | ||
const containerRef = useRef(null); | ||
|
||
const radius = Math.min(width, height) / 2 - MARGIN; | ||
|
||
const pieGenerator = d3.pie<any, DataItem>().value((d) => d.value); | ||
const pie = pieGenerator(data); | ||
|
||
const arcPathGenerator = d3.arc(); | ||
const arcs = pie.map((p) => | ||
arcPathGenerator({ | ||
innerRadius: 0, | ||
outerRadius: radius, | ||
startAngle: p.startAngle, | ||
endAngle: p.endAngle, | ||
}) | ||
); | ||
|
||
return ( | ||
<svg | ||
width={width} | ||
height={height} | ||
ref={containerRef} | ||
className={styles.container} | ||
> | ||
<g transform={`translate(${width / 2}, ${height / 2})`}> | ||
{arcs.map((arc, i) => { | ||
return ( | ||
<path | ||
key={i} | ||
d={arc} | ||
fill={colors[i]} | ||
className={styles.slice} | ||
onMouseEnter={() => { | ||
if (containerRef.current) { | ||
containerRef.current.classList.add(styles.hasHighlight); | ||
} | ||
}} | ||
onMouseLeave={() => { | ||
if (containerRef.current) { | ||
containerRef.current.classList.remove(styles.hasHighlight); | ||
} | ||
}} | ||
/> | ||
); | ||
})} | ||
</g> | ||
</svg> | ||
); | ||
}; |
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,12 @@ | ||
export type DataItem = { | ||
name: string; | ||
value: number; | ||
}; | ||
|
||
export const data: DataItem[] = [ | ||
{ name: "Mark", value: 90 }, | ||
{ name: "Robert", value: 12 }, | ||
{ name: "Emily", value: 34 }, | ||
{ name: "Marion", value: 53 }, | ||
{ name: "Nicolas", value: 98 }, | ||
] |
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,12 @@ | ||
.container .slice { | ||
opacity: 0.8; | ||
cursor: pointer; | ||
} | ||
|
||
.container.hasHighlight .slice { | ||
opacity: 0.2; | ||
} | ||
|
||
.container.hasHighlight .slice:hover { | ||
opacity: 1; | ||
} |
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,6 @@ | ||
// File used to render something in codesandbox only | ||
import ReactDOM from 'react-dom'; | ||
import { Graph } from './Graph'; | ||
|
||
const rootElement = document.getElementById('root'); | ||
ReactDOM.render(<Graph />, rootElement); |
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,29 @@ | ||
{ | ||
"name": "pie-chart-basic", | ||
"version": "1.0.0", | ||
"description": "", | ||
"keywords": [], | ||
"main": "index.js", | ||
"dependencies": { | ||
"react": "17.0.2", | ||
"d3": "7.1.1", | ||
"react-dom": "17.0.2", | ||
"react-scripts": "4.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/runtime": "7.13.8", | ||
"typescript": "4.1.3" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test --env=jsdom", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not ie <= 11", | ||
"not op_mini all" | ||
] | ||
} |