-
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
Holtz Yan
authored and
Holtz Yan
committed
Jul 28, 2023
1 parent
035cbec
commit 9362cfd
Showing
22 changed files
with
846 additions
and
266 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,18 +1,18 @@ | ||
import { data } from "./data"; | ||
import { Radar } from "./Radar"; | ||
import { data } from './data'; | ||
import { Radar } from './Radar'; | ||
|
||
export const RadarBasicDemo = ({ width = 700, height = 400 }) => ( | ||
<Radar | ||
data={data} | ||
width={width} | ||
height={height} | ||
axisConfig={[ | ||
{ name: "speed", max: 10 }, | ||
{ name: "acceleration", max: 10 }, | ||
{ name: "conso", max: 10 }, | ||
{ name: "safety", max: 2 }, | ||
{ name: "style", max: 1000 }, | ||
{ name: "price", max: 100 }, | ||
{ name: 'speed', max: 10 }, | ||
{ name: 'acceleration', max: 10 }, | ||
{ name: 'conso', max: 10 }, | ||
{ name: 'safety', max: 2 }, | ||
{ name: 'style', max: 1000 }, | ||
{ name: 'price', max: 100 }, | ||
]} | ||
/> | ||
); |
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,95 @@ | ||
import { Variable } from './data'; | ||
import { polarToCartesian } from './polarToCartesian'; | ||
import * as d3 from 'd3'; | ||
|
||
// | ||
// Constants | ||
// | ||
export const INNER_RADIUS = 40; | ||
const GRID_NUMBER = 5; | ||
const GRID_COLOR = 'lightGrey'; | ||
|
||
// | ||
// Types | ||
// | ||
export type AxisConfig = { | ||
name: Variable; | ||
max: number; | ||
}; | ||
|
||
type RadarGridProps = { | ||
outerRadius: number; | ||
xScale: d3.ScaleBand<string>; | ||
axisConfig: AxisConfig[]; | ||
}; | ||
|
||
/* | ||
A react component that adds a grid background | ||
for a radar chart in polar coordinates | ||
*/ | ||
export const RadarGrid = ({ | ||
outerRadius, | ||
xScale, | ||
axisConfig, | ||
}: RadarGridProps) => { | ||
const lineGenerator = d3.lineRadial(); | ||
|
||
// Compute Axes = from center to outer | ||
const allAxes = axisConfig.map((axis, i) => { | ||
const angle = xScale(axis.name); | ||
|
||
if (angle === undefined) { | ||
return null; | ||
} | ||
|
||
const path = lineGenerator([ | ||
[angle, INNER_RADIUS], | ||
[angle, outerRadius], | ||
]); | ||
|
||
const labelPosition = polarToCartesian( | ||
angle - Math.PI / 2, | ||
outerRadius + 10 | ||
); | ||
|
||
return ( | ||
<g key={i}> | ||
<path d={path} stroke={GRID_COLOR} strokeWidth={0.5} rx={1} /> | ||
<text | ||
x={labelPosition.x} | ||
y={labelPosition.y} | ||
fontSize={12} | ||
fill={GRID_COLOR} | ||
textAnchor={labelPosition.x > 0 ? 'start' : 'end'} | ||
dominantBaseline="middle" | ||
> | ||
{axis.name} | ||
</text> | ||
</g> | ||
); | ||
}); | ||
|
||
// Compte grid = concentric circles | ||
const allCircles = [...Array(GRID_NUMBER).keys()].map((position, i) => { | ||
return ( | ||
<circle | ||
key={i} | ||
cx={0} | ||
cy={0} | ||
r={ | ||
INNER_RADIUS + | ||
(position * (outerRadius - INNER_RADIUS)) / (GRID_NUMBER - 1) | ||
} | ||
stroke={GRID_COLOR} | ||
fill="none" | ||
/> | ||
); | ||
}); | ||
|
||
return ( | ||
<g> | ||
{allAxes} | ||
{allCircles} | ||
</g> | ||
); | ||
}; |
Oops, something went wrong.