UNPKG

2.02 kBTypeScriptView Raw
1import { isFunction } from '@antv/util';
2import { jsx } from '../../jsx';
3
4const Marker = ({ type, color }) => {
5 if (type === 'square') {
6 return (
7 <rect
8 style={{
9 width: '12px',
10 height: '12px',
11 marginRight: '10px',
12 }}
13 attrs={{
14 fill: color,
15 }}
16 />
17 );
18 }
19 return (
20 <circle
21 style={{
22 width: '12px',
23 height: '12px',
24 marginRight: '10px',
25 }}
26 attrs={{
27 fill: color,
28 }}
29 />
30 );
31};
32
33export default (props) => {
34 const {
35 items,
36 itemWidth,
37 itemFormatter,
38 style,
39 marker = 'circle', // 图例标记默认为 circle
40 nameStyle,
41 valueStyle,
42 valuePrefix
43 } = props;
44
45 const formatValue = (value, valuePrefix = ": ") => {
46 return `${valuePrefix}${value}`;
47 };
48
49 return (
50 <group style={style}>
51 {items.map((item) => {
52 const { color, name, value, filtered, tickValue } = item;
53 const valueText = isFunction(itemFormatter) ? itemFormatter(value, tickValue) : value;
54 return (
55 <group
56 className="legend-item"
57 style={{
58 width: itemWidth,
59 display: 'flex',
60 flexDirection: 'row',
61 alignItems: 'center',
62 justifyContent: 'flex-start',
63 padding: ['6px', '6px', '6px', 0],
64 }}
65 data-item={item}
66 >
67 <Marker color={filtered ? '#bfbfbf' : color} type={marker} />
68 <text
69 attrs={{
70 fill: filtered ? '#bfbfbf' : '#808080',
71 text: name,
72 ...nameStyle,
73 }}
74 />
75 { valueText ? (
76 <text
77 attrs={{
78 fill: '#808080',
79 text: formatValue(valueText, valuePrefix),
80 ...valueStyle,
81 }}
82 />
83 ) : null}
84 </group>
85 );
86 })}
87 </group>
88 );
89};