1- import React from 'react' ;
1+ import React , { useMemo } from 'react' ;
22import { v4 } from 'uuid' ;
33import {
44 getNumberOfDots ,
@@ -8,8 +8,10 @@ import {
88import {
99 Elements ,
1010 DEFAULT_COLUMNS ,
11- DEFAULT_ROWS
12- } from "./constants" ;
11+ DEFAULT_ROWS ,
12+ DEFAULT_ROW_WIDTH ,
13+ DEFAULT_ROW_GAP
14+ } from './constants' ;
1315import { ChartProps , DataPointType } from './types' ;
1416import classes from './styles.module.scss' ;
1517
@@ -27,13 +29,10 @@ const Chart = (props: ChartProps) : JSX.Element => {
2729 columns = DEFAULT_COLUMNS
2830 } = dimensions ;
2931
30- const getDotWidth = ( ) : number => {
31- let dotWidth = 35 ;
32- if ( width ) {
33- dotWidth = width / columns - 19 ;
34- }
35- return dotWidth ;
36- }
32+ const dotWidth = useMemo (
33+ ( ) => ( width ? width / columns - DEFAULT_ROW_GAP : DEFAULT_ROW_WIDTH ) ,
34+ [ width ]
35+ ) ;
3736 return (
3837 < div
3938 className = { classes . dotsContainer }
@@ -49,20 +48,22 @@ const Chart = (props: ChartProps) : JSX.Element => {
4948 < div
5049 className = { classes . eachDot }
5150 style = { {
52- backgroundImage : `linear-gradient(to right, ${ data [ rowIndex - 1 ] . color } 20%, ${ dataItem ?. color } 50%)` ,
53- width : `${ getDotWidth ( ) } px` ,
54- height : `${ getDotWidth ( ) } px` ,
55- ...( getStyles ( Elements . Dot , styles ) )
51+ backgroundImage : `linear-gradient(to right, ${
52+ data [ rowIndex - 1 ] . color
53+ } 20%, ${ dataItem ?. color } 50%)`,
54+ width : `${ dotWidth } px` ,
55+ height : `${ dotWidth } px` ,
56+ ...getStyles ( Elements . Dot , styles )
5657 } }
5758 />
5859 ) ) || (
5960 < div
6061 className = { classes . eachDot }
6162 style = { {
6263 backgroundColor : dataItem ?. color ,
63- width : `${ getDotWidth ( ) } px` ,
64- height : `${ getDotWidth ( ) } px` ,
65- ...( getStyles ( Elements . Dot , styles ) )
64+ width : `${ dotWidth } px` ,
65+ height : `${ dotWidth } px` ,
66+ ...getStyles ( Elements . Dot , styles )
6667 } }
6768 key = { v4 ( ) }
6869 id = { `each-category-${ rowIndex } -${ columnIndex } ` }
@@ -73,6 +74,6 @@ const Chart = (props: ChartProps) : JSX.Element => {
7374 </ React . Fragment >
7475 ) ) }
7576 </ div >
76- )
77+ ) ;
7778} ;
78- export default Chart ;
79+ export default Chart ;
0 commit comments