bento-charts 1.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/.git-ignore +6 -0
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/package.json +38 -0
- package/src/ChartConfigProvider.tsx +38 -0
- package/src/Charts/BentoBarChart.tsx +113 -0
- package/src/Charts/BentoPie.tsx +232 -0
- package/src/constants/chartConstants.ts +120 -0
- package/src/index.js +4 -0
- package/src/types/chartTypes.ts +70 -0
- package/src/util/chartUtils.ts +8 -0
- package/tsconfig.json +24 -0
- package/webpack.config.js +18 -0
package/.git-ignore
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Bento Platform
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bento-charts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Charts library for Bento-platform",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "webpack --mode=production",
|
|
8
|
+
"watch": "webpack --mode=development --watch"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/bento-platform/Bento-Charts.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"Bento",
|
|
16
|
+
"Charts"
|
|
17
|
+
],
|
|
18
|
+
"author": "Sanjeev Lakhwani",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/bento-platform/Bento-Charts/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/bento-platform/Bento-Charts#readme",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@types/react": "^18.0.28",
|
|
26
|
+
"@types/react-dom": "^18.0.10",
|
|
27
|
+
"react": "^18.2.0",
|
|
28
|
+
"react-dom": "^18.2.0",
|
|
29
|
+
"recharts": "^2.4.3",
|
|
30
|
+
"ts-loader": "^9.4.2"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"prettier": "2.7.1",
|
|
34
|
+
"typescript": "^4.9.5",
|
|
35
|
+
"webpack": "^5.76.0",
|
|
36
|
+
"webpack-cli": "^4.9.2"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React, { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { DEFAULT_CHART_THEME, defaultTranslationObject } from './constants/chartConstants';
|
|
4
|
+
import { ChartTheme, LngDictionary, SupportedLng, TranslationObject } from './types/chartTypes';
|
|
5
|
+
|
|
6
|
+
const ChartThemeContext = React.createContext<ChartTheme>(DEFAULT_CHART_THEME);
|
|
7
|
+
export const useChartTheme = () => useContext(ChartThemeContext);
|
|
8
|
+
|
|
9
|
+
const ChartTranslationContext = React.createContext<LngDictionary>(defaultTranslationObject.en);
|
|
10
|
+
export const useChartTranslation = () => useContext(ChartTranslationContext);
|
|
11
|
+
|
|
12
|
+
const ChartConfigProvider = ({
|
|
13
|
+
theme = useChartTheme(),
|
|
14
|
+
Lng,
|
|
15
|
+
translationMap,
|
|
16
|
+
children,
|
|
17
|
+
}: {
|
|
18
|
+
theme?: ChartTheme;
|
|
19
|
+
Lng: string;
|
|
20
|
+
translationMap?: TranslationObject;
|
|
21
|
+
children: React.ReactElement;
|
|
22
|
+
}) => {
|
|
23
|
+
let lang: SupportedLng = 'en';
|
|
24
|
+
try {
|
|
25
|
+
lang = Lng as SupportedLng;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error('Lng is not a supported language');
|
|
28
|
+
}
|
|
29
|
+
return (
|
|
30
|
+
<ChartThemeContext.Provider value={theme}>
|
|
31
|
+
<ChartTranslationContext.Provider value={translationMap ? translationMap[lang] : defaultTranslationObject[lang]}>
|
|
32
|
+
{children}
|
|
33
|
+
</ChartTranslationContext.Provider>
|
|
34
|
+
</ChartThemeContext.Provider>
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export default ChartConfigProvider;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { BarChart, Bar, Cell, XAxis, YAxis, Tooltip, Label } from 'recharts';
|
|
3
|
+
import {
|
|
4
|
+
TOOL_TIP_STYLE,
|
|
5
|
+
COUNT_STYLE,
|
|
6
|
+
LABEL_STYLE,
|
|
7
|
+
CHART_WRAPPER_STYLE,
|
|
8
|
+
MAX_TICK_LABEL_CHARS,
|
|
9
|
+
TITLE_STYLE,
|
|
10
|
+
ASPECT_RATIO,
|
|
11
|
+
TICKS_SHOW_ALL_LABELS_BELOW,
|
|
12
|
+
UNITS_LABEL_OFFSET,
|
|
13
|
+
TICK_MARGIN,
|
|
14
|
+
} from '../constants/chartConstants';
|
|
15
|
+
|
|
16
|
+
import type { BarChartProps, ChartDataItem, TooltipPayload } from '../types/chartTypes';
|
|
17
|
+
import { useChartTheme, useChartTranslation } from '../ChartConfigProvider';
|
|
18
|
+
|
|
19
|
+
const tickFormatter = (tickLabel: string) => {
|
|
20
|
+
if (tickLabel.length <= MAX_TICK_LABEL_CHARS) {
|
|
21
|
+
return tickLabel;
|
|
22
|
+
}
|
|
23
|
+
return `${tickLabel.substring(0, MAX_TICK_LABEL_CHARS)}...`;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const BentoBarChart = ({
|
|
27
|
+
data,
|
|
28
|
+
height,
|
|
29
|
+
units,
|
|
30
|
+
title,
|
|
31
|
+
preFilter,
|
|
32
|
+
dataMap,
|
|
33
|
+
postFilter,
|
|
34
|
+
removeEmpty = true,
|
|
35
|
+
colorTheme = 'default',
|
|
36
|
+
}: BarChartProps) => {
|
|
37
|
+
const t = useChartTranslation();
|
|
38
|
+
const { fill: chartFill, missing } = useChartTheme().bar[colorTheme];
|
|
39
|
+
|
|
40
|
+
const fill = (entry: ChartDataItem) => (entry.x === 'missing' ? missing : chartFill);
|
|
41
|
+
|
|
42
|
+
data = [...data];
|
|
43
|
+
if (preFilter) data = data.filter(preFilter);
|
|
44
|
+
if (dataMap) data = data.map(dataMap);
|
|
45
|
+
if (postFilter) data = data.filter(postFilter);
|
|
46
|
+
|
|
47
|
+
if (removeEmpty) data = data.filter((d) => d.y !== 0);
|
|
48
|
+
|
|
49
|
+
const totalCount = data.reduce((sum, e) => sum + e.y, 0);
|
|
50
|
+
|
|
51
|
+
// Regarding XAxis.ticks below:
|
|
52
|
+
// The weird conditional is added from https://github.com/recharts/recharts/issues/2593#issuecomment-1311678397
|
|
53
|
+
// Basically, if data is empty, Recharts will default to a domain of [0, "auto"] and our tickFormatter trips up
|
|
54
|
+
// on formatting a non-string. This hack manually overrides the ticks for the axis and blanks it out.
|
|
55
|
+
// - David L, 2023-01-03
|
|
56
|
+
return (
|
|
57
|
+
<div style={CHART_WRAPPER_STYLE}>
|
|
58
|
+
<div style={TITLE_STYLE}>{title}</div>
|
|
59
|
+
<BarChart width={height * ASPECT_RATIO} height={height} data={data} margin={{ top: 10, bottom: 100, right: 20 }}>
|
|
60
|
+
<XAxis
|
|
61
|
+
dataKey="x"
|
|
62
|
+
height={20}
|
|
63
|
+
angle={-45}
|
|
64
|
+
ticks={data.length ? undefined : ['']}
|
|
65
|
+
tickFormatter={tickFormatter}
|
|
66
|
+
tickMargin={TICK_MARGIN}
|
|
67
|
+
textAnchor="end"
|
|
68
|
+
interval={data.length < TICKS_SHOW_ALL_LABELS_BELOW ? 0 : 'preserveStartEnd'}
|
|
69
|
+
>
|
|
70
|
+
<Label value={units} offset={UNITS_LABEL_OFFSET} position="insideBottom" />
|
|
71
|
+
</XAxis>
|
|
72
|
+
<YAxis>
|
|
73
|
+
<Label value={t['Count']} offset={-10} position="left" angle={270} />
|
|
74
|
+
</YAxis>
|
|
75
|
+
<Tooltip content={<BarTooltip totalCount={totalCount} />} />
|
|
76
|
+
<Bar dataKey="y" isAnimationActive={false}>
|
|
77
|
+
{data.map((entry) => (
|
|
78
|
+
<Cell key={entry.x} fill={fill(entry)} />
|
|
79
|
+
))}
|
|
80
|
+
</Bar>
|
|
81
|
+
</BarChart>
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const BarTooltip = ({
|
|
87
|
+
active,
|
|
88
|
+
payload,
|
|
89
|
+
totalCount,
|
|
90
|
+
}: {
|
|
91
|
+
active?: boolean;
|
|
92
|
+
payload?: TooltipPayload;
|
|
93
|
+
totalCount: number;
|
|
94
|
+
}) => {
|
|
95
|
+
if (!active) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const name = (payload && payload[0]?.payload?.x) || '';
|
|
100
|
+
const value = (payload && payload[0]?.value) || 0;
|
|
101
|
+
const percentage = totalCount ? Math.round((value / totalCount) * 100) : 0;
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<div style={TOOL_TIP_STYLE}>
|
|
105
|
+
<p style={LABEL_STYLE}>{name}</p>
|
|
106
|
+
<p style={COUNT_STYLE}>
|
|
107
|
+
{value} ({percentage}%)
|
|
108
|
+
</p>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default BentoBarChart;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { PieChart, Pie, Cell, Curve, Tooltip, Sector } from 'recharts';
|
|
3
|
+
import { Props } from 'recharts/types/polar/Pie';
|
|
4
|
+
type PieProps = Props;
|
|
5
|
+
import type CSS from 'csstype';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
TOOL_TIP_STYLE,
|
|
9
|
+
LABEL_STYLE,
|
|
10
|
+
COUNT_STYLE,
|
|
11
|
+
OTHER_THRESHOLD,
|
|
12
|
+
CHART_MISSING_FILL,
|
|
13
|
+
CHART_WRAPPER_STYLE,
|
|
14
|
+
RADIAN,
|
|
15
|
+
MAX_LABEL_CHARS,
|
|
16
|
+
CHART_ASPECT_RATIO,
|
|
17
|
+
LABEL_THRESHOLD,
|
|
18
|
+
COUNT_TEXT_STYLE,
|
|
19
|
+
TEXT_STYLE,
|
|
20
|
+
} from '../constants/chartConstants';
|
|
21
|
+
import type { PieChartProps, TooltipPayload } from '../types/chartTypes';
|
|
22
|
+
import { useChartTheme, useChartTranslation } from '../ChartConfigProvider';
|
|
23
|
+
import { polarToCartesian } from '../util/chartUtils';
|
|
24
|
+
|
|
25
|
+
const labelShortName = (name: string) => {
|
|
26
|
+
if (name.length <= MAX_LABEL_CHARS) {
|
|
27
|
+
return name;
|
|
28
|
+
}
|
|
29
|
+
// removing 3 character cause ... s add three characters
|
|
30
|
+
return `${name.substring(0, MAX_LABEL_CHARS - 3)}\u2026`;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const BentoPie = ({
|
|
34
|
+
data,
|
|
35
|
+
height,
|
|
36
|
+
preFilter,
|
|
37
|
+
dataMap,
|
|
38
|
+
postFilter,
|
|
39
|
+
sort = true,
|
|
40
|
+
removeEmpty = true,
|
|
41
|
+
colorTheme = 'default',
|
|
42
|
+
}: PieChartProps) => {
|
|
43
|
+
const t = useChartTranslation();
|
|
44
|
+
const theme = useChartTheme().pie[colorTheme];
|
|
45
|
+
|
|
46
|
+
const [activeIndex, setActiveIndex] = useState<number | undefined>(undefined);
|
|
47
|
+
|
|
48
|
+
// ##################### Data processing #####################
|
|
49
|
+
|
|
50
|
+
data = [...data]; // Changing immutable data to mutable data
|
|
51
|
+
if (preFilter) data = data.filter(preFilter);
|
|
52
|
+
if (dataMap) data = data.map(dataMap);
|
|
53
|
+
if (postFilter) data = data.filter(postFilter);
|
|
54
|
+
|
|
55
|
+
// removing empty values
|
|
56
|
+
if (removeEmpty) data = data.filter((e) => e.y !== 0);
|
|
57
|
+
|
|
58
|
+
if (sort) data.sort((a, b) => a.y - b.y);
|
|
59
|
+
|
|
60
|
+
// combining sections with less than OTHER_THRESHOLD
|
|
61
|
+
const sum = data.reduce((acc, e) => acc + e.y, 0);
|
|
62
|
+
const length = data.length;
|
|
63
|
+
const threshold = OTHER_THRESHOLD * sum;
|
|
64
|
+
const temp = data.filter((e) => e.y > threshold);
|
|
65
|
+
// length - 1 intentional: if there is just one category bellow threshold the "Other" category is not necessary
|
|
66
|
+
data = temp.length === length - 1 ? data : temp;
|
|
67
|
+
if (data.length !== length) {
|
|
68
|
+
data.push({ x: t['Other'], y: sum - data.reduce((acc, e) => acc + e.y, 0) });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const bentoFormatData = data.map((e) => ({ name: e.x, value: e.y }));
|
|
72
|
+
|
|
73
|
+
// ##################### Rendering #####################
|
|
74
|
+
const onEnter: PieProps['onMouseEnter'] = (_data, index) => {
|
|
75
|
+
setActiveIndex(index);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const onHover: PieProps['onMouseOver'] = (_data, _index, e) => {
|
|
79
|
+
const { target } = e;
|
|
80
|
+
if (target) (target as SVGElement).style.cursor = 'pointer';
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const onLeave: PieProps['onMouseLeave'] = () => {
|
|
84
|
+
setActiveIndex(undefined);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<div style={CHART_WRAPPER_STYLE}>
|
|
89
|
+
<PieChart height={height} width={height * CHART_ASPECT_RATIO}>
|
|
90
|
+
<Pie
|
|
91
|
+
data={bentoFormatData}
|
|
92
|
+
dataKey="value"
|
|
93
|
+
cx="50%"
|
|
94
|
+
cy="50%"
|
|
95
|
+
innerRadius={35}
|
|
96
|
+
outerRadius={80}
|
|
97
|
+
label={RenderLabel}
|
|
98
|
+
labelLine={false}
|
|
99
|
+
isAnimationActive={false}
|
|
100
|
+
onMouseEnter={onEnter}
|
|
101
|
+
onMouseLeave={onLeave}
|
|
102
|
+
onMouseOver={onHover}
|
|
103
|
+
activeIndex={activeIndex}
|
|
104
|
+
activeShape={RenderActiveLabel}
|
|
105
|
+
>
|
|
106
|
+
{data.map((entry, index) => {
|
|
107
|
+
let fill = theme[index % theme.length];
|
|
108
|
+
fill = entry.x.toLowerCase() === 'missing' ? CHART_MISSING_FILL : fill;
|
|
109
|
+
return <Cell key={index} fill={fill} />;
|
|
110
|
+
})}
|
|
111
|
+
</Pie>
|
|
112
|
+
<Tooltip
|
|
113
|
+
content={<CustomTooltip totalCount={sum} />}
|
|
114
|
+
isAnimationActive={false}
|
|
115
|
+
allowEscapeViewBox={{ x: true, y: true }}
|
|
116
|
+
/>
|
|
117
|
+
</PieChart>
|
|
118
|
+
</div>
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const RenderLabel: PieProps['label'] = (params) => {
|
|
123
|
+
const { cx, cy, midAngle, outerRadius, fill, payload, index, activeIndex } = params;
|
|
124
|
+
|
|
125
|
+
// skip rendering this static label if the sector is selected.
|
|
126
|
+
// this will let the 'renderActiveState' draw without overlapping.
|
|
127
|
+
// also, skip rendering if segment is too small a percentage (avoids label clutter)
|
|
128
|
+
if (index === activeIndex || params.percent < LABEL_THRESHOLD) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const name = payload.name === 'null' ? '(Empty)' : payload.name;
|
|
133
|
+
|
|
134
|
+
const sin = Math.sin(-RADIAN * midAngle);
|
|
135
|
+
const cos = Math.cos(-RADIAN * midAngle);
|
|
136
|
+
const sx = cx + (outerRadius + 10) * cos;
|
|
137
|
+
const sy = cy + (outerRadius + 10) * sin;
|
|
138
|
+
const mx = cx + (outerRadius + 20) * cos;
|
|
139
|
+
const my = cy + (outerRadius + 20) * sin;
|
|
140
|
+
const ex = mx + (cos >= 0 ? 1 : -1) * 22;
|
|
141
|
+
const ey = my;
|
|
142
|
+
const textAnchor = cos >= 0 ? 'start' : 'end';
|
|
143
|
+
|
|
144
|
+
const currentTextStyle: CSS.Properties = {
|
|
145
|
+
...TEXT_STYLE,
|
|
146
|
+
fontWeight: payload.selected ? 'bold' : 'normal',
|
|
147
|
+
fontStyle: payload.name === 'null' ? 'italic' : 'normal',
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const offsetRadius = 20;
|
|
151
|
+
const startPoint = polarToCartesian(params.cx, params.cy, params.outerRadius, midAngle);
|
|
152
|
+
const endPoint = polarToCartesian(params.cx, params.cy, params.outerRadius + offsetRadius, midAngle);
|
|
153
|
+
const lineProps = {
|
|
154
|
+
...params,
|
|
155
|
+
fill: 'none',
|
|
156
|
+
stroke: fill,
|
|
157
|
+
points: [startPoint, endPoint],
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<g>
|
|
162
|
+
<Curve {...lineProps} type="linear" className="recharts-pie-label-line" />
|
|
163
|
+
<path d={`M${sx},${sy}L${mx},${my}L${ex},${ey}`} stroke={fill} fill="none" />
|
|
164
|
+
<circle cx={ex} cy={ey} r={2} fill={fill} stroke="none" />
|
|
165
|
+
<text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey + 3} textAnchor={textAnchor} style={currentTextStyle}>
|
|
166
|
+
{labelShortName(name)}
|
|
167
|
+
</text>
|
|
168
|
+
<text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} dy={14} textAnchor={textAnchor} style={COUNT_TEXT_STYLE}>
|
|
169
|
+
{`(${payload.value})`}
|
|
170
|
+
</text>
|
|
171
|
+
</g>
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const RenderActiveLabel: PieProps['activeShape'] = (params) => {
|
|
176
|
+
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle, fill } = params;
|
|
177
|
+
|
|
178
|
+
// render arc around active segment
|
|
179
|
+
return (
|
|
180
|
+
<g>
|
|
181
|
+
<Sector
|
|
182
|
+
cx={cx}
|
|
183
|
+
cy={cy}
|
|
184
|
+
startAngle={startAngle}
|
|
185
|
+
endAngle={endAngle}
|
|
186
|
+
innerRadius={innerRadius}
|
|
187
|
+
outerRadius={outerRadius}
|
|
188
|
+
fill={fill}
|
|
189
|
+
/>
|
|
190
|
+
<Sector
|
|
191
|
+
cx={cx}
|
|
192
|
+
cy={cy}
|
|
193
|
+
startAngle={startAngle}
|
|
194
|
+
endAngle={endAngle}
|
|
195
|
+
innerRadius={outerRadius + 6}
|
|
196
|
+
outerRadius={outerRadius + 10}
|
|
197
|
+
fill={fill}
|
|
198
|
+
/>
|
|
199
|
+
</g>
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const CustomTooltip = ({
|
|
204
|
+
active,
|
|
205
|
+
payload,
|
|
206
|
+
totalCount,
|
|
207
|
+
}: {
|
|
208
|
+
active?: boolean;
|
|
209
|
+
payload?: TooltipPayload;
|
|
210
|
+
totalCount: number;
|
|
211
|
+
}) => {
|
|
212
|
+
if (!active) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const name = payload ? payload[0].name : '';
|
|
217
|
+
const value = payload ? payload[0].value : 0;
|
|
218
|
+
const percentage = totalCount ? Math.round((value / totalCount) * 100) : 0;
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<div style={TOOL_TIP_STYLE}>
|
|
222
|
+
<p style={LABEL_STYLE}>{name}</p>
|
|
223
|
+
<p style={COUNT_STYLE}>
|
|
224
|
+
{' '}
|
|
225
|
+
{value} ({percentage}
|
|
226
|
+
%)
|
|
227
|
+
</p>
|
|
228
|
+
</div>
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export default BentoPie;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type CSS from 'csstype';
|
|
2
|
+
import { ChartTheme, HexColor, TranslationObject } from '../types/chartTypes';
|
|
3
|
+
|
|
4
|
+
// ################### PACKAGE CONSTANTS ###################
|
|
5
|
+
export const defaultTranslationObject: TranslationObject = {
|
|
6
|
+
en: {
|
|
7
|
+
Count: 'Count',
|
|
8
|
+
Other: 'Other',
|
|
9
|
+
},
|
|
10
|
+
fr: {
|
|
11
|
+
Count: 'Comptage',
|
|
12
|
+
Other: 'Autre',
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// ################### THEME CONSTANTS ###################
|
|
17
|
+
// Bento-web colours
|
|
18
|
+
export const COLORS: HexColor[] = [
|
|
19
|
+
'#3366CC',
|
|
20
|
+
'#DC3912',
|
|
21
|
+
'#FF9900',
|
|
22
|
+
'#109618',
|
|
23
|
+
'#990099',
|
|
24
|
+
'#3B3EAC',
|
|
25
|
+
'#0099C6',
|
|
26
|
+
'#DD4477',
|
|
27
|
+
'#66AA00',
|
|
28
|
+
'#B82E2E',
|
|
29
|
+
'#316395',
|
|
30
|
+
'#994499',
|
|
31
|
+
'#22AA99',
|
|
32
|
+
'#AAAA11',
|
|
33
|
+
'#6633CC',
|
|
34
|
+
'#E67300',
|
|
35
|
+
'#8B0707',
|
|
36
|
+
'#329262',
|
|
37
|
+
'#5574A6',
|
|
38
|
+
'#3B3EAC',
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
export const BAR_CHART_FILL = '#4575b4';
|
|
42
|
+
export const CHART_MISSING_FILL = '#bbbbbb';
|
|
43
|
+
|
|
44
|
+
export const DEFAULT_CHART_THEME: ChartTheme = {
|
|
45
|
+
pie: {
|
|
46
|
+
default: COLORS,
|
|
47
|
+
},
|
|
48
|
+
bar: {
|
|
49
|
+
default: {
|
|
50
|
+
fill: BAR_CHART_FILL,
|
|
51
|
+
missing: CHART_MISSING_FILL,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// ################### CHART STYLES ###################
|
|
57
|
+
|
|
58
|
+
// common
|
|
59
|
+
export const TOOL_TIP_STYLE: CSS.Properties = {
|
|
60
|
+
backgroundColor: 'rgba(255, 255, 255, 0.9)',
|
|
61
|
+
padding: '5px',
|
|
62
|
+
border: '1px solid grey',
|
|
63
|
+
boxShadow: '0px 0px 2px rgba(0, 0, 0, 0.9)',
|
|
64
|
+
borderRadius: '2px',
|
|
65
|
+
textAlign: 'left',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const LABEL_STYLE: CSS.Properties = {
|
|
69
|
+
fontWeight: 'bold',
|
|
70
|
+
fontSize: '12px',
|
|
71
|
+
padding: '0',
|
|
72
|
+
margin: '0',
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const COUNT_STYLE: CSS.Properties = {
|
|
76
|
+
fontWeight: 'normal',
|
|
77
|
+
fontSize: '11px',
|
|
78
|
+
padding: '0',
|
|
79
|
+
margin: '0',
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const CHART_WRAPPER_STYLE: CSS.Properties = {
|
|
83
|
+
display: 'flex',
|
|
84
|
+
flexDirection: 'column',
|
|
85
|
+
alignItems: 'center',
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// bar chart
|
|
89
|
+
export const TITLE_STYLE: CSS.Properties = {
|
|
90
|
+
fontStyle: 'italic',
|
|
91
|
+
fontSize: '1.5em',
|
|
92
|
+
textAlign: 'center',
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// pie chart
|
|
96
|
+
export const TEXT_STYLE: CSS.Properties = {
|
|
97
|
+
fontSize: '11px',
|
|
98
|
+
fill: '#333',
|
|
99
|
+
};
|
|
100
|
+
export const COUNT_TEXT_STYLE: CSS.Properties = {
|
|
101
|
+
fontSize: '10px',
|
|
102
|
+
fill: '#999',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// ################### CHART CONSTANTS ###################
|
|
106
|
+
// bar chart
|
|
107
|
+
export const ASPECT_RATIO = 1.2;
|
|
108
|
+
export const MAX_TICK_LABEL_CHARS = 15;
|
|
109
|
+
export const UNITS_LABEL_OFFSET = -75;
|
|
110
|
+
export const TICKS_SHOW_ALL_LABELS_BELOW = 11; // Below this # of X-axis ticks, force-show all labels
|
|
111
|
+
export const TICK_MARGIN = 5; // vertical spacing between tick line and tick label
|
|
112
|
+
|
|
113
|
+
// pie chart
|
|
114
|
+
export const CHART_ASPECT_RATIO = 1.4;
|
|
115
|
+
export const LABEL_THRESHOLD = 0.05;
|
|
116
|
+
export const MAX_LABEL_CHARS = 14;
|
|
117
|
+
export const OTHER_THRESHOLD = 0.01;
|
|
118
|
+
|
|
119
|
+
// ################### UTIL CONSTANTS ###################
|
|
120
|
+
export const RADIAN = Math.PI / 180;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export interface ChartDataType extends Array<ChartDataItem> {}
|
|
2
|
+
|
|
3
|
+
export interface ChartDataItem {
|
|
4
|
+
x: string;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface TooltipPayload extends Array<TooltipPayloadItem> {}
|
|
9
|
+
|
|
10
|
+
interface TooltipPayloadItem {
|
|
11
|
+
name: string;
|
|
12
|
+
payload: {
|
|
13
|
+
x: string;
|
|
14
|
+
};
|
|
15
|
+
value: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type HexColor = `#${string}`;
|
|
19
|
+
|
|
20
|
+
export type ChartTheme = {
|
|
21
|
+
pie: {
|
|
22
|
+
[key in string]: HexColor[];
|
|
23
|
+
} & {
|
|
24
|
+
default: HexColor[];
|
|
25
|
+
};
|
|
26
|
+
bar: {
|
|
27
|
+
[key in string]: { fill: HexColor; missing: HexColor };
|
|
28
|
+
} & {
|
|
29
|
+
default: { fill: HexColor; missing: HexColor };
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type FilterCallback<T> = (value: T, index: number, array: T[]) => boolean;
|
|
34
|
+
export type UnitaryMapCallback<T> = (value: T, index: number, array: T[]) => T;
|
|
35
|
+
// export type BinaryMapCallback<T, U> = (value: T, index: number, array: T[]) => U;
|
|
36
|
+
|
|
37
|
+
export type ChartFilterCallback = FilterCallback<ChartDataItem>;
|
|
38
|
+
|
|
39
|
+
export type SupportedLng = 'en' | 'fr';
|
|
40
|
+
|
|
41
|
+
type TranslationWords = 'Count' | 'Other';
|
|
42
|
+
|
|
43
|
+
export type LngDictionary = {
|
|
44
|
+
[key in TranslationWords]: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type TranslationObject = {
|
|
48
|
+
[key in SupportedLng]: LngDictionary;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// ################### COMPONENT PROPS #####################
|
|
52
|
+
interface BaseChartProps {
|
|
53
|
+
data: ChartDataType;
|
|
54
|
+
height: number;
|
|
55
|
+
preFilter?: ChartFilterCallback;
|
|
56
|
+
dataMap?: UnitaryMapCallback<ChartDataItem>;
|
|
57
|
+
postFilter?: ChartFilterCallback;
|
|
58
|
+
removeEmpty?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface PieChartProps extends BaseChartProps {
|
|
62
|
+
colorTheme?: keyof ChartTheme['pie'];
|
|
63
|
+
sort?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface BarChartProps extends BaseChartProps {
|
|
67
|
+
colorTheme?: keyof ChartTheme['bar'];
|
|
68
|
+
title?: string;
|
|
69
|
+
units: string;
|
|
70
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./dist/",
|
|
4
|
+
"sourceMap": true,
|
|
5
|
+
"strictNullChecks": true,
|
|
6
|
+
"module": "es6",
|
|
7
|
+
"target": "es5",
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"noFallthroughCasesInSwitch": true,
|
|
15
|
+
"moduleResolution": "node",
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"isolatedModules": true,
|
|
18
|
+
"noEmit": false,
|
|
19
|
+
"jsx": "react-jsx"
|
|
20
|
+
},
|
|
21
|
+
"include": [
|
|
22
|
+
"./src/"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
mode: 'production',
|
|
3
|
+
entry: './src/index.js',
|
|
4
|
+
output: {
|
|
5
|
+
filename: 'BentoCharts.js',
|
|
6
|
+
library: 'bento-charts',
|
|
7
|
+
libraryTarget: 'umd',
|
|
8
|
+
globalObject: 'this',
|
|
9
|
+
},
|
|
10
|
+
module: {
|
|
11
|
+
rules: [
|
|
12
|
+
{ test: /\.[tj](sx|s)?$/, use: { loader: 'ts-loader' }, exclude: /node_modules/ }
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
resolve: {
|
|
16
|
+
extensions: ['.tsx', '.ts', '.js'],
|
|
17
|
+
},
|
|
18
|
+
};
|