Skip to content

Commit b757c12

Browse files
code refactoring and readme file updation
1 parent 4ebfdd2 commit b757c12

File tree

7 files changed

+99
-76
lines changed

7 files changed

+99
-76
lines changed

README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Try tweaking a dot matrix using this codesandbox link <a href="https://codesandb
1919

2020
The easiest way to use react-dot-matrix-chart is to install it from npm and build it into your app with Webpack.
2121

22-
```
22+
```bash
2323
npm install react-dot-matrix-chart
2424
```
2525

@@ -33,7 +33,7 @@ You’ll need to install React separately since it isn't included in the package
3333

3434
React Dot Matrix Chart can run in a very basic mode by just providing the `dataPoints` like given below:
3535

36-
```
36+
```jsx
3737

3838
import DotMatrix from 'react-dot-matrix-chart';
3939

@@ -53,9 +53,8 @@ The datapoints is an array of objects with the following keys:
5353

5454
An example for dataPoints array is shown below:
5555

56-
```
57-
58-
dataPointsArray = [
56+
```jsx
57+
const dataPointsArray = [
5958
{
6059
name: 'Category 1',
6160
count: 10,
@@ -77,32 +76,39 @@ dataPointsArray = [
7776

7877
You can use `title` prop to add a Title value to the dot matrix chart
7978

80-
```
81-
79+
```jsx
8280
<DotMatrix
8381
dataPoints={dataPointsArray}
8482
title="Dot Matrix"
8583
/>
86-
8784
```
8885

8986
You can specify the number of rows or columns to be present in the chart as well using the dimensions prop.
9087

91-
```
88+
```jsx
9289
<DotMatrix
9390
dataPoints={dataPointsArray}
9491
dimensions={
9592
rows: 5,
9693
columns: 10
9794
}
9895
/>
99-
10096
```
10197

10298
If the count given in the dataPoints array results in a partial percentage (decimal value), a gradient dot will be displayed as shown below
99+
<div align="center">
100+
<img src="./screenshotPartial.png" alt="" width="208" height="199"/>
101+
</div>
103102

104-
103+
We can also control the display of the legend consisting of the details regarding the colour distribution using the props 'showLegend' and 'legendPosition' as follows.
105104

105+
```jsx
106+
<DotMatrix
107+
dataPoints={dataPointsArray}
108+
showLegend={true}
109+
legendPosition="top"
110+
/>
111+
```
106112
## Props
107113

108114

@@ -146,6 +152,20 @@ Props that can be passed to the component are listed below:
146152
</td>
147153
<td><code>undefined</code></td>
148154
</tr>
155+
<tr>
156+
<td><code><b>showLegend?:</b> boolean</code></td>
157+
<td>
158+
To specify whether to show the legend or not
159+
</td>
160+
<td><code>false</code></td>
161+
</tr>
162+
<tr>
163+
<td><code><b>legendPosition?:</b> string</code></td>
164+
<td>
165+
To specify the position of the legend. The values that can be passes using this prop are 'left', 'right', 'top' and 'bottom'
166+
</td>
167+
<td><code>right</code></td>
168+
</tr>
149169
</tbody>
150170
</table>
151171

@@ -155,7 +175,7 @@ Props that can be passed to the component are listed below:
155175
All the default styles provided by this package are overridable using the `style` prop.
156176
the below code shows all the overridable styles:
157177

158-
```
178+
```jsx
159179
<DotMatrix
160180
dataPoints={dataPointsArray}
161181
styles={{

screenshotPartial.png

6.94 KB
Loading

src/lib/dot-matrix/Chart.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react';
22
import { v4 } from 'uuid';
3-
import { getNumberOfDots, getContainerWidth } from './utils/utils';
3+
import {
4+
getNumberOfDots,
5+
getContainerWidth,
6+
getStyles
7+
} from './utils/utils';
48
import {
59
Elements,
610
DEFAULT_COLUMNS,
@@ -12,7 +16,7 @@ import classes from './styles.module.scss';
1216
const Chart = (props: ChartProps) : JSX.Element => {
1317
const {
1418
dimensions = {},
15-
getStyles,
19+
styles,
1620
data,
1721
overlappingValues,
1822
total
@@ -21,32 +25,36 @@ const Chart = (props: ChartProps) : JSX.Element => {
2125
rows = DEFAULT_ROWS,
2226
columns = DEFAULT_COLUMNS
2327
} = dimensions;
28+
29+
const hasOverlapping = (values: number[], indexRow: number, indexColumn: number): boolean => (
30+
indexColumn === 0 && indexRow > 0 && values[indexRow - 1] < 1 && values[indexRow - 1] !== 0
31+
);
2432
return (
2533
<div
2634
className={classes.dotsContainer}
2735
style={{
2836
width: `${getContainerWidth(columns)}rem`,
29-
...getStyles(Elements.DotsContainer)
37+
...getStyles(Elements.DotsContainer, styles)
3038
}}
3139
>
32-
{data?.map((eachPoint: DataPointType, rowIndex: number) => (
40+
{data?.map((dataItem: DataPointType, rowIndex: number) => (
3341
<React.Fragment key={v4()}>
34-
{eachPoint && Array.apply(null, Array(getNumberOfDots(eachPoint, rows, columns, total))).map((item: null, columnIndex: number) => (
42+
{dataItem && Array.apply(null, Array(getNumberOfDots(dataItem, rows, columns, total))).map((item: null, columnIndex: number) => (
3543
<div id="dot-matrix-dots" key={v4()}>
36-
{(columnIndex === 0 && rowIndex > 0 && overlappingValues[rowIndex - 1] < 1 && overlappingValues[rowIndex - 1] !== 0 && (
44+
{(hasOverlapping(overlappingValues, rowIndex, columnIndex) && (
3745
<div
3846
className={classes.eachDot}
3947
style={{
40-
backgroundImage: `linear-gradient(to right, ${data[rowIndex - 1].color} 20%, ${eachPoint?.color} 50%)`,
41-
...(getStyles(Elements.Dot))
48+
backgroundImage: `linear-gradient(to right, ${data[rowIndex - 1].color} 20%, ${dataItem?.color} 50%)`,
49+
...(getStyles(Elements.Dot, styles))
4250
}}
4351
/>
4452
)) || (
4553
<div
4654
className={classes.eachDot}
4755
style={{
48-
backgroundColor: eachPoint?.color,
49-
...(getStyles(Elements.Dot))
56+
backgroundColor: dataItem?.color,
57+
...(getStyles(Elements.Dot, styles))
5058
}}
5159
key={v4()}
5260
id={`each-category-${rowIndex}-${columnIndex}`}

src/lib/dot-matrix/DotMatrix.tsx

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DotMatrixPropType } from "./types";
44
import { useDotMatrix } from './custom-hooks/useDotMatrix';
55
import Chart from './Chart';
66
import Legend from './Legend';
7+
import { getLegendPosition, getStyles } from "./utils/utils";
78
import {
89
Elements,
910
DEFAULT_COLUMNS,
@@ -23,58 +24,25 @@ const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
2324
} = props;
2425

2526
const [data, total, overlappingValues] = useDotMatrix(dataPoints, dimensions);
26-
const getStyles = (element: Elements): object => {
27-
const getElementStyle = styles[element];
28-
if (getElementStyle) {
29-
return getElementStyle();
30-
}
31-
return {};
32-
};
33-
34-
35-
const getLegendPosition = (): {flexDirection: string, alignItems: string} => {
36-
let flexDirection = '';
37-
let alignItems = 'end';
38-
switch (legendPosition) {
39-
case 'left':
40-
flexDirection = 'row-reverse';
41-
break;
42-
case 'right':
43-
flexDirection = 'row';
44-
break;
45-
case 'top':
46-
flexDirection = 'column-reverse';
47-
alignItems = 'center';
48-
break;
49-
case 'bottom':
50-
flexDirection = 'column';
51-
alignItems = 'center';
52-
break;
53-
default:
54-
flexDirection = 'row'
55-
break;
56-
}
57-
return { flexDirection, alignItems};
58-
}
5927
return (
6028
<div className={classes.container}>
6129
<div
6230
className={classes.dotsWithLegend}
6331
style={{
64-
...getStyles(Elements.Container),
65-
...(getLegendPosition() as React.CSSProperties)
32+
...getStyles(Elements.Container, styles),
33+
...(getLegendPosition(legendPosition) as React.CSSProperties)
6634
}}
6735
>
6836
<Chart
69-
getStyles={getStyles}
37+
styles={styles}
7038
dimensions={dimensions}
7139
data={data}
7240
total={total}
7341
overlappingValues={overlappingValues}
7442
/>
7543
{showLegend && (
7644
<Legend
77-
getStyles={getStyles}
45+
styles={styles}
7846
data={data}
7947
/>
8048
)}

src/lib/dot-matrix/Legend.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { v4 } from 'uuid';
3+
import { getStyles } from './utils/utils';
34
import { DataPointType, LegendProps } from "./types";
45
import {
56
Elements
@@ -8,26 +9,26 @@ import classes from './styles.module.scss';
89

910
const Legend = (props: LegendProps): JSX.Element => {
1011
const {
11-
getStyles,
12+
styles,
1213
data
1314
} = props;
1415
return (
1516
<div
1617
className={classes.legends}
17-
style={{ ...getStyles(Elements.LegendContainer)}}
18+
style={{ ...getStyles(Elements.LegendContainer, styles)}}
1819
>
1920
{data?.map((point: DataPointType) => (
2021
<div className={classes.legend} key={v4()}>
2122
<div
2223
className={classes.legendDot}
2324
style={{
2425
backgroundColor: point?.color,
25-
...(getStyles(Elements.LegendDot))
26+
...(getStyles(Elements.LegendDot, styles))
2627
}}
2728
/>
2829
<div
2930
className={classes.name}
30-
style={{ ...getStyles(Elements.LegendName)}}
31+
style={{ ...getStyles(Elements.LegendName, styles)}}
3132
>
3233
{point.name}
3334
</div>

src/lib/dot-matrix/types.d.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface DataPointType {
55
}
66
export interface ChartProps {
77
dimensions?: DimensionProp,
8-
getStyles: (string) => {},
8+
styles: StyleProp,
99
data: DataPointType[],
1010
overlappingValues: number[],
1111
total: number
@@ -16,22 +16,23 @@ export type DimensionProp = {
1616
columns?: number
1717
}
1818

19+
export type StyleProp = {
20+
Dot?: () => {},
21+
DotsContainer?: () => {},
22+
Container?: () => {},
23+
LegendContainer?: () => {},
24+
LegendName?: () => {},
25+
LegendDot?: () => {}
26+
}
1927
export interface LegendProps {
20-
getStyles: (string) => {},
28+
styles: StyleProp,
2129
data: DataPointType[]
2230
}
2331
export interface DotMatrixPropType {
2432
dataPoints: DataPointType[],
2533
dimensions?: DimensionProp,
2634
showLegend?: boolean,
2735
legendPosition?: 'left' | 'right' | 'top' | 'bottom'
28-
styles?: {
29-
Dot?: () => {},
30-
DotsContainer?: () => {},
31-
Container?: () => {},
32-
LegendContainer?: () => {},
33-
LegendName?: () => {},
34-
LegendDot?: () => {}
35-
}
36+
styles?: StyleProp
3637
}
3738

src/lib/dot-matrix/utils/utils.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { DataPointType } from "../types";
2-
import { CONTAINER_WIDTH_CONVERSION_FACTOR } from '../constants';
1+
import { DataPointType, StyleProp } from "../types";
2+
import { CONTAINER_WIDTH_CONVERSION_FACTOR, Elements } from '../constants';
33
export const getNumberOfDots = (point: DataPointType, rows: number, columns: number, total: number): number => {
44
const percentage = point.count / total;
55
return Math.floor(percentage * rows * columns);
@@ -11,4 +11,29 @@ export const isColorPresent = (dataPoints: DataPointType[], color: string, dataV
1111
const findColor = dataPoints?.find((e) => e.color === color);
1212
const colorInLocal = dataValues?.find((e) => e.color === color);
1313
return Boolean(findColor) || Boolean(colorInLocal);
14-
}
14+
}
15+
16+
export const getLegendPosition = (legendPosition: string): {flexDirection: string, alignItems: string} => {
17+
const flexDirection = {
18+
left: 'row-reverse',
19+
right: 'row',
20+
top: 'column-reverse',
21+
bottom: 'column'
22+
};
23+
24+
const alignment = {
25+
left: 'end',
26+
right: 'end',
27+
top: 'center',
28+
bottom: 'center'
29+
}
30+
return { flexDirection: flexDirection[legendPosition], alignItems: alignment[legendPosition]};
31+
}
32+
33+
export const getStyles = (element: Elements, styles: StyleProp): object => {
34+
const getElementStyle = styles[element];
35+
if (getElementStyle) {
36+
return getElementStyle();
37+
}
38+
return {};
39+
};

0 commit comments

Comments
 (0)