Skip to content

Commit d39c6ce

Browse files
Fix: Resolve Interval Edge Case, Console Errors & Alignment Issues (#19)
* Handled edge case of interval calculation and fixed invalid prop console error * x-axis labels aligned centrally * resolved type issue related to custom style * implemented a better solution to align x-axis labels centrally * updated storybook data * center align x-axis markings * Allow value box to take height on context instead of fixed height * updated testcase
1 parent 6e80cdd commit d39c6ce

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

src/data/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const data2 = [
134134

135135
export const data3 = [
136136
{
137-
x: 1672617600000,
137+
x: 1671117600000,
138138
y: 150
139139
},
140140
{

src/scatter-graph/DefaultValueBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DefaultValueBoxPropTypes } from '../types/types';
55
import './styles.css';
66

77
const DefaultValueBox: FC<DefaultValueBoxPropTypes> = ({ x, y }): ReactElement => (
8-
<div className='valueBox' data-testId='value-box'>
8+
<div className='valueBox' data-testid='value-box'>
99
x: {x}
1010
<br />
1111
y: {y}

src/scatter-graph/ScatterGraph.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ const ScatterGraph: FC<ScatterGraphPropTypes> = ({
116116
className='xPoints'
117117
style={{
118118
top: graphHeight + 5,
119-
left: index * (graphWidth / (xPoints.length - 1)) - 10.5,
119+
left: index * (graphWidth / (xPoints.length - 1)),
120120
...getCallableStyles(styles, CallableStyleElements.XLabel, xLabel)
121121
}}
122122
>
123-
{renderXLabel ? renderXLabel(xLabel) : xLabel}
123+
<div className='xPointsLabel'>{renderXLabel ? renderXLabel(xLabel) : xLabel}</div>
124124
</div>
125125
))}
126126
</div>

src/scatter-graph/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export enum CallableStyleElements {
66
export enum GenericStyleElements {
77
Root = 'Root'
88
}
9+
10+
export const DEFAULT_INTERVAL = 1;

src/scatter-graph/styles.css

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
display: flex;
2121
}
2222

23+
.xPointsLabel {
24+
transform: translateX(-50%);
25+
}
26+
2327
.xPoints {
2428
position: absolute;
2529
line-height: 16px;
26-
text-align: right;
30+
text-align: center;
2731
}
2832

2933
.xPoints::before {
@@ -34,11 +38,11 @@
3438
background-color: #9e9e9e;
3539
position: relative;
3640
top: -8px;
37-
left: 10px;
41+
left: -0.5px;
3842
}
3943

4044
.yPointsContainer {
41-
padding-right: 6px;
45+
padding: 0 6px 0 8px;
4246
}
4347

4448
.yPoints {
@@ -60,17 +64,15 @@
6064
}
6165

6266
.valueBox {
63-
height: 40px;
6467
border: 1px solid gray;
6568
border-radius: 5px;
6669
background-color: white;
6770
box-shadow: 3px 3px 4px 0px rgba(189, 189, 189, 1);
6871
transition: all 0.5s ease;
69-
padding: 4px 10px;
72+
padding: 5px 10px;
7073
}
7174

7275
.valueBoxTest {
73-
height: 40px;
7476
border: 1px solid gray;
7577
border-radius: 5px;
7678
background-color: white;

src/scatter-graph/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { AxisRanges, AxisRangesArguments, CustomStyles } from '../types/types';
2-
import { CallableStyleElements } from './constants';
2+
import { CallableStyleElements, DEFAULT_INTERVAL } from './constants';
33

44
// This function takes in a range of values and calculates a suitable interval to
55
// divide that range into smaller segments for display purposes.
66
const getInterval = (min: number, max: number): number => {
77
const range = max - min;
8+
9+
/* Important condition, Do not remove without proper testing!
10+
In the below algorithm, Math.log10(range) returns (-Infinity), when range = 0, causing bugs.
11+
This condition bypasses the above flaw */
12+
if (range === 0) return DEFAULT_INTERVAL;
13+
814
// This formula calculates an interval that is a power of 10,
915
// such as 1, 10, 100, etc., based on the magnitude of the range.
1016
let interval = Math.pow(10, Math.floor(Math.log10(range)));
@@ -51,7 +57,7 @@ export const getAxisRanges = (testData: AxisRangesArguments): AxisRanges => {
5157

5258
// function to extract the style applicable to a given element when a function is exposed to user
5359
export const getCallableStyles = (
54-
allStyles: CustomStyles,
60+
allStyles: CustomStyles | object,
5561
element: CallableStyleElements,
5662
id: number | string
5763
): object => {

src/tests/ScatterGraph.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GraphPoint } from '../types/types';
99
const getById = queryByAttribute.bind(null, 'id');
1010

1111
test('Scatter graph Component - simple', async () => {
12-
const dom = render(<ReactScatterGraph data={data} graphHeight={500} />);
12+
const dom = render(<ReactScatterGraph data={[{ x: 1, y: 1 }]} graphHeight={500} />);
1313
const label = await getById(dom.container, 'graph-svg-wrapper');
1414
expect(label).not.toBeNull();
1515
});

src/types/types.d.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ type StyleObjectType = React.CSSProperties;
1818

1919
type StyleFunction = (value: number | string) => StyleObjectType;
2020

21-
type GenericStyles = {
22-
[value in GenericStyleElements]?: StyleObjectType;
21+
export type CustomStyles = {
22+
[GenericStyleElements.Root]: StyleObjectType;
23+
[CallableStyleElements.XLabel]: StyleFunction;
24+
[CallableStyleElements.YLabel]: StyleFunction;
2325
};
2426

25-
export type CallableStyles = {
26-
[value in CallableStyleElements]?: StyleFunction;
27-
};
28-
29-
export type CustomStyles = CallableStyles & GenericStyles;
30-
3127
export type ScatterGraphPropTypes = {
3228
data: Array<GraphPoint>;
3329
graphHeight: number;

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"importHelpers": true,
88
"rootDir": "src",
99
"outDir": "build",
10-
"strict": true,
1110
"noImplicitReturns": true,
1211
"noFallthroughCasesInSwitch": true,
1312
"noUnusedLocals": true,

0 commit comments

Comments
 (0)