File size: 6,316 Bytes
1e92f2d | 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | /**
* @fileOverview Default Legend Content
*/
import * as React from 'react';
import { PureComponent, ReactNode, MouseEvent, ReactElement } from 'react';
import { clsx } from 'clsx';
import { Surface } from '../container/Surface';
import { Symbols } from '../shape/Symbols';
import {
DataKey,
LegendType,
LayoutType,
adaptEventsOfChild,
PresentationAttributesAdaptChildEvent,
} from '../util/types';
const SIZE = 32;
export type ContentType = ReactElement | ((props: Props) => ReactNode);
export type IconType = Exclude<LegendType, 'none'>;
export type HorizontalAlignmentType = 'center' | 'left' | 'right';
export type VerticalAlignmentType = 'top' | 'bottom' | 'middle';
export type Formatter = (value: any, entry: LegendPayload, index: number) => ReactNode;
export interface LegendPayload {
/**
* This is the text that will be displayed in the legend in the DOM.
* If undefined, the text will not be displayed, so the icon will be rendered without text.
*/
value: string | undefined;
type?: LegendType;
color?: string;
payload?: {
strokeDasharray?: number | string;
value?: any;
};
formatter?: Formatter;
inactive?: boolean;
legendIcon?: ReactElement<SVGElement>;
dataKey?: DataKey<any>;
}
interface InternalProps {
content?: ContentType;
iconSize?: number;
iconType?: IconType;
layout?: LayoutType;
align?: HorizontalAlignmentType;
verticalAlign?: VerticalAlignmentType;
inactiveColor?: string;
formatter?: Formatter;
onMouseEnter?: (data: LegendPayload, index: number, event: MouseEvent) => void;
onMouseLeave?: (data: LegendPayload, index: number, event: MouseEvent) => void;
onClick?: (data: LegendPayload, index: number, event: MouseEvent) => void;
/**
* DefaultLegendContent.payload is omitted from Legend props.
* A custom payload can be passed here if desired or it can be passed from the Legend "content" callback.
*/
payload?: ReadonlyArray<LegendPayload>;
}
export type Props = InternalProps & Omit<PresentationAttributesAdaptChildEvent<any, ReactElement>, keyof InternalProps>;
export class DefaultLegendContent extends PureComponent<Props> {
static displayName = 'Legend';
static defaultProps: Partial<Props> = {
align: 'center',
iconSize: 14,
inactiveColor: '#ccc',
layout: 'horizontal',
verticalAlign: 'middle',
};
/**
* Render the path of icon
* @param data Data of each legend item
* @param iconType if defined, it will always render this icon. If undefined then it uses icon from data.type
* @return Path element
*/
renderIcon(data: LegendPayload, iconType: IconType | undefined) {
const { inactiveColor } = this.props;
const halfSize = SIZE / 2;
const sixthSize = SIZE / 6;
const thirdSize = SIZE / 3;
const color = data.inactive ? inactiveColor : data.color;
const preferredIcon = iconType ?? data.type;
if (preferredIcon === 'none') {
return null;
}
if (preferredIcon === 'plainline') {
return (
<line
strokeWidth={4}
fill="none"
stroke={color}
strokeDasharray={data.payload.strokeDasharray}
x1={0}
y1={halfSize}
x2={SIZE}
y2={halfSize}
className="recharts-legend-icon"
/>
);
}
if (preferredIcon === 'line') {
return (
<path
strokeWidth={4}
fill="none"
stroke={color}
d={`M0,${halfSize}h${thirdSize}
A${sixthSize},${sixthSize},0,1,1,${2 * thirdSize},${halfSize}
H${SIZE}M${2 * thirdSize},${halfSize}
A${sixthSize},${sixthSize},0,1,1,${thirdSize},${halfSize}`}
className="recharts-legend-icon"
/>
);
}
if (preferredIcon === 'rect') {
return (
<path
stroke="none"
fill={color}
d={`M0,${SIZE / 8}h${SIZE}v${(SIZE * 3) / 4}h${-SIZE}z`}
className="recharts-legend-icon"
/>
);
}
if (React.isValidElement(data.legendIcon)) {
const iconProps: any = { ...data };
delete iconProps.legendIcon;
return React.cloneElement(data.legendIcon, iconProps);
}
return <Symbols fill={color} cx={halfSize} cy={halfSize} size={SIZE} sizeType="diameter" type={preferredIcon} />;
}
/**
* Draw items of legend
* @return Items
*/
renderItems() {
const { payload, iconSize, layout, formatter, inactiveColor, iconType } = this.props;
const viewBox = { x: 0, y: 0, width: SIZE, height: SIZE };
const itemStyle = {
display: layout === 'horizontal' ? 'inline-block' : 'block',
marginRight: 10,
};
const svgStyle = { display: 'inline-block', verticalAlign: 'middle', marginRight: 4 };
return payload.map((entry: LegendPayload, i: number) => {
const finalFormatter = entry.formatter || formatter;
const className = clsx({
'recharts-legend-item': true,
[`legend-item-${i}`]: true,
inactive: entry.inactive,
});
if (entry.type === 'none') {
return null;
}
const color = entry.inactive ? inactiveColor : entry.color;
const finalValue = finalFormatter ? finalFormatter(entry.value, entry, i) : entry.value;
return (
<li
className={className}
style={itemStyle}
// eslint-disable-next-line react/no-array-index-key
key={`legend-item-${i}`}
{...adaptEventsOfChild(this.props, entry, i)}
>
<Surface
width={iconSize}
height={iconSize}
viewBox={viewBox}
style={svgStyle}
aria-label={`${finalValue} legend icon`}
>
{this.renderIcon(entry, iconType)}
</Surface>
<span className="recharts-legend-item-text" style={{ color }}>
{finalValue}
</span>
</li>
);
});
}
render() {
const { payload, layout, align } = this.props;
if (!payload || !payload.length) {
return null;
}
const finalStyle = {
padding: 0,
margin: 0,
textAlign: layout === 'horizontal' ? align : 'left',
};
return (
<ul className="recharts-default-legend" style={finalStyle}>
{this.renderItems()}
</ul>
);
}
}
|