-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.tsx
More file actions
153 lines (128 loc) · 5.18 KB
/
index.tsx
File metadata and controls
153 lines (128 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React from 'react';
import {color, select} from 'd3';
import type {Dispatch} from 'd3';
import get from 'lodash/get';
import type {LabelData} from '../../../types';
import {block, filterOverlappingLabels} from '../../../utils';
import type {PreparedSeriesOptions} from '../../useSeries/types';
import {HtmlLayer} from '../HtmlLayer';
import {getRectPath} from '../utils';
import type {PreparedBarXData} from './types';
export {prepareBarXData} from './prepare-data';
export * from './types';
const b = block('bar-x');
type Args = {
dispatcher: Dispatch<object>;
preparedData: PreparedBarXData[];
seriesOptions: PreparedSeriesOptions;
htmlLayout: HTMLElement | null;
};
export const BarXSeriesShapes = (args: Args) => {
const {dispatcher, preparedData, seriesOptions, htmlLayout} = args;
const hoveredDataRef = React.useRef<PreparedBarXData[] | null | undefined>(null);
const ref = React.useRef<SVGGElement>(null);
React.useEffect(() => {
if (!ref.current) {
return () => {};
}
const svgElement = select(ref.current);
const hoverOptions = get(seriesOptions, 'bar-x.states.hover');
const inactiveOptions = get(seriesOptions, 'bar-x.states.inactive');
svgElement.selectAll('*').remove();
const rectSelection = svgElement
.selectAll('allRects')
.data(preparedData)
.join('path')
.attr('d', (d) => {
const borderRadius = d.isLastStackItem
? Math.min(d.height, d.width / 2, d.series.borderRadius)
: 0;
const p = getRectPath({
x: d.x,
y: d.y,
width: d.width,
height: d.height,
borderRadius: [borderRadius, borderRadius, 0, 0],
});
return p.toString();
})
.attr('class', b('segment'))
.attr('x', (d) => d.x)
.attr('y', (d) => d.y)
.attr('height', (d) => d.height)
.attr('width', (d) => d.width)
.attr('fill', (d) => d.data.color || d.series.color)
.attr('opacity', (d) => d.opacity)
.attr('cursor', (d) => d.series.cursor);
let dataLabels = preparedData.map((d) => d.label).filter(Boolean) as LabelData[];
if (!preparedData[0]?.series.dataLabels.allowOverlap) {
dataLabels = filterOverlappingLabels(dataLabels);
}
const labelSelection = svgElement
.selectAll('text')
.data(dataLabels)
.join('text')
.text((d) => d.text)
.attr('class', b('label'))
.attr('x', (d) => d.x)
.attr('y', (d) => d.y)
.attr('text-anchor', (d) => d.textAnchor)
.style('font-size', (d) => d.style.fontSize)
.style('font-weight', (d) => d.style.fontWeight || null)
.style('fill', (d) => d.style.fontColor || null);
function handleShapeHover(data?: PreparedBarXData[]) {
hoveredDataRef.current = data;
const hoverEnabled = hoverOptions?.enabled;
const inactiveEnabled = inactiveOptions?.enabled;
if (!data) {
if (hoverEnabled) {
rectSelection.attr('fill', (d) => d.data.color || d.series.color);
}
if (inactiveEnabled) {
rectSelection.attr('opacity', null);
labelSelection.attr('opacity', null);
}
return;
}
if (hoverEnabled) {
const hoveredValues = data.map((d) => d.data.x);
rectSelection.attr('fill', (d) => {
const fillColor = d.data.color || d.series.color;
if (hoveredValues.includes(d.data.x)) {
return (
color(fillColor)?.brighter(hoverOptions?.brightness).toString() ||
fillColor
);
}
return fillColor;
});
}
if (inactiveEnabled) {
const hoveredSeries = data.map((d) => d.series.id);
rectSelection.attr('opacity', (d) => {
return hoveredSeries.includes(d.series.id)
? null
: inactiveOptions?.opacity || null;
});
labelSelection.attr('opacity', (d) => {
return hoveredSeries.includes(d.series.id)
? null
: inactiveOptions?.opacity || null;
});
}
}
if (hoveredDataRef.current !== null) {
handleShapeHover(hoveredDataRef.current);
}
dispatcher.on('hover-shape.bar-x', handleShapeHover);
return () => {
dispatcher.on('hover-shape.bar-x', null);
};
}, [dispatcher, preparedData, seriesOptions]);
return (
<React.Fragment>
<g ref={ref} className={b()} />
<HtmlLayer preparedData={preparedData} htmlLayout={htmlLayout} />
</React.Fragment>
);
};