All files / Bars Bars.stories.tsx

100% Statements 30/30
100% Branches 0/0
100% Functions 6/6
100% Lines 30/30

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 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                                          1x     1x 1x   1x                 1x 1x       4x           1x             1x         1x       1x                                                         1x     1x 1x   1x                 1x 1x       4x           1x             1x         1x       1x                                         1x 1x               1x 1x   1x   1x         1x             5x                                
import _ from 'lodash';
import React from 'react';
import { Meta, Story } from '@storybook/react';
 
import Bars, { IBarsProps } from '../Bars/Bars';
import * as chartConstants from '../../constants/charts';
import * as d3Scale from 'd3-scale';
 
export default {
	title: 'Private/Bars',
	component: Bars,
	parameters: {
		docs: {
			description: {
				component: Bars.peek.description,
			},
		},
	},
} as Meta;
 
/* Basic */
export const Basic: Story<IBarsProps> = (args) => {
	/* eslint-disable comma-spacing */
 
	const width = 750;
	const height = 400;
 
	const data: Array<{ [key: string]: string | number }> = [
		{ x: 'one', y0: 1, y1: 2, y2: 3, y3: 5 },
		{ x: 'two', y0: 2, y1: 3, y2: 4, y3: 6 },
		{ x: 'three', y0: 2, y1: 4, y2: 5, y3: 6 },
		{ x: 'four', y0: 3, y1: 6, y2: 7, y3: 7 },
		{ x: 'five', y0: 4, y1: 8, y2: 9, y3: 8 },
		{ x: 'six', y0: 11, y1: 8, y2: 9, y3: 5 },
	];
 
	const yFields = ['y0', 'y1', 'y2', 'y3'];
	const yMax = _.max(
		_.reduce(
			yFields,
			(acc: number[], field: string) => {
				return acc.concat(_.map(data, field) as number[]);
			},
			[]
		)
	);
 
	const xScale = d3Scale
		.scaleBand()
		.domain(_.map(data, 'x') as string[])
		.range([0, width])
		.round(true)
		.paddingInner(0.1);
 
	const yScale = d3Scale
		.scaleLinear()
		.domain([0, yMax as any])
		.range([height, 0]);
 
	const style = {
		paddingTop: '9rem',
	};
 
	return (
		<div style={style}>
			<svg width={width} height={height}>
				<Bars
					{...args}
					data={data}
					xScale={xScale}
					yScale={yScale}
					yFields={yFields}
					hasToolTips
				/>
			</svg>
 
			<svg width={width} height={height}>
				<Bars
					{...args}
					data={data}
					xScale={xScale}
					yScale={yScale}
					yFields={yFields}
					isStacked={true}
					hasToolTips
				/>
			</svg>
		</div>
	);
};
 
/* Custom Colors */
export const CustomColors: Story<IBarsProps> = (args) => {
	/* eslint-disable comma-spacing */
 
	const width = 750;
	const height = 400;
 
	const data = [
		{ x: 'one', y0: 1, y1: 2, y2: 3, y3: 5 },
		{ x: 'two', y0: 2, y1: 3, y2: 4, y3: 6 },
		{ x: 'three', y0: 2, y1: 4, y2: 5, y3: 6 },
		{ x: 'four', y0: 3, y1: 6, y2: 7, y3: 7 },
		{ x: 'five', y0: 4, y1: 8, y2: 9, y3: 8 },
		{ x: 'six', y0: 11, y1: 8, y2: 9, y3: 5 },
	];
 
	const yFields = ['y0', 'y1', 'y2', 'y3'];
	const yMax = _.max(
		_.reduce(
			yFields,
			(acc, field) => {
				return acc.concat(_.map(data, field) as any);
			},
			[]
		)
	);
 
	const xScale = d3Scale
		.scaleBand()
		.domain(_.map(data, 'x'))
		.range([0, width])
		.round(true)
		.paddingInner(0.1);
 
	const yScale = d3Scale
		.scaleLinear()
		.domain([0, yMax as any])
		.range([height, 0]);
 
	const style = {
		paddingTop: '9rem',
	};
 
	return (
		<div style={style}>
			<svg width={width} height={height}>
				<Bars
					{...args}
					data={data}
					xScale={xScale}
					yScale={yScale}
					yFields={yFields}
					colorMap={{
						y0: chartConstants.COLOR_GOOD,
						y2: chartConstants.COLOR_BAD,
					}}
					hasToolTips
				/>
			</svg>
		</div>
	);
};
 
/* Log Scale */
export const LogScale: Story<IBarsProps> = (args) => {
	const data: Array<{ [key: string]: string | number }> = [
		{ x: 'one', y: 10 },
		{ x: 'two', y: 100 },
		{ x: 'three', y: 1000 },
		{ x: 'four', y: 10000 },
		{ x: 'five', y: 100000 },
	];
 
	const width = 750;
	const height = 400;
 
	const yScale = d3Scale.scaleLog().domain([1, 1000000]).range([height, 0]);
 
	const xScale = d3Scale
		.scaleBand()
		.domain(_.map(data, 'x') as string[])
		.range([0, width]);
 
	return (
		<svg height={600} width={1000}>
			<g>
				<Bars {...args} data={data} xScale={xScale} yScale={yScale} />
			</g>
			<g>
				{_.map(data, (item, idx) => {
					return (
						<text
							key={idx}
							textAnchor='middle'
							x={(xScale(item.x as string) as number) + xScale.bandwidth() / 2}
							y={(yScale(item.y as number) as any) - 10}
							fill='gray'
						>
							{item.y}
						</text>
					);
				})}
			</g>
		</svg>
	);
};