Skip to content

Commit 777f4f2

Browse files
Merge branch 'main' of github.com:KeyValueSoftwareSystems/react-dot-matrix-chart-
2 parents 6925d02 + 0a661b5 commit 777f4f2

File tree

14 files changed

+146
-97
lines changed

14 files changed

+146
-97
lines changed

.eslintrc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"parserOptions": {
1111
"ecmaFeatures": {
1212
"jsx": true
13-
},
14-
"project": "./tsconfig.json"
13+
}
1514
},
1615
"plugins": [
1716
"@typescript-eslint",
@@ -31,7 +30,9 @@
3130
"max-len":["warn", 150],
3231
"space-before-blocks":"error",
3332
"prefer-spread": ["off"],
34-
"react-hooks/exhaustive-deps": "off"
33+
"react-hooks/exhaustive-deps": "off",
34+
"arrow-parens": "error",
35+
"arrow-spacing": "error"
3536
},
3637
"settings": {
3738
"react": {

.github/workflows/deploy.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
uses: actions/setup-node@v2
2525
with:
2626
node-version: 16.x
27+
scope: '@keyvaluesystems'
2728

2829
- name: Install dependencies
2930
run: npm install
@@ -81,11 +82,7 @@ jobs:
8182
run: npm run build
8283

8384
- name: Publish package
84-
uses: JS-DevTools/npm-publish@v1
85-
with:
86-
access: 'public'
87-
check-version: true
88-
token: ${{ secrets.NPM_AUTH_TOKEN }}
85+
run: npm publish --access public --//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}
8986
if: success()
9087

9188
- name: Revert package.json and package-lock.json

jest.config.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
/* eslint-disable no-undef */
12
module.exports = {
2-
// Other configuration above...
3+
// Other configuration above...
34

4-
// Add the next three options if using TypeScript
5-
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
6-
preset: "ts-jest",
7-
roots: ["<rootDir>/src"],
8-
transform: {
9-
"^.+\\.[t|j]sx?$": "ts-jest",
10-
"^.+\\.svg?$": "<rootDir>/transform.js",
11-
"^.+\\.scss?$": "<rootDir>/transform.js"
12-
},
13-
"testEnvironment": "jsdom",
14-
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
5+
// Add the next three options if using TypeScript
6+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
7+
preset: "ts-jest",
8+
roots: ["<rootDir>/src"],
9+
transform: {
10+
"^.+\\.[t|j]sx?$": "ts-jest",
11+
"^.+\\.svg?$": "<rootDir>/transform.js",
12+
"^.+\\.scss?$": "<rootDir>/transform.js"
13+
},
14+
"testEnvironment": "jsdom",
15+
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$"
1516
};

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "react-dot-matrix-chart",
3-
"version": "0.2.1",
2+
"name": "@keyvaluesystems/react-dot-matrix-chart",
3+
"version": "0.2.3",
44
"description": "A Dot Matrix Chart component",
55
"main": "build/index.js",
66
"source": "src/lib/index.tsx",

src/lib/dot-matrix/Chart.tsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import { v4 } from 'uuid';
33
import {
44
getNumberOfDots,
5-
getContainerWidth,
65
getStyles,
76
hasOverlapping
87
} from './utils/utils';
98
import {
109
Elements,
1110
DEFAULT_COLUMNS,
12-
DEFAULT_ROWS
13-
} from "./constants";
11+
DEFAULT_ROWS,
12+
DEFAULT_ROW_WIDTH,
13+
DEFAULT_ROW_GAP
14+
} from './constants';
1415
import { ChartProps, DataPointType } from './types';
1516
import classes from './styles.module.scss';
1617

@@ -20,18 +21,22 @@ const Chart = (props: ChartProps) : JSX.Element => {
2021
styles,
2122
data,
2223
overlappingValues,
23-
total
24+
total,
25+
width
2426
} = props;
2527
const {
2628
rows = DEFAULT_ROWS,
2729
columns = DEFAULT_COLUMNS
2830
} = dimensions;
2931

32+
const dotWidth = useMemo(
33+
() => (width ? width / columns - DEFAULT_ROW_GAP : DEFAULT_ROW_WIDTH),
34+
[width]
35+
);
3036
return (
3137
<div
3238
className={classes.dotsContainer}
3339
style={{
34-
width: `${getContainerWidth(columns)}rem`,
3540
...getStyles(Elements.DotsContainer, styles)
3641
}}
3742
>
@@ -43,16 +48,22 @@ const Chart = (props: ChartProps) : JSX.Element => {
4348
<div
4449
className={classes.eachDot}
4550
style={{
46-
backgroundImage: `linear-gradient(to right, ${data[rowIndex - 1].color} 20%, ${dataItem?.color} 50%)`,
47-
...(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)
4857
}}
4958
/>
5059
)) || (
5160
<div
5261
className={classes.eachDot}
5362
style={{
5463
backgroundColor: dataItem?.color,
55-
...(getStyles(Elements.Dot, styles))
64+
width: `${dotWidth}px`,
65+
height: `${dotWidth}px`,
66+
...getStyles(Elements.Dot, styles)
5667
}}
5768
key={v4()}
5869
id={`each-category-${rowIndex}-${columnIndex}`}
@@ -63,6 +74,6 @@ const Chart = (props: ChartProps) : JSX.Element => {
6374
</React.Fragment>
6475
))}
6576
</div>
66-
)
77+
);
6778
};
68-
export default Chart;
79+
export default Chart;

src/lib/dot-matrix/DotMatrix.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React from "react";
1+
import React from 'react';
22
import classes from './styles.module.scss';
3-
import { DotMatrixPropType } from "./types";
3+
import { DotMatrixPropType } from './types';
44
import { useDotMatrix } from './custom-hooks/useDotMatrix';
5+
import useChartContainerWidth from './custom-hooks/useChartContainerWidth';
56
import Chart from './Chart';
67
import Legend from './Legend';
7-
import { getLegendPosition, getStyles } from "./utils/utils";
8+
import { getLegendPosition, getStyles } from './utils/utils';
89
import {
910
Elements,
1011
DEFAULT_COLUMNS,
1112
DEFAULT_ROWS,
1213
DEFAULT_LEGEND_POSITION
13-
} from "./constants";
14+
} from './constants';
1415
const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
1516
const {
1617
dataPoints,
@@ -19,11 +20,16 @@ const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
1920
columns: DEFAULT_COLUMNS
2021
},
2122
styles = {},
22-
showLegend,
23+
showLegend = false,
2324
legendPosition = DEFAULT_LEGEND_POSITION
2425
} = props;
2526

27+
const width = useChartContainerWidth('dots-container', [
28+
showLegend,
29+
legendPosition
30+
]);
2631
const [data, total, overlappingValues] = useDotMatrix(dataPoints, dimensions);
32+
2733
return (
2834
<div className={classes.container}>
2935
<div
@@ -33,22 +39,24 @@ const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
3339
...(getLegendPosition(legendPosition) as React.CSSProperties)
3440
}}
3541
>
36-
<Chart
37-
styles={styles}
38-
dimensions={dimensions}
39-
data={data}
40-
total={total}
41-
overlappingValues={overlappingValues}
42-
/>
43-
{showLegend && (
44-
<Legend
42+
<div className={classes.chartContainer} id="dots-container">
43+
<Chart
4544
styles={styles}
45+
dimensions={dimensions}
4646
data={data}
47+
total={total}
48+
width={width}
49+
overlappingValues={overlappingValues}
4750
/>
51+
</div>
52+
{showLegend && (
53+
<div>
54+
<Legend styles={styles} data={data} />
55+
</div>
4856
)}
4957
</div>
5058
</div>
51-
)
59+
);
5260
};
5361

54-
export default DotMatrix;
62+
export default DotMatrix;

src/lib/dot-matrix/constants.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
export const COLOR_PALATTE = [
2-
'#B8255F',
3-
'#96C3EB',
4-
'#DB4035',
5-
'#4073FF',
6-
'#FF9933',
7-
'#884DFF',
8-
'#FAD000',
9-
'#AF38EB',
10-
'#AFB83B',
11-
'#EB96EB',
12-
'#7ECC49',
13-
'#E05194',
14-
'#299438',
15-
'#FF8D85',
16-
'#6ACCBC',
17-
'#808080',
18-
'#158FAD',
19-
'#B8B8B8',
20-
'#14AAF5',
21-
'#CCAC93'
2+
'#fd7f6f',
3+
'#7eb0d5',
4+
'#b2e061',
5+
'#bd7ebe',
6+
'#ffb55a',
7+
'#ffee65',
8+
'#beb9db',
9+
'#fdcce5',
10+
'#8bd3c7'
2211
];
2312

2413
export enum Elements {
@@ -36,4 +25,6 @@ export const DEFAULT_COLUMNS = 12;
3625

3726
export const DEFAULT_LEGEND_POSITION = 'right';
3827

39-
export const CONTAINER_WIDTH_CONVERSION_FACTOR = 2.6;
28+
export const DEFAULT_ROW_WIDTH = 35;
29+
30+
export const DEFAULT_ROW_GAP = 6;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useState, useEffect } from 'react';
2+
import { findContainerWidth } from '../utils/utils';
3+
4+
const useChartContainerWidth = (
5+
id: string,
6+
dependencyArray: Array<boolean | string>
7+
): number => {
8+
const [width, setWidth] = useState<number>(0);
9+
10+
useEffect(() => {
11+
updateContainerWidth();
12+
}, []);
13+
14+
useEffect(() => {
15+
updateContainerWidth();
16+
}, [...dependencyArray]);
17+
18+
window.onresize = (): void => {
19+
updateContainerWidth();
20+
};
21+
22+
const updateContainerWidth = (): void => {
23+
const widthValue = findContainerWidth(id);
24+
if (widthValue) setWidth(widthValue);
25+
};
26+
return width;
27+
};
28+
29+
export default useChartContainerWidth;

src/lib/dot-matrix/styles.module.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
.container {
2-
margin: 10px;
3-
width: fit-content;
2+
padding: 10px;
3+
width: inherit;
44
.dotsWithLegend {
55
width: 100%;
66
display: flex;
7-
gap: 50px;
8-
justify-content: space-between;
7+
gap: 20px;
98
align-items: end;
109
}
1110
.dotsContainer {
1211
display: flex;
1312
flex-wrap: wrap;
1413
gap: 6px;
15-
width: 490px;
14+
width: 100%;
1615
.eachDot {
1716
border-radius: 50%;
1817
width: 2.2rem;
@@ -39,5 +38,6 @@
3938
gap: 5px;
4039
border: 1px solid gainsboro;
4140
padding: 7px;
41+
width: max-content;
4242
}
4343
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export interface ChartProps {
88
styles: StyleProp,
99
data: DataPointType[],
1010
overlappingValues: number[],
11-
total: number
11+
total: number,
12+
width: number
1213
}
1314

1415
export type DimensionProp = {

0 commit comments

Comments
 (0)