All files / Point Point.tsx

100% Statements 15/15
100% Branches 2/2
100% Functions 1/1
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                176x   176x       176x                                                     176x                 176x                     45x   45x   45x 45x       45x               45x                                 176x   176x   176x                   176x                                                                                  
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps, StandardProps } from '../../util/component-types';
import { transformFromCenter } from '../../util/chart-helpers';
import * as chartConstants from '../../constants/charts';
 
const cx = lucidClassNames.bind('&-Point');
 
const { number, bool, string, object } = PropTypes;
 
// These were originally built in a 12x12 grid, except triangles which were
// 14x12 cause triangles are poo.
const PATHS = [
	'M6,12 C2.686,12 0,9.314 0,6 C0,2.686 2.686,0 6,0 C9.314,-0 12,2.686 12,6 C12,9.314 9.314,12 6,12 z',
	'M6,12 C0,12 0,12 0,6 C0,0 -0,0 6,0 C12,0 12,0 12,6 C12,12 12,12 6,12 z',
	'M6.034,1.656 C7,0 7,0 7.966,1.656 L13.034,10.344 C14,12 13,12 12,12 L2,12 C1,12 0,12 0.966,10.344 L6.034,1.656 z',
	'M7.966,10.344 C7,12 7,12 6.034,10.344 L0.966,1.656 C-0,0 1,0 2,0 L12,0 C13,0 14,0 13.034,1.656 L7.966,10.344 z',
	'M2.594,9.406 C-0.812,6 -0.812,6 2.594,2.594 C6,-0.812 6,-0.812 9.406,2.594 C12.812,6 12.812,6 9.406,9.406 C6,12.812 6,12.812 2.594,9.406 z',
];
 
export enum Kind {
	Circle,
	Square,
	TriangleUp,
	TriangleDown,
	Diamond,
}
 
export interface IPointProps
	extends StandardProps,
		React.SVGProps<SVGPathElement> {
	hasStroke: boolean;
	kind: Kind;
	color: string;
	scale: number;
	x: number;
	y: number;
}
 
const defaultProps = {
	x: 0,
	y: 0,
	kind: 0,
	color: chartConstants.COLOR_0,
	hasStroke: false,
	scale: 1,
};
 
export const Point = (props: IPointProps): React.ReactElement => {
	const {
		color,
		hasStroke,
		kind,
		x,
		y,
		scale,
		className,
		style,
		...passThroughs
	} = props;
 
	const kindIndex = kind % 5;
 
	const isCustomColor = _.startsWith(color, '#');
	const colorStyle = isCustomColor ? { fill: color } : null;
 
	// These transforms are used to center the icon on the x y coordinate
	// provided.
	const transforms = [
		transformFromCenter(x, y, 6, 6, scale),
		transformFromCenter(x, y, 6, 6, scale),
		transformFromCenter(x, y, 7, 6, scale), // triangle
		transformFromCenter(x, y, 7, 6, scale), // triangle
		transformFromCenter(x, y, 6, 6, scale),
	];
 
	return (
		<path
			{...omitProps(passThroughs, undefined, _.keys(Point.propTypes))}
			style={{
				...style,
				...colorStyle,
			}}
			className={cx(className, '&', {
				'&-has-stroke': hasStroke,
				[`&-${color}`]: !isCustomColor,
			})}
			transform={transforms[kindIndex]}
			d={PATHS[kindIndex]}
		/>
	);
};
 
Point.defaultProps = defaultProps;
 
Point.displayName = 'Point';
 
Point.peek = {
	description: `
				*For use within an \`svg\`*
 
				Points are typically used for scatter plots or overlaying shapes on
				lines.
			`,
	categories: ['visualizations', 'geoms'],
};
 
Point.propTypes = {
	style: object`
			Passed through to the root element.
		`,
 
	className: string`
			Appended to the component-specific class names set on the root element.
		`,
 
	hasStroke: bool`
			Determines if the point has a white stroke around it.
		`,
 
	x: number`
			x coordinate
		`,
 
	y: number`
			y coordinate
		`,
 
	kind: number`
			Zero-based set of shapes. It's recommended that you pass the index of
			your array for shapes.
		`,
 
	color: string`
			Strings should match an existing color class unless they start with a '#'
			for specific colors. E.g.:
 
			- \`COLOR_0\`
			- \`COLOR_GOOD\`
			- \`'#123abc'\`
		`,
 
	scale: number`
			Scale up the size of the symbol. 2 would be double the original size.
		`,
};
 
export default Point;