Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversed curved label #2797

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3a86384
add textPath
acharyakavita Jan 30, 2024
78acb71
Merge branch 'main' of https://github.com/FormidableLabs/victory into…
acharyakavita Jan 30, 2024
75737d0
wip
acharyakavita Feb 1, 2024
c06b3d0
wip
acharyakavita Feb 2, 2024
202853e
Merge branch 'main' of https://github.com/FormidableLabs/victory into…
acharyakavita Feb 2, 2024
bb80fbc
working version
acharyakavita Feb 2, 2024
4dc49b5
working version 2
acharyakavita Feb 2, 2024
3ecaaf5
working version 3
acharyakavita Feb 2, 2024
294af7b
working version 4
acharyakavita Feb 2, 2024
2707e1c
working version 5
acharyakavita Feb 2, 2024
1d5dbff
add startOffset
acharyakavita Feb 5, 2024
a7fd60b
fix lint
acharyakavita Feb 5, 2024
994de07
fix key error
acharyakavita Feb 6, 2024
a19f484
fix key issue
acharyakavita Feb 6, 2024
eec1236
reversed label
acharyakavita Feb 7, 2024
19dda4a
reverse label
acharyakavita Feb 7, 2024
2b32e1b
update documentation
acharyakavita Feb 7, 2024
8d7bd34
lint and format fix
acharyakavita Feb 7, 2024
d2231a1
lint fix
acharyakavita Feb 7, 2024
e0caab0
lint fix
acharyakavita Feb 7, 2024
6454829
fix conflict
acharyakavita Feb 7, 2024
1df4838
update tests and documentation
acharyakavita Feb 7, 2024
0598e12
update tests and documentation
acharyakavita Feb 7, 2024
fd6a4f5
fix lint and test case
acharyakavita Feb 7, 2024
3c44cb0
fix types
acharyakavita Feb 7, 2024
36ff240
fix default values
acharyakavita Feb 8, 2024
21e7060
fix types
acharyakavita Feb 8, 2024
866bc2e
remove id from textprops
acharyakavita Feb 8, 2024
00d4e57
fix types
acharyakavita Feb 8, 2024
53793f4
Merge branch 'main' of https://github.com/FormidableLabs/victory into…
acharyakavita Feb 9, 2024
1155ef1
fix undefined error
acharyakavita Feb 9, 2024
b435687
fix format
acharyakavita Feb 9, 2024
900a38e
resolve conflicts
acharyakavita Feb 9, 2024
9d31d8e
reverse label arc
acharyakavita Feb 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 38 additions & 1 deletion demo/js/components/victory-pie-demo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-magic-numbers,react/no-multi-comp*/
import { random, range } from "lodash";
import React from "react";
import { VictoryPie } from "victory-pie";
import { VictoryPie, CurvedLabel } from "victory-pie";
import { VictoryTooltip } from "victory-tooltip";
import { VictoryTheme, LineSegment } from "victory-core";

Expand Down Expand Up @@ -331,6 +331,43 @@ export default class App extends React.Component {
labelIndicatorInnerOffset={35}
labelIndicatorOuterOffset={4}
/>
<VictoryPie
style={{ ...parentStyle, labels: { fill: "magenta" } }}
radius={100}
labelPlacement="curved"
labels={({ datum }) => `${datum.l}`}
labelRadius={85}
startOffset={30}
data={[
{ x: 1, y: 1, l: 0 },
{ x: 2, y: 1, l: 45 },
{ x: 3, y: 1, l: 90 },
{ x: 4, y: 1, l: 135 },
{ x: 5, y: 1, l: 180 },
{ x: 6, y: 1, l: 225 },
{ x: 7, y: 1, l: 270 },
{ x: 8, y: 1, l: 315 },
]}
/>
<VictoryPie
style={{ ...parentStyle, labels: { fill: "magenta" } }}
radius={100}
labelPlacement="curved"
labels={({ datum }) => `${datum.l} deg`}
labelRadius={115}
startOffset={10}
curvedLabelComponent = {<CurvedLabel style={{ fill: "red" }}/>}
data={[
{ x: 1, y: 1, l: 0 },
{ x: 2, y: 1, l: 45 },
{ x: 3, y: 1, l: 90 },
{ x: 4, y: 1, l: 135 },
{ x: 5, y: 1, l: 180 },
{ x: 6, y: 1, l: 225 },
{ x: 7, y: 1, l: 270 },
{ x: 8, y: 1, l: 315 },
]}
/>
</div>
</div>
);
Expand Down
39 changes: 38 additions & 1 deletion demo/ts/components/victory-pie-demo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { random, range } from "lodash";
import { VictoryPie } from "victory-pie";
import { VictoryPie, CurvedLabel } from "victory-pie";
import { VictoryTooltip } from "victory-tooltip";
import { VictoryTheme, LineSegment } from "victory-core";

Expand Down Expand Up @@ -320,6 +320,43 @@ export default class VictoryPieDemo extends React.Component<
labelIndicatorInnerOffset={45}
labelIndicatorOuterOffset={15}
/>
<VictoryPie
style={{ parent: parentStyle, labels: { fill: "magenta" } }}
radius={100}
labelPlacement="curved"
labels={({ datum }) => `${datum.l}`}
labelRadius={85}
startOffset={30}
data={[
{ x: 1, y: 1, l: 0 },
{ x: 2, y: 1, l: 45 },
{ x: 3, y: 1, l: 90 },
{ x: 4, y: 1, l: 135 },
{ x: 5, y: 1, l: 180 },
{ x: 6, y: 1, l: 225 },
{ x: 7, y: 1, l: 270 },
{ x: 8, y: 1, l: 315 },
]}
/>
<VictoryPie
style={{ parent: parentStyle, labels: { fill: "magenta" } }}
radius={100}
labelPlacement="curved"
labels={({ datum }) => `${datum.l} deg`}
curvedLabelComponent={<CurvedLabel style={{ fill: "red" }}/>}
labelRadius={115}
startOffset={10}
data={[
{ x: 1, y: 1, l: 0 },
{ x: 2, y: 1, l: 45 },
{ x: 3, y: 1, l: 90 },
{ x: 4, y: 1, l: 135 },
{ x: 5, y: 1, l: 180 },
{ x: 6, y: 1, l: 225 },
{ x: 7, y: 1, l: 270 },
{ x: 8, y: 1, l: 315 },
]}
/>
</div>
</div>
);
Expand Down
80 changes: 77 additions & 3 deletions docs/src/content/docs/victory-pie.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ The `innerRadius` prop determines the number of pixels between the center of the

`type: element`

`VictoryPie` uses the standard `labelComponent` prop. [Read about it here](/docs/common-props#labelcomponent)
`VictoryPie` uses the standard `labelComponent` prop for standard labels. [Read about it here](/docs/common-props#labelcomponent)

For curved labels use `curvedLabelComponent` prop with `labelPlacement="curved"`.
[Read about it here](/docs/victory-pie#curvedLabelComponent)

_default:_ `<VictoryLabel/>`

Expand All @@ -252,9 +255,11 @@ _default:_ `<VictoryLabel/>`

## labelPlacement

`type "parallel" || "perpendicular" || "vertical" || function`
`type "parallel" || "perpendicular" || "vertical" || "curved" || function`

The `labelPlacement` prop specifies the angular placement of each label relative to the angle of its corresponding slice. This prop should be given as "parallel", "perpendicular", "vertical", "curved" or as a function that returns one of these values. When this prop is not given, the label will be placed vertically.

The `labelPlacement` prop specifies the angular placement of each label relative to the angle of its corresponding slice. This prop should be given as "parallel", "perpendicular", "vertical", or as a function that returns one of these values. When this prop is not given, the label will be placed vertically.
we need to use `curvedLabelComponent` prop.[Read about it here](/docs/victory-pie#curvedLabelComponent) to style and update accessibility related props when using `labelPlacement = "curved"`. We cannot use `VictoryLabel` for curved labels.

```playground
<VictoryPie
Expand All @@ -271,6 +276,16 @@ The `labelPlacement` prop specifies the angular placement of each label relative
/>
```

```playground
<VictoryPie
data={sampleData}
labels={({ datum }) => `y: ${datum.y}`}
labelPlacement={"curved"}
startOffset={30}
radius={120}
labelRadius={100}
/>
```
## labelPosition

`type "startAngle" || "endAngle" || "centroid" || function`
Expand Down Expand Up @@ -571,6 +586,65 @@ The `labelIndicatorOuterOffset` prop defines the offset by which the indicator l
/>
```

## curvedLabelComponent

`type: element`

The `curvedLabelComponent` prop is used to pass `<CurvedLabel/>` component to modify styles and accessibility parameters for curved labels.
Currently it supports following props of `VictoryLabel`.
`active`,`capHeight`,`className`,`data`,`datum`,`desc`,`events`,`lineHeight`,`renderInPortal`,`style`,`tabIndex`,`text`,`textComponent`
[Read about it here](/docs/docs/victory-label)

```playground
<VictoryPie
data={sampleData}
labels={({ datum }) => `${datum.l} deg`}
labelPlacement={"curved"}
startOffset={30}
radius={120}
labelRadius={100}
curvedLabelComponent={<CurvedLabel style={{ fill: "red" }}/>}
data={[
{ x: 1, y: 1, l: 0 },
{ x: 2, y: 1, l: 45 },
{ x: 3, y: 1, l: 90 },
{ x: 4, y: 1, l: 135 },
{ x: 5, y: 1, l: 180 },
{ x: 6, y: 1, l: 225 },
{ x: 7, y: 1, l: 270 },
{ x: 8, y: 1, l: 315 },
]}
/>
```

## startOffset

`type: number`

The `startOffset` prop is used along with `labelPlacement="curved"` type.
The startOffset attribute defines an offset from the start of the path current text position along the path.This will help us to center the curved labels

```playground
<VictoryPie
data={sampleData}
labels={({ datum }) => `${datum.l} deg`}
labelPlacement={"curved"}
startOffset={30}
radius={120}
labelRadius={100}
data={[
{ x: 1, y: 1, l: 0 },
{ x: 2, y: 1, l: 45 },
{ x: 3, y: 1, l: 90 },
{ x: 4, y: 1, l: 135 },
{ x: 5, y: 1, l: 180 },
{ x: 6, y: 1, l: 225 },
{ x: 7, y: 1, l: 270 },
{ x: 8, y: 1, l: 315 },
]}
/>
```

[animations guide]: /guides/animations
[data accessors guide]: /guides/data-accessors
[custom components guide]: /guides/custom-components
Expand Down
14 changes: 14 additions & 0 deletions docs/src/content/docs/victory-primitives.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ Used by `VictoryLabel`
const TSpan = (props) => <tspan {...props} />;
```

### TextPath

Used by `curvedLabelComponent` in VictoryPie.

```jsx
const TextPath = (props) => {
const { children, href, startOffset, ...rest } = props;
return (
<textPath href={href} startOffset={startOffset}>
{children}
</text>
);
```

## Simple Components

### Arc
Expand Down
2 changes: 2 additions & 0 deletions packages/victory-core/src/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
TSpan,
Text,
TextAnchorType,
TextPath,
TextProps,
TextSize,
TextSizeStyleInterface,
Expand Down Expand Up @@ -177,6 +178,7 @@ describe("victory-core", () => {
"Style",
"TSpan",
"Text",
"TextPath",
"TextSize",
"Timer",
"TimerContext",
Expand Down
1 change: 1 addition & 0 deletions packages/victory-core/src/victory-primitives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./path";
export * from "./point";
export * from "./rect";
export * from "./text";
export * from "./text-path";
export * from "./tspan";
export * from "./types";
export * from "./whisker";
24 changes: 24 additions & 0 deletions packages/victory-core/src/victory-primitives/text-path.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import { evaluateProp } from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";

export interface TextPathProps extends VictoryCommonPrimitiveProps {
children?: React.ReactNode;
href?: string;
startOffset?: number;
}

export const TextPath = (props: TextPathProps) => {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars --
* origin conflicts with the SVG element's origin attribute
*/
const { href, startOffset, children, id, tabIndex, origin, ...rest } = props;

const svgProps: React.SVGProps<SVGTextPathElement> = {
href: evaluateProp(href, props),
startOffset: evaluateProp(startOffset, props),
...rest,
};

return <textPath {...svgProps}>{children}</textPath>;
};