-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arcs): finalize arc link labels
- Loading branch information
Showing
14 changed files
with
381 additions
and
231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,47 @@ | ||
import React from 'react' | ||
import { useTheme } from '@nivo/core' | ||
import { SpringValue, Interpolation, animated } from 'react-spring' | ||
import { DatumWithArcAndColor } from '../types' | ||
|
||
export interface ArcLinkLabelProps<Datum extends DatumWithArcAndColor> { | ||
datum: Datum | ||
label: string | ||
style: { | ||
linkColor: SpringValue<string> | ||
path: Interpolation<string> | ||
thickness: number | ||
textPosition: Interpolation<string> | ||
textAnchor: Interpolation<'start' | 'end'> | ||
linkColor: SpringValue<string> | ||
opacity: SpringValue<number> | ||
path: Interpolation<string> | ||
textColor: SpringValue<string> | ||
} | ||
} | ||
|
||
export const ArcLinkLabel = <Datum extends DatumWithArcAndColor>({ | ||
label, | ||
style, | ||
}: ArcLinkLabelProps<Datum>) => { | ||
const theme = useTheme() | ||
|
||
return ( | ||
<animated.path | ||
fill="none" | ||
stroke={style.linkColor} | ||
strokeWidth={style.thickness} | ||
opacity={style.opacity} | ||
d={style.path} | ||
/> | ||
<animated.g opacity={style.opacity}> | ||
<animated.path | ||
fill="none" | ||
stroke={style.linkColor} | ||
strokeWidth={style.thickness} | ||
d={style.path} | ||
/> | ||
<animated.text | ||
transform={style.textPosition} | ||
textAnchor={style.textAnchor} | ||
dominantBaseline="central" | ||
style={{ | ||
...theme.labels.text, | ||
fill: style.textColor, | ||
}} | ||
> | ||
{label} | ||
</animated.text> | ||
</animated.g> | ||
) | ||
} |
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
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,63 @@ | ||
import { positionFromAngle } from '@nivo/core' | ||
import { Arc, Point } from '../types' | ||
import { getNormalizedAngle } from '../utils' | ||
import { ArcLink } from './types' | ||
|
||
/** | ||
* Compute text anchor for a given arc. | ||
* | ||
* `computeArcLink` already computes a `side`, but when using | ||
* `react-spring`, you cannot have a single interpolation | ||
* returning several output values, so we need to compute | ||
* them in separate interpolations. | ||
*/ | ||
export const computeArcLinkTextAnchor = (arc: Arc): 'start' | 'end' => { | ||
const centerAngle = getNormalizedAngle( | ||
arc.startAngle + (arc.endAngle - arc.startAngle) / 2 - Math.PI / 2 | ||
) | ||
|
||
if (centerAngle < Math.PI / 2 || centerAngle > Math.PI * 1.5) { | ||
return 'start' | ||
} | ||
|
||
return 'end' | ||
} | ||
|
||
/** | ||
* Compute the link of a single arc, returning its points, | ||
* please not that points coordinates are relative to | ||
* the center of the arc. | ||
*/ | ||
export const computeArcLink = ( | ||
arc: Arc, | ||
offset: number, | ||
diagonalLength: number, | ||
straightLength: number | ||
): ArcLink => { | ||
const centerAngle = getNormalizedAngle( | ||
arc.startAngle + (arc.endAngle - arc.startAngle) / 2 - Math.PI / 2 | ||
) | ||
const point0: Point = positionFromAngle(centerAngle, arc.outerRadius + offset) | ||
const point1: Point = positionFromAngle(centerAngle, arc.outerRadius + offset + diagonalLength) | ||
|
||
let side: ArcLink['side'] | ||
let point2: Point | ||
if (centerAngle < Math.PI / 2 || centerAngle > Math.PI * 1.5) { | ||
side = 'after' | ||
point2 = { | ||
x: point1.x + straightLength, | ||
y: point1.y, | ||
} | ||
} else { | ||
side = 'before' | ||
point2 = { | ||
x: point1.x - straightLength, | ||
y: point1.y, | ||
} | ||
} | ||
|
||
return { | ||
side, | ||
points: [point0, point1, point2], | ||
} | ||
} |
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,4 +1,8 @@ | ||
export * from './ArcLinkLabelsLayer' | ||
export * from './canvas' | ||
export * from './compute' | ||
export * from './props' | ||
export * from './types' | ||
export * from './useArcLinkLabels' | ||
export * from './useArcLinkLabelsTransition' | ||
export * from './useArcLinks' |
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,19 @@ | ||
import { DatumWithArc, DatumWithArcAndColor, Point } from '../types' | ||
|
||
export interface ArcLink { | ||
side: 'before' | 'after' | ||
points: [Point, Point, Point] | ||
} | ||
|
||
export interface ArcLinkWithDatum<Datum extends DatumWithArc> extends ArcLink { | ||
data: Datum | ||
} | ||
|
||
export interface ArcLinkLabel<Datum extends DatumWithArcAndColor> extends ArcLinkWithDatum<Datum> { | ||
x: number | ||
y: number | ||
label: string | ||
linkColor: string | ||
textAnchor: 'start' | 'end' | ||
textColor: string | ||
} |
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,76 @@ | ||
import { useCallback } from 'react' | ||
import { PropertyAccessor, usePropertyAccessor, useTheme } from '@nivo/core' | ||
import { InheritedColorConfig, useInheritedColor } from '@nivo/colors' | ||
import { DatumWithArcAndColor } from '../types' | ||
import { ArcLinkWithDatum, ArcLinkLabel } from './types' | ||
import { useArcLinks } from './useArcLinks' | ||
|
||
/** | ||
* Compute arc link labels, please note that the datum should | ||
* contain a color in order to be able to compute the link/label text color. | ||
* | ||
* Please see `useArcLinks` for a more detailed explanation | ||
* about the parameters. | ||
*/ | ||
export const useArcLinkLabels = <Datum extends DatumWithArcAndColor>({ | ||
data, | ||
skipAngle, | ||
offset, | ||
diagonalLength, | ||
straightLength, | ||
textOffset = 0, | ||
label, | ||
linkColor, | ||
textColor, | ||
}: { | ||
data: Datum[] | ||
skipAngle?: number | ||
offset?: number | ||
diagonalLength: number | ||
straightLength: number | ||
textOffset: number | ||
label: PropertyAccessor<Datum, string> | ||
linkColor: InheritedColorConfig<Datum> | ||
textColor: InheritedColorConfig<Datum> | ||
}) => { | ||
const getLabel = usePropertyAccessor<Datum, string>(label) | ||
|
||
const theme = useTheme() | ||
const getLinkColor = useInheritedColor<Datum>(linkColor, theme) | ||
const getTextColor = useInheritedColor<Datum>(textColor, theme) | ||
|
||
const computeExtraProps = useCallback( | ||
(link: ArcLinkWithDatum<Datum>) => { | ||
const position = { | ||
x: link.points[2].x, | ||
y: link.points[2].y, | ||
} | ||
let textAnchor: ArcLinkLabel<Datum>['textAnchor'] | ||
if (link.side === 'before') { | ||
position.x -= textOffset | ||
textAnchor = 'end' | ||
} else { | ||
position.x += textOffset | ||
textAnchor = 'start' | ||
} | ||
|
||
return { | ||
...position, | ||
label: getLabel(link.data), | ||
linkColor: getLinkColor(link.data), | ||
textAnchor, | ||
textColor: getTextColor(link.data), | ||
} | ||
}, | ||
[getLabel, getLinkColor, getTextColor, textOffset] | ||
) | ||
|
||
return useArcLinks<Datum, Omit<ArcLinkLabel<Datum>, keyof ArcLinkWithDatum<Datum>>>({ | ||
data, | ||
skipAngle, | ||
offset, | ||
diagonalLength, | ||
straightLength, | ||
computeExtraProps, | ||
}) | ||
} |
Oops, something went wrong.