react-minimal-pie-chart 8.4.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -12
- package/{types → dist}/Chart/Chart.d.ts +5 -4
- package/{types → dist}/Chart/renderLabels.d.ts +2 -2
- package/{types → dist}/Chart/renderSegments.d.ts +2 -2
- package/{types → dist}/Label.d.ts +3 -2
- package/{types → dist}/Path.d.ts +3 -2
- package/dist/commonTypes.d.ts +17 -0
- package/dist/index.cjs.js +247 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +244 -0
- package/dist/index.esm.js.map +1 -0
- package/{types → dist}/utils.d.ts +4 -2
- package/package.json +52 -92
- package/dist/index.js +0 -356
- package/dist/index.js.map +0 -1
- package/es/index.js +0 -349
- package/es/index.js.map +0 -1
- package/lib/index.js +0 -352
- package/lib/index.js.map +0 -1
- package/types/commonTypes.d.ts +0 -17
- /package/{types → dist}/Chart/extendData.d.ts +0 -0
- /package/{types → dist}/Chart/index.d.ts +0 -0
- /package/{types → dist}/index.d.ts +0 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[![Coveralls][coveralls-badge]][coveralls]
|
|
6
6
|
[![Bundle size][bundlephobia-badge]][bundlephobia]
|
|
7
7
|
|
|
8
|
-
Lightweight React **SVG pie charts**, with **versatile options** and **CSS animation** included.
|
|
8
|
+
Lightweight React **SVG pie charts**, with **versatile options** and **CSS animation** included. **~2kB** gzipped. [👏 Demo 👏][storybook].
|
|
9
9
|
|
|
10
10
|
<p align="center">
|
|
11
11
|
<img
|
|
@@ -17,11 +17,11 @@ Lightweight React **SVG pie charts**, with **versatile options** and **CSS anima
|
|
|
17
17
|
|
|
18
18
|
## Why?
|
|
19
19
|
|
|
20
|
-
Because [Recharts][recharts-github] is awesome, but when you just need a simple pie/donought chart,
|
|
20
|
+
Because [Recharts][recharts-github] is awesome, but when you just need a simple pie/donought chart, **2kB** are usually enough.
|
|
21
21
|
|
|
22
22
|
| | Size<br>by Bundlefobia | Benchmark Size \* | Loading time<br>on a slow 3g \* |
|
|
23
23
|
| :----------------------------------------------------: | :-----------------------------------------------------------------------------------------------------: | :---------------: | :-----------------------------: |
|
|
24
|
-
| react-minimal-pie-chart (
|
|
24
|
+
| react-minimal-pie-chart (_v9.0.0_) | [![Bundle size: React minimal pie chart][bundlephobia-badge]][bundlephobia] | 1.99 KB | ~40 ms |
|
|
25
25
|
| [rechart][recharts-github] (_v1.8.5_) | [![Bundle size: Recharts][recharts-bundlephobia-badge]][recharts-bundlephobia] | 96.9 KB | ~1900 ms |
|
|
26
26
|
| [victory-pie][victory-pie-github] (_v34.1.3_) | [![Bundle size: Victory pie][victory-pie-bundlephobia-badge]][victory-pie-bundlephobia] | 50.5 KB | ~1100 ms |
|
|
27
27
|
| [react-apexcharts][react-apexcharts-github] (_v1.3.7_) | [![Bundle size: React apec charts][react-apexcharts-bundlephobia-badge]][react-apexcharts-bundlephobia] | 114.6 KB | ~2300 ms |
|
|
@@ -174,17 +174,17 @@ Render segment's percentage as labels:
|
|
|
174
174
|
label={({ dataEntry }) => `${Math.round(dataEntry.percentage)} %`}
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
-
See examples in the [demo source]
|
|
177
|
+
See examples in the [demo source](https://github.com/toomuchdesign/react-minimal-pie-chart/blob/v8.2.0/stories/index.tsx#L81).
|
|
178
178
|
|
|
179
179
|
## How to
|
|
180
180
|
|
|
181
181
|
### User interactions with the chart
|
|
182
182
|
|
|
183
|
-
See [demo]
|
|
183
|
+
See [demo](https://toomuchdesign.github.io/react-minimal-pie-chart/index.html?path=/story/example-interaction--click-mouseover-mouseout-callbacks) and relative source [here](https://github.com/toomuchdesign/react-minimal-pie-chart/blob/v8.0.0/stories/InteractionStory.tsx) and [here](https://github.com/toomuchdesign/react-minimal-pie-chart/blob/v8.0.0/stories/InteractionTabStory.tsx).
|
|
184
184
|
|
|
185
185
|
### Custom tooltip
|
|
186
186
|
|
|
187
|
-
See [demo]
|
|
187
|
+
See [demo](https://toomuchdesign.github.io/react-minimal-pie-chart/index.html?path=/story/example-misc--tooltip-integration) and [relative source](https://github.com/toomuchdesign/react-minimal-pie-chart/blob/master/stories/components/Tooltip.tsx).
|
|
188
188
|
|
|
189
189
|
## Browsers support
|
|
190
190
|
|
|
@@ -278,12 +278,6 @@ Thanks to you all ([emoji key](https://github.com/kentcdodds/all-contributors#em
|
|
|
278
278
|
[react-vis-bundlephobia]: https://bundlephobia.com/result?p=react-vis
|
|
279
279
|
[react-vis-github]: https://github.com/uber/react-vis
|
|
280
280
|
[storybook]: https://toomuchdesign.github.io/react-minimal-pie-chart/index.html
|
|
281
|
-
[demo-interaction]: https://toomuchdesign.github.io/react-minimal-pie-chart/index.html?path=/story/interaction--click-mouseover-mouseout-callbacks
|
|
282
|
-
[demo-interaction-source]: https://github.com/toomuchdesign/react-minimal-pie-chart/blob/v8.0.0/stories/InteractionStory.tsx
|
|
283
|
-
[demo-interaction-2-source]: https://github.com/toomuchdesign/react-minimal-pie-chart/blob/v8.0.0/stories/InteractionTabStory.tsx
|
|
284
|
-
[demo-tooltip]: https://toomuchdesign.github.io/react-minimal-pie-chart/index.html?path=/story/misc--tooltip-integration
|
|
285
|
-
[demo-tooltip-source]: https://github.com/toomuchdesign/react-minimal-pie-chart/blob/master/stories/Tooltip.tsx
|
|
286
|
-
[demo-label-source]: https://github.com/toomuchdesign/react-minimal-pie-chart/blob/v8.2.0/stories/index.tsx#L81
|
|
287
281
|
[data-props-docs]: #about-data-prop
|
|
288
282
|
[label-props-docs]: #custom-labels-with-label-render-prop
|
|
289
283
|
[svg-elements]: https://developer.mozilla.org/en-US/docs/Web/SVG/Element
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import type { CSSProperties, FocusEvent, KeyboardEvent, MouseEvent, ReactNode } from 'react';
|
|
2
3
|
import type { Data, BaseDataEntry, LabelRenderFunction } from '../commonTypes';
|
|
3
|
-
export
|
|
4
|
+
export type Props<DataEntry extends BaseDataEntry = BaseDataEntry> = {
|
|
4
5
|
animate?: boolean;
|
|
5
6
|
animationDuration?: number;
|
|
6
7
|
animationEasing?: string;
|
|
@@ -36,7 +37,7 @@ export declare const defaultProps: {
|
|
|
36
37
|
animationDuration: number;
|
|
37
38
|
animationEasing: string;
|
|
38
39
|
center: [number, number];
|
|
39
|
-
data: Data
|
|
40
|
+
data: Data;
|
|
40
41
|
labelPosition: number;
|
|
41
42
|
lengthAngle: number;
|
|
42
43
|
lineWidth: number;
|
|
@@ -45,5 +46,5 @@ export declare const defaultProps: {
|
|
|
45
46
|
startAngle: number;
|
|
46
47
|
viewBoxSize: [number, number];
|
|
47
48
|
};
|
|
48
|
-
export
|
|
49
|
-
export declare function ReactMinimalPieChart<DataEntry extends BaseDataEntry>(originalProps: Props<DataEntry>): JSX.Element;
|
|
49
|
+
export type PropsWithDefaults<DataEntry extends BaseDataEntry = BaseDataEntry> = Props<DataEntry> & typeof defaultProps;
|
|
50
|
+
export declare function ReactMinimalPieChart<DataEntry extends BaseDataEntry>(originalProps: Props<DataEntry>): React.JSX.Element;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import React from 'react';
|
|
2
2
|
import type { PropsWithDefaults as ChartProps } from './Chart';
|
|
3
3
|
import type { ExtendedData, BaseDataEntry } from '../commonTypes';
|
|
4
|
-
export default function renderLabels<DataEntry extends BaseDataEntry>(data: ExtendedData<DataEntry>, props: ChartProps<DataEntry>): JSX.Element[] | undefined;
|
|
4
|
+
export default function renderLabels<DataEntry extends BaseDataEntry>(data: ExtendedData<DataEntry>, props: ChartProps<DataEntry>): React.JSX.Element[] | undefined;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import React from 'react';
|
|
2
2
|
import type { ExtendedData, BaseDataEntry } from '../commonTypes';
|
|
3
3
|
import type { PropsWithDefaults as ChartProps } from './Chart';
|
|
4
|
-
export default function renderSegments<DataEntry extends BaseDataEntry>(data: ExtendedData<DataEntry>, props: ChartProps<DataEntry>, revealOverride?: null | number): JSX.Element[];
|
|
4
|
+
export default function renderSegments<DataEntry extends BaseDataEntry>(data: ExtendedData<DataEntry>, props: ChartProps<DataEntry>, revealOverride?: null | number): React.JSX.Element[];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import type { CSSProperties } from 'react';
|
|
2
3
|
import type { ExtendedData, BaseDataEntry, LabelRenderFunction } from './commonTypes';
|
|
3
|
-
export
|
|
4
|
+
export type LabelRenderProps<DataEntry extends BaseDataEntry> = {
|
|
4
5
|
x: number;
|
|
5
6
|
y: number;
|
|
6
7
|
dx: number;
|
|
@@ -13,4 +14,4 @@ export declare type LabelRenderProps<DataEntry extends BaseDataEntry> = {
|
|
|
13
14
|
export default function ReactMinimalPieChartLabel<DataEntry extends BaseDataEntry>({ renderLabel, labelProps, }: {
|
|
14
15
|
renderLabel: LabelRenderFunction<DataEntry>;
|
|
15
16
|
labelProps: LabelRenderProps<DataEntry>;
|
|
16
|
-
}): JSX.Element | null;
|
|
17
|
+
}): React.JSX.Element | null;
|
package/{types → dist}/Path.d.ts
RENAMED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import type { SVGProps } from 'react';
|
|
2
3
|
export declare function makePathCommands(cx: number, cy: number, startAngle: number, lengthAngle: number, radius: number): string;
|
|
3
|
-
|
|
4
|
+
type Props = SVGProps<SVGPathElement> & {
|
|
4
5
|
cx: number;
|
|
5
6
|
cy: number;
|
|
6
7
|
lengthAngle: number;
|
|
@@ -12,5 +13,5 @@ declare type Props = SVGProps<SVGPathElement> & {
|
|
|
12
13
|
startAngle: number;
|
|
13
14
|
title?: string | number;
|
|
14
15
|
};
|
|
15
|
-
export default function ReactMinimalPieChartPath({ cx, cy, lengthAngle, lineWidth, radius, shift, reveal, rounded, startAngle, title, ...props }: Props): JSX.Element;
|
|
16
|
+
export default function ReactMinimalPieChartPath({ cx, cy, lengthAngle, lineWidth, radius, shift, reveal, rounded, startAngle, title, ...props }: Props): React.JSX.Element;
|
|
16
17
|
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { LabelRenderProps } from './Label';
|
|
3
|
+
export type LabelRenderFunction<DataEntry extends BaseDataEntry> = (labelRenderProps: LabelRenderProps<DataEntry>) => ReactNode;
|
|
4
|
+
export type BaseDataEntry = {
|
|
5
|
+
title?: string | number;
|
|
6
|
+
color: string;
|
|
7
|
+
value: number;
|
|
8
|
+
key?: string | number;
|
|
9
|
+
};
|
|
10
|
+
type BaseExtendedDataEntry<DataEntry extends BaseDataEntry = BaseDataEntry> = DataEntry & {
|
|
11
|
+
degrees: number;
|
|
12
|
+
startAngle: number;
|
|
13
|
+
percentage: number;
|
|
14
|
+
};
|
|
15
|
+
export type Data<DataEntry extends BaseDataEntry = BaseDataEntry> = DataEntry[];
|
|
16
|
+
export type ExtendedData<DataEntry extends BaseDataEntry = BaseDataEntry> = BaseExtendedDataEntry<DataEntry>[];
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var partialCircle = require('svg-partial-circle');
|
|
5
|
+
|
|
6
|
+
function degreesToRadians(degrees) {
|
|
7
|
+
return (degrees * Math.PI) / 180;
|
|
8
|
+
}
|
|
9
|
+
function valueBetween(value, min, max) {
|
|
10
|
+
if (value > max)
|
|
11
|
+
return max;
|
|
12
|
+
if (value < min)
|
|
13
|
+
return min;
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
function extractPercentage(value, percentage) {
|
|
17
|
+
return (percentage / 100) * value;
|
|
18
|
+
}
|
|
19
|
+
function bisectorAngle(startAngle, lengthAngle) {
|
|
20
|
+
return startAngle + lengthAngle / 2;
|
|
21
|
+
}
|
|
22
|
+
function shiftVectorAlongAngle(angle, distance) {
|
|
23
|
+
const angleRadians = degreesToRadians(angle);
|
|
24
|
+
return {
|
|
25
|
+
dx: distance * Math.cos(angleRadians),
|
|
26
|
+
dy: distance * Math.sin(angleRadians),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function isNumber(value) {
|
|
30
|
+
return typeof value === 'number';
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Conditionally return a prop or a function prop result
|
|
34
|
+
*/
|
|
35
|
+
function functionProp(prop, payload) {
|
|
36
|
+
return typeof prop === 'function'
|
|
37
|
+
? // @ts-expect-error: cannot find a way to type 2nd prop arg as anything-but-function
|
|
38
|
+
prop(payload)
|
|
39
|
+
: prop;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function sumValues(data) {
|
|
43
|
+
let sum = 0;
|
|
44
|
+
for (let i = 0; i < data.length; i++) {
|
|
45
|
+
sum += data[i].value;
|
|
46
|
+
}
|
|
47
|
+
return sum;
|
|
48
|
+
}
|
|
49
|
+
// Append "percentage", "degrees" and "startAngle" to each data entry
|
|
50
|
+
function extendData({ data, lengthAngle: totalAngle, totalValue, paddingAngle, startAngle: chartStartAngle, }) {
|
|
51
|
+
const total = totalValue || sumValues(data);
|
|
52
|
+
const normalizedTotalAngle = valueBetween(totalAngle, -360, 360);
|
|
53
|
+
const numberOfPaddings = Math.abs(normalizedTotalAngle) === 360 ? data.length : data.length - 1;
|
|
54
|
+
const singlePaddingDegrees = Math.abs(paddingAngle) * Math.sign(totalAngle);
|
|
55
|
+
const degreesTakenByPadding = singlePaddingDegrees * numberOfPaddings;
|
|
56
|
+
const degreesTakenByPaths = normalizedTotalAngle - degreesTakenByPadding;
|
|
57
|
+
let lastSegmentEnd = 0;
|
|
58
|
+
const extendedData = [];
|
|
59
|
+
// @NOTE: Shall we evaluate percentage accordingly to dataEntry.value's sign?
|
|
60
|
+
for (let i = 0; i < data.length; i++) {
|
|
61
|
+
const dataEntry = data[i];
|
|
62
|
+
const valueInPercentage = total === 0 ? 0 : (dataEntry.value / total) * 100;
|
|
63
|
+
const degrees = extractPercentage(degreesTakenByPaths, valueInPercentage);
|
|
64
|
+
const startAngle = lastSegmentEnd + chartStartAngle;
|
|
65
|
+
lastSegmentEnd = lastSegmentEnd + degrees + singlePaddingDegrees;
|
|
66
|
+
extendedData.push(Object.assign({
|
|
67
|
+
percentage: valueInPercentage,
|
|
68
|
+
startAngle,
|
|
69
|
+
degrees,
|
|
70
|
+
}, dataEntry));
|
|
71
|
+
}
|
|
72
|
+
return extendedData;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function ReactMinimalPieChartLabel({ renderLabel, labelProps, }) {
|
|
76
|
+
const label = renderLabel(labelProps);
|
|
77
|
+
// Default label
|
|
78
|
+
if (typeof label === 'string' || typeof label === 'number') {
|
|
79
|
+
const { dataEntry, dataIndex, ...props } = labelProps;
|
|
80
|
+
return (React.createElement("text", { dominantBaseline: "central", ...props }, label));
|
|
81
|
+
}
|
|
82
|
+
if (React.isValidElement(label)) {
|
|
83
|
+
return label;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function round(number) {
|
|
89
|
+
const divisor = 1e14; // 14 decimals
|
|
90
|
+
return Math.round((number + Number.EPSILON) * divisor) / divisor;
|
|
91
|
+
}
|
|
92
|
+
function evaluateTextAnchorPosition({ labelPosition, lineWidth, labelHorizontalShift, }) {
|
|
93
|
+
const dx = round(labelHorizontalShift);
|
|
94
|
+
// Label in the vertical center
|
|
95
|
+
if (dx === 0) {
|
|
96
|
+
return 'middle';
|
|
97
|
+
}
|
|
98
|
+
// Outward label
|
|
99
|
+
if (labelPosition > 100) {
|
|
100
|
+
return dx > 0 ? 'start' : 'end';
|
|
101
|
+
}
|
|
102
|
+
// Inward label
|
|
103
|
+
const innerRadius = 100 - lineWidth;
|
|
104
|
+
if (labelPosition < innerRadius) {
|
|
105
|
+
return dx > 0 ? 'end' : 'start';
|
|
106
|
+
}
|
|
107
|
+
// Overlying label
|
|
108
|
+
return 'middle';
|
|
109
|
+
}
|
|
110
|
+
function makeLabelRenderProps(data, props) {
|
|
111
|
+
return data.map((dataEntry, index) => {
|
|
112
|
+
const segmentsShift = functionProp(props.segmentsShift, index) ?? 0;
|
|
113
|
+
const distanceFromCenter = extractPercentage(props.radius, props.labelPosition) + segmentsShift;
|
|
114
|
+
const { dx, dy } = shiftVectorAlongAngle(bisectorAngle(dataEntry.startAngle, dataEntry.degrees), distanceFromCenter);
|
|
115
|
+
// This object is passed as argument to the "label" function prop
|
|
116
|
+
const labelRenderProps = {
|
|
117
|
+
x: props.center[0],
|
|
118
|
+
y: props.center[1],
|
|
119
|
+
dx,
|
|
120
|
+
dy,
|
|
121
|
+
textAnchor: evaluateTextAnchorPosition({
|
|
122
|
+
labelPosition: props.labelPosition,
|
|
123
|
+
lineWidth: props.lineWidth,
|
|
124
|
+
labelHorizontalShift: dx,
|
|
125
|
+
}),
|
|
126
|
+
dataEntry,
|
|
127
|
+
dataIndex: index,
|
|
128
|
+
style: functionProp(props.labelStyle, index),
|
|
129
|
+
};
|
|
130
|
+
return labelRenderProps;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function renderLabels(data, props) {
|
|
134
|
+
const { label } = props;
|
|
135
|
+
if (label) {
|
|
136
|
+
return makeLabelRenderProps(data, props).map((labelRenderProps, index) => (React.createElement(ReactMinimalPieChartLabel, { key: `label-${labelRenderProps.dataEntry.key || index}`, renderLabel: label, labelProps: labelRenderProps })));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function makePathCommands(cx, cy, startAngle, lengthAngle, radius) {
|
|
141
|
+
const patchedLengthAngle = valueBetween(lengthAngle, -359.999, 359.999);
|
|
142
|
+
return partialCircle(cx, cy, // center X and Y
|
|
143
|
+
radius, degreesToRadians(startAngle), degreesToRadians(startAngle + patchedLengthAngle))
|
|
144
|
+
.map((command) => command.join(' '))
|
|
145
|
+
.join(' ');
|
|
146
|
+
}
|
|
147
|
+
function ReactMinimalPieChartPath({ cx, cy, lengthAngle, lineWidth, radius, shift = 0, reveal, rounded, startAngle, title, ...props }) {
|
|
148
|
+
const pathRadius = radius - lineWidth / 2;
|
|
149
|
+
//@NOTE This shift might be rendered as a translation in future
|
|
150
|
+
const { dx, dy } = shiftVectorAlongAngle(bisectorAngle(startAngle, lengthAngle), shift);
|
|
151
|
+
const pathCommands = makePathCommands(cx + dx, cy + dy, startAngle, lengthAngle, pathRadius);
|
|
152
|
+
let strokeDasharray;
|
|
153
|
+
let strokeDashoffset;
|
|
154
|
+
// Animate/hide paths with "stroke-dasharray" + "stroke-dashoffset"
|
|
155
|
+
// https://css-tricks.com/svg-line-animation-works/
|
|
156
|
+
if (isNumber(reveal)) {
|
|
157
|
+
const pathLength = degreesToRadians(pathRadius) * lengthAngle;
|
|
158
|
+
strokeDasharray = Math.abs(pathLength);
|
|
159
|
+
strokeDashoffset =
|
|
160
|
+
strokeDasharray - extractPercentage(strokeDasharray, reveal);
|
|
161
|
+
}
|
|
162
|
+
return (React.createElement("path", { d: pathCommands, fill: "none", strokeWidth: lineWidth, strokeDasharray: strokeDasharray, strokeDashoffset: strokeDashoffset, strokeLinecap: rounded ? 'round' : undefined, ...props }, title && React.createElement("title", null, title)));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function combineSegmentTransitionsStyle(duration, easing, customStyle) {
|
|
166
|
+
// Merge chart's animation CSS transition with "transition" found to customStyle
|
|
167
|
+
let transition = `stroke-dashoffset ${duration}ms ${easing}`;
|
|
168
|
+
if (customStyle && customStyle.transition) {
|
|
169
|
+
transition = `${transition},${customStyle.transition}`;
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
transition,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function getRevealValue({ reveal, animate, }) {
|
|
176
|
+
//@NOTE When animation is on, chart has to be fully revealed when reveal is not set
|
|
177
|
+
if (animate && !isNumber(reveal)) {
|
|
178
|
+
return 100;
|
|
179
|
+
}
|
|
180
|
+
return reveal;
|
|
181
|
+
}
|
|
182
|
+
function makeEventHandler(eventHandler, payload) {
|
|
183
|
+
return (eventHandler &&
|
|
184
|
+
((e) => {
|
|
185
|
+
eventHandler(e, payload);
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
function renderSegments(data, props, revealOverride) {
|
|
189
|
+
// @NOTE this should go in Path component. Here for performance reasons
|
|
190
|
+
const reveal = revealOverride ?? getRevealValue(props);
|
|
191
|
+
const { radius, center: [cx, cy], } = props;
|
|
192
|
+
const lineWidth = extractPercentage(radius, props.lineWidth);
|
|
193
|
+
const paths = data.map((dataEntry, index) => {
|
|
194
|
+
const segmentsStyle = functionProp(props.segmentsStyle, index);
|
|
195
|
+
return (React.createElement(ReactMinimalPieChartPath, { cx: cx, cy: cy, key: dataEntry.key || index, lengthAngle: dataEntry.degrees, lineWidth: lineWidth, radius: radius, rounded: props.rounded, reveal: reveal, shift: functionProp(props.segmentsShift, index), startAngle: dataEntry.startAngle, title: dataEntry.title, style: Object.assign({}, segmentsStyle, props.animate &&
|
|
196
|
+
combineSegmentTransitionsStyle(props.animationDuration, props.animationEasing, segmentsStyle)), stroke: dataEntry.color, tabIndex: props.segmentsTabIndex, onBlur: makeEventHandler(props.onBlur, index), onClick: makeEventHandler(props.onClick, index), onFocus: makeEventHandler(props.onFocus, index), onKeyDown: makeEventHandler(props.onKeyDown, index), onMouseOver: makeEventHandler(props.onMouseOver, index), onMouseOut: makeEventHandler(props.onMouseOut, index) }));
|
|
197
|
+
});
|
|
198
|
+
if (props.background) {
|
|
199
|
+
paths.unshift(React.createElement(ReactMinimalPieChartPath, { cx: cx, cy: cy, key: "bg", lengthAngle: props.lengthAngle, lineWidth: lineWidth, radius: radius, rounded: props.rounded, startAngle: props.startAngle, stroke: props.background }));
|
|
200
|
+
}
|
|
201
|
+
return paths;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const defaultProps = {
|
|
205
|
+
animationDuration: 500,
|
|
206
|
+
animationEasing: 'ease-out',
|
|
207
|
+
center: [50, 50],
|
|
208
|
+
data: [],
|
|
209
|
+
labelPosition: 50,
|
|
210
|
+
lengthAngle: 360,
|
|
211
|
+
lineWidth: 100,
|
|
212
|
+
paddingAngle: 0,
|
|
213
|
+
radius: 50,
|
|
214
|
+
startAngle: 0,
|
|
215
|
+
viewBoxSize: [100, 100],
|
|
216
|
+
};
|
|
217
|
+
function makePropsWithDefaults(props) {
|
|
218
|
+
const result = Object.assign({}, defaultProps, props);
|
|
219
|
+
// @NOTE Object.assign doesn't default properties with undefined value (like React defaultProps does)
|
|
220
|
+
let key;
|
|
221
|
+
for (key in defaultProps) {
|
|
222
|
+
if (props[key] === undefined) {
|
|
223
|
+
// @ts-expect-error: TS cannot ensure we're assigning the expected props accross abjects
|
|
224
|
+
result[key] = defaultProps[key];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
function ReactMinimalPieChart(originalProps) {
|
|
230
|
+
const props = makePropsWithDefaults(originalProps);
|
|
231
|
+
const [revealOverride, setRevealOverride] = React.useState(props.animate ? 0 : null);
|
|
232
|
+
React.useEffect(() => {
|
|
233
|
+
if (props.animate) {
|
|
234
|
+
// Trigger initial animation
|
|
235
|
+
setRevealOverride(null);
|
|
236
|
+
}
|
|
237
|
+
}, []);
|
|
238
|
+
const extendedData = extendData(props);
|
|
239
|
+
return (React.createElement("svg", { viewBox: `0 0 ${props.viewBoxSize[0]} ${props.viewBoxSize[1]}`, width: "100%", height: "100%", className: props.className, style: props.style },
|
|
240
|
+
renderSegments(extendedData, props, revealOverride),
|
|
241
|
+
renderLabels(extendedData, props),
|
|
242
|
+
props.children));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
exports.PieChart = ReactMinimalPieChart;
|
|
246
|
+
exports.pieChartDefaultProps = defaultProps;
|
|
247
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/utils.ts","../src/Chart/extendData.ts","../src/Label.tsx","../src/Chart/renderLabels.tsx","../src/Path.tsx","../src/Chart/renderSegments.tsx","../src/Chart/Chart.tsx"],"sourcesContent":[null,null,null,null,null,null,null],"names":["Label","Path","useState","useEffect"],"mappings":";;;;;AAAM,SAAU,gBAAgB,CAAC,OAAe,EAAA;IAC9C,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,IAAI,GAAG;AAClC;SAEgB,YAAY,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW,EAAA;IAClE,IAAI,KAAK,GAAG,GAAG;AAAE,QAAA,OAAO,GAAG;IAC3B,IAAI,KAAK,GAAG,GAAG;AAAE,QAAA,OAAO,GAAG;AAC3B,IAAA,OAAO,KAAK;AACd;AAEgB,SAAA,iBAAiB,CAAC,KAAa,EAAE,UAAkB,EAAA;AACjE,IAAA,OAAO,CAAC,UAAU,GAAG,GAAG,IAAI,KAAK;AACnC;AAEgB,SAAA,aAAa,CAAC,UAAkB,EAAE,WAAmB,EAAA;AACnE,IAAA,OAAO,UAAU,GAAG,WAAW,GAAG,CAAC;AACrC;AAEgB,SAAA,qBAAqB,CAAC,KAAa,EAAE,QAAgB,EAAA;AACnE,IAAA,MAAM,YAAY,GAAG,gBAAgB,CAAC,KAAK,CAAC;IAC5C,OAAO;QACL,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC;QACrC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC;KACtC;AACH;AAEM,SAAU,QAAQ,CAAC,KAAc,EAAA;AACrC,IAAA,OAAO,OAAO,KAAK,KAAK,QAAQ;AAClC;AAEA;;AAEG;AACa,SAAA,YAAY,CAC1B,IAAsD,EACtD,OAAgB,EAAA;IAEhB,OAAO,OAAO,IAAI,KAAK;AACrB;YACE,IAAI,CAAC,OAAO;UACZ,IAAI;AACV;;AC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EAAE,cAAc,CAAC;AACnD,QAAA,YAAY,CAAC,YAAY,EAAE,KAAK,CAAC;AACjC,QAAA,KAAK,CAAC,QAAQ,CACX;AAEV;;;;;"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import partialCircle from 'svg-partial-circle';
|
|
3
|
+
|
|
4
|
+
function degreesToRadians(degrees) {
|
|
5
|
+
return (degrees * Math.PI) / 180;
|
|
6
|
+
}
|
|
7
|
+
function valueBetween(value, min, max) {
|
|
8
|
+
if (value > max)
|
|
9
|
+
return max;
|
|
10
|
+
if (value < min)
|
|
11
|
+
return min;
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function extractPercentage(value, percentage) {
|
|
15
|
+
return (percentage / 100) * value;
|
|
16
|
+
}
|
|
17
|
+
function bisectorAngle(startAngle, lengthAngle) {
|
|
18
|
+
return startAngle + lengthAngle / 2;
|
|
19
|
+
}
|
|
20
|
+
function shiftVectorAlongAngle(angle, distance) {
|
|
21
|
+
const angleRadians = degreesToRadians(angle);
|
|
22
|
+
return {
|
|
23
|
+
dx: distance * Math.cos(angleRadians),
|
|
24
|
+
dy: distance * Math.sin(angleRadians),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function isNumber(value) {
|
|
28
|
+
return typeof value === 'number';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Conditionally return a prop or a function prop result
|
|
32
|
+
*/
|
|
33
|
+
function functionProp(prop, payload) {
|
|
34
|
+
return typeof prop === 'function'
|
|
35
|
+
? // @ts-expect-error: cannot find a way to type 2nd prop arg as anything-but-function
|
|
36
|
+
prop(payload)
|
|
37
|
+
: prop;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function sumValues(data) {
|
|
41
|
+
let sum = 0;
|
|
42
|
+
for (let i = 0; i < data.length; i++) {
|
|
43
|
+
sum += data[i].value;
|
|
44
|
+
}
|
|
45
|
+
return sum;
|
|
46
|
+
}
|
|
47
|
+
// Append "percentage", "degrees" and "startAngle" to each data entry
|
|
48
|
+
function extendData({ data, lengthAngle: totalAngle, totalValue, paddingAngle, startAngle: chartStartAngle, }) {
|
|
49
|
+
const total = totalValue || sumValues(data);
|
|
50
|
+
const normalizedTotalAngle = valueBetween(totalAngle, -360, 360);
|
|
51
|
+
const numberOfPaddings = Math.abs(normalizedTotalAngle) === 360 ? data.length : data.length - 1;
|
|
52
|
+
const singlePaddingDegrees = Math.abs(paddingAngle) * Math.sign(totalAngle);
|
|
53
|
+
const degreesTakenByPadding = singlePaddingDegrees * numberOfPaddings;
|
|
54
|
+
const degreesTakenByPaths = normalizedTotalAngle - degreesTakenByPadding;
|
|
55
|
+
let lastSegmentEnd = 0;
|
|
56
|
+
const extendedData = [];
|
|
57
|
+
// @NOTE: Shall we evaluate percentage accordingly to dataEntry.value's sign?
|
|
58
|
+
for (let i = 0; i < data.length; i++) {
|
|
59
|
+
const dataEntry = data[i];
|
|
60
|
+
const valueInPercentage = total === 0 ? 0 : (dataEntry.value / total) * 100;
|
|
61
|
+
const degrees = extractPercentage(degreesTakenByPaths, valueInPercentage);
|
|
62
|
+
const startAngle = lastSegmentEnd + chartStartAngle;
|
|
63
|
+
lastSegmentEnd = lastSegmentEnd + degrees + singlePaddingDegrees;
|
|
64
|
+
extendedData.push(Object.assign({
|
|
65
|
+
percentage: valueInPercentage,
|
|
66
|
+
startAngle,
|
|
67
|
+
degrees,
|
|
68
|
+
}, dataEntry));
|
|
69
|
+
}
|
|
70
|
+
return extendedData;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function ReactMinimalPieChartLabel({ renderLabel, labelProps, }) {
|
|
74
|
+
const label = renderLabel(labelProps);
|
|
75
|
+
// Default label
|
|
76
|
+
if (typeof label === 'string' || typeof label === 'number') {
|
|
77
|
+
const { dataEntry, dataIndex, ...props } = labelProps;
|
|
78
|
+
return (React.createElement("text", { dominantBaseline: "central", ...props }, label));
|
|
79
|
+
}
|
|
80
|
+
if (React.isValidElement(label)) {
|
|
81
|
+
return label;
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function round(number) {
|
|
87
|
+
const divisor = 1e14; // 14 decimals
|
|
88
|
+
return Math.round((number + Number.EPSILON) * divisor) / divisor;
|
|
89
|
+
}
|
|
90
|
+
function evaluateTextAnchorPosition({ labelPosition, lineWidth, labelHorizontalShift, }) {
|
|
91
|
+
const dx = round(labelHorizontalShift);
|
|
92
|
+
// Label in the vertical center
|
|
93
|
+
if (dx === 0) {
|
|
94
|
+
return 'middle';
|
|
95
|
+
}
|
|
96
|
+
// Outward label
|
|
97
|
+
if (labelPosition > 100) {
|
|
98
|
+
return dx > 0 ? 'start' : 'end';
|
|
99
|
+
}
|
|
100
|
+
// Inward label
|
|
101
|
+
const innerRadius = 100 - lineWidth;
|
|
102
|
+
if (labelPosition < innerRadius) {
|
|
103
|
+
return dx > 0 ? 'end' : 'start';
|
|
104
|
+
}
|
|
105
|
+
// Overlying label
|
|
106
|
+
return 'middle';
|
|
107
|
+
}
|
|
108
|
+
function makeLabelRenderProps(data, props) {
|
|
109
|
+
return data.map((dataEntry, index) => {
|
|
110
|
+
const segmentsShift = functionProp(props.segmentsShift, index) ?? 0;
|
|
111
|
+
const distanceFromCenter = extractPercentage(props.radius, props.labelPosition) + segmentsShift;
|
|
112
|
+
const { dx, dy } = shiftVectorAlongAngle(bisectorAngle(dataEntry.startAngle, dataEntry.degrees), distanceFromCenter);
|
|
113
|
+
// This object is passed as argument to the "label" function prop
|
|
114
|
+
const labelRenderProps = {
|
|
115
|
+
x: props.center[0],
|
|
116
|
+
y: props.center[1],
|
|
117
|
+
dx,
|
|
118
|
+
dy,
|
|
119
|
+
textAnchor: evaluateTextAnchorPosition({
|
|
120
|
+
labelPosition: props.labelPosition,
|
|
121
|
+
lineWidth: props.lineWidth,
|
|
122
|
+
labelHorizontalShift: dx,
|
|
123
|
+
}),
|
|
124
|
+
dataEntry,
|
|
125
|
+
dataIndex: index,
|
|
126
|
+
style: functionProp(props.labelStyle, index),
|
|
127
|
+
};
|
|
128
|
+
return labelRenderProps;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function renderLabels(data, props) {
|
|
132
|
+
const { label } = props;
|
|
133
|
+
if (label) {
|
|
134
|
+
return makeLabelRenderProps(data, props).map((labelRenderProps, index) => (React.createElement(ReactMinimalPieChartLabel, { key: `label-${labelRenderProps.dataEntry.key || index}`, renderLabel: label, labelProps: labelRenderProps })));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function makePathCommands(cx, cy, startAngle, lengthAngle, radius) {
|
|
139
|
+
const patchedLengthAngle = valueBetween(lengthAngle, -359.999, 359.999);
|
|
140
|
+
return partialCircle(cx, cy, // center X and Y
|
|
141
|
+
radius, degreesToRadians(startAngle), degreesToRadians(startAngle + patchedLengthAngle))
|
|
142
|
+
.map((command) => command.join(' '))
|
|
143
|
+
.join(' ');
|
|
144
|
+
}
|
|
145
|
+
function ReactMinimalPieChartPath({ cx, cy, lengthAngle, lineWidth, radius, shift = 0, reveal, rounded, startAngle, title, ...props }) {
|
|
146
|
+
const pathRadius = radius - lineWidth / 2;
|
|
147
|
+
//@NOTE This shift might be rendered as a translation in future
|
|
148
|
+
const { dx, dy } = shiftVectorAlongAngle(bisectorAngle(startAngle, lengthAngle), shift);
|
|
149
|
+
const pathCommands = makePathCommands(cx + dx, cy + dy, startAngle, lengthAngle, pathRadius);
|
|
150
|
+
let strokeDasharray;
|
|
151
|
+
let strokeDashoffset;
|
|
152
|
+
// Animate/hide paths with "stroke-dasharray" + "stroke-dashoffset"
|
|
153
|
+
// https://css-tricks.com/svg-line-animation-works/
|
|
154
|
+
if (isNumber(reveal)) {
|
|
155
|
+
const pathLength = degreesToRadians(pathRadius) * lengthAngle;
|
|
156
|
+
strokeDasharray = Math.abs(pathLength);
|
|
157
|
+
strokeDashoffset =
|
|
158
|
+
strokeDasharray - extractPercentage(strokeDasharray, reveal);
|
|
159
|
+
}
|
|
160
|
+
return (React.createElement("path", { d: pathCommands, fill: "none", strokeWidth: lineWidth, strokeDasharray: strokeDasharray, strokeDashoffset: strokeDashoffset, strokeLinecap: rounded ? 'round' : undefined, ...props }, title && React.createElement("title", null, title)));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function combineSegmentTransitionsStyle(duration, easing, customStyle) {
|
|
164
|
+
// Merge chart's animation CSS transition with "transition" found to customStyle
|
|
165
|
+
let transition = `stroke-dashoffset ${duration}ms ${easing}`;
|
|
166
|
+
if (customStyle && customStyle.transition) {
|
|
167
|
+
transition = `${transition},${customStyle.transition}`;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
transition,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function getRevealValue({ reveal, animate, }) {
|
|
174
|
+
//@NOTE When animation is on, chart has to be fully revealed when reveal is not set
|
|
175
|
+
if (animate && !isNumber(reveal)) {
|
|
176
|
+
return 100;
|
|
177
|
+
}
|
|
178
|
+
return reveal;
|
|
179
|
+
}
|
|
180
|
+
function makeEventHandler(eventHandler, payload) {
|
|
181
|
+
return (eventHandler &&
|
|
182
|
+
((e) => {
|
|
183
|
+
eventHandler(e, payload);
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
function renderSegments(data, props, revealOverride) {
|
|
187
|
+
// @NOTE this should go in Path component. Here for performance reasons
|
|
188
|
+
const reveal = revealOverride ?? getRevealValue(props);
|
|
189
|
+
const { radius, center: [cx, cy], } = props;
|
|
190
|
+
const lineWidth = extractPercentage(radius, props.lineWidth);
|
|
191
|
+
const paths = data.map((dataEntry, index) => {
|
|
192
|
+
const segmentsStyle = functionProp(props.segmentsStyle, index);
|
|
193
|
+
return (React.createElement(ReactMinimalPieChartPath, { cx: cx, cy: cy, key: dataEntry.key || index, lengthAngle: dataEntry.degrees, lineWidth: lineWidth, radius: radius, rounded: props.rounded, reveal: reveal, shift: functionProp(props.segmentsShift, index), startAngle: dataEntry.startAngle, title: dataEntry.title, style: Object.assign({}, segmentsStyle, props.animate &&
|
|
194
|
+
combineSegmentTransitionsStyle(props.animationDuration, props.animationEasing, segmentsStyle)), stroke: dataEntry.color, tabIndex: props.segmentsTabIndex, onBlur: makeEventHandler(props.onBlur, index), onClick: makeEventHandler(props.onClick, index), onFocus: makeEventHandler(props.onFocus, index), onKeyDown: makeEventHandler(props.onKeyDown, index), onMouseOver: makeEventHandler(props.onMouseOver, index), onMouseOut: makeEventHandler(props.onMouseOut, index) }));
|
|
195
|
+
});
|
|
196
|
+
if (props.background) {
|
|
197
|
+
paths.unshift(React.createElement(ReactMinimalPieChartPath, { cx: cx, cy: cy, key: "bg", lengthAngle: props.lengthAngle, lineWidth: lineWidth, radius: radius, rounded: props.rounded, startAngle: props.startAngle, stroke: props.background }));
|
|
198
|
+
}
|
|
199
|
+
return paths;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const defaultProps = {
|
|
203
|
+
animationDuration: 500,
|
|
204
|
+
animationEasing: 'ease-out',
|
|
205
|
+
center: [50, 50],
|
|
206
|
+
data: [],
|
|
207
|
+
labelPosition: 50,
|
|
208
|
+
lengthAngle: 360,
|
|
209
|
+
lineWidth: 100,
|
|
210
|
+
paddingAngle: 0,
|
|
211
|
+
radius: 50,
|
|
212
|
+
startAngle: 0,
|
|
213
|
+
viewBoxSize: [100, 100],
|
|
214
|
+
};
|
|
215
|
+
function makePropsWithDefaults(props) {
|
|
216
|
+
const result = Object.assign({}, defaultProps, props);
|
|
217
|
+
// @NOTE Object.assign doesn't default properties with undefined value (like React defaultProps does)
|
|
218
|
+
let key;
|
|
219
|
+
for (key in defaultProps) {
|
|
220
|
+
if (props[key] === undefined) {
|
|
221
|
+
// @ts-expect-error: TS cannot ensure we're assigning the expected props accross abjects
|
|
222
|
+
result[key] = defaultProps[key];
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
function ReactMinimalPieChart(originalProps) {
|
|
228
|
+
const props = makePropsWithDefaults(originalProps);
|
|
229
|
+
const [revealOverride, setRevealOverride] = useState(props.animate ? 0 : null);
|
|
230
|
+
useEffect(() => {
|
|
231
|
+
if (props.animate) {
|
|
232
|
+
// Trigger initial animation
|
|
233
|
+
setRevealOverride(null);
|
|
234
|
+
}
|
|
235
|
+
}, []);
|
|
236
|
+
const extendedData = extendData(props);
|
|
237
|
+
return (React.createElement("svg", { viewBox: `0 0 ${props.viewBoxSize[0]} ${props.viewBoxSize[1]}`, width: "100%", height: "100%", className: props.className, style: props.style },
|
|
238
|
+
renderSegments(extendedData, props, revealOverride),
|
|
239
|
+
renderLabels(extendedData, props),
|
|
240
|
+
props.children));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export { ReactMinimalPieChart as PieChart, defaultProps as pieChartDefaultProps };
|
|
244
|
+
//# sourceMappingURL=index.esm.js.map
|