All files / Points Points.stories.tsx

100% Statements 26/26
100% Branches 0/0
100% Functions 4/4
100% Lines 26/26

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                                                1x     1x 1x 1x   1x 1x   1x                   1x 1x       4x           1x         1x         1x                               1x     1x 1x 1x   1x 1x   1x                   1x 1x       4x           1x         1x         1x                                        
import _ from 'lodash';
import React from 'react';
import createClass from 'create-react-class';
import { Meta, Story } from '@storybook/react';
 
import * as chartConstants from '../../constants/charts';
import * as d3Scale from 'd3-scale';
import Points from './Points';
import { IPointsProps } from '../Points/Points';
 
export default {
	title: 'Visualizations/Points',
	component: Points,
	parameters: {
		docs: {
			description: {
				component: Points.peek.description,
			},
		},
	},
	args: Points.defaultProps,
} as Meta;
 
/* Basic Points */
export const Basic: Story<IPointsProps> = (args) => {
	/* eslint-disable comma-spacing */
 
	const width = 1000;
	const height = 400;
	const margin = { top: 10, right: 10, bottom: 10, left: 10 };
 
	const innerWidth = width - margin.left - margin.right;
	const innerHeight = height - margin.top - margin.bottom;
 
	const data = [
		{ x: 'one', y0: 4, y1: 5, y2: 6, y3: 7 },
		{ x: 'two', y0: 3, y1: 4, y2: 5, y3: 6 },
		{ x: 'three', y0: 2, y1: 3, y2: 4, y3: 5 },
		{ x: 'four', y0: 1, y1: 2, y2: 3, y3: 4 },
		{ x: 'five', y0: 2, y1: 3, y2: 4, y3: 5 },
		{ x: 'six', y0: 3, y1: 4, y2: 5, y3: 6 },
		{ x: 'seven', y0: 4, y1: 5, y2: 6, y3: 7 },
	];
 
	const yFields = ['y0', 'y1', 'y2', 'y3'];
	const yMax: any = _.max(
		_.reduce(
			yFields,
			(acc, field) => {
				return acc.concat(_.map(data, field) as any);
			},
			[]
		)
	);
 
	const xScale: any = d3Scale
		.scalePoint()
		.domain(_.map(data, 'x'))
		.range([0, innerWidth]);
 
	const yScale = d3Scale
		.scaleLinear()
		.domain([0, yMax])
		.range([innerHeight, 0]);
 
	return (
		<svg width={width} height={height}>
			<g transform={`translate(${margin.left}, ${margin.top})`}>
				<Points
					{...args}
					data={data}
					xScale={xScale}
					yScale={yScale}
					yFields={yFields}
				/>
			</g>
		</svg>
	);
};
 
/* Custom Colors */
export const CustomColors: Story<IPointsProps> = (args) => {
	/* eslint-disable comma-spacing */
 
	const width = 1000;
	const height = 400;
	const margin = { top: 10, right: 10, bottom: 10, left: 10 };
 
	const innerWidth = width - margin.left - margin.right;
	const innerHeight = height - margin.top - margin.bottom;
 
	const data = [
		{ x: 'one', y0: 4, y1: 5, y2: 6, y3: 7 },
		{ x: 'two', y0: 3, y1: 4, y2: 5, y3: 6 },
		{ x: 'three', y0: 2, y1: 3, y2: 4, y3: 5 },
		{ x: 'four', y0: 1, y1: 2, y2: 3, y3: 4 },
		{ x: 'five', y0: 2, y1: 3, y2: 4, y3: 5 },
		{ x: 'six', y0: 3, y1: 4, y2: 5, y3: 6 },
		{ x: 'seven', y0: 4, y1: 5, y2: 6, y3: 7 },
	];
 
	const yFields = ['y0', 'y1', 'y2', 'y3'];
	const yMax: any = _.max(
		_.reduce(
			yFields,
			(acc, field) => {
				return acc.concat(_.map(data, field) as any);
			},
			[]
		)
	);
 
	const xScale: any = d3Scale
		.scalePoint()
		.domain(_.map(data, 'x'))
		.range([0, innerWidth]);
 
	const yScale = d3Scale
		.scaleLinear()
		.domain([0, yMax])
		.range([innerHeight, 0]);
 
	return (
		<svg width={width} height={height}>
			<g transform={`translate(${margin.left}, ${margin.top})`}>
				<Points
					{...args}
					data={data}
					xScale={xScale}
					yScale={yScale}
					yFields={yFields}
					colorMap={{
						y0: chartConstants.COLOR_BAD,
						y1: chartConstants.COLOR_GOOD,
						y2: '#ff8800',
						y3: '#abc123',
					}}
				/>
			</g>
		</svg>
	);
};