@lunit/oui 2.4.9 → 2.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
2
2
  import { findIndex as _findIndex } from 'lodash';
3
3
  import { Box } from '@mui/material';
4
4
  import { AnalysisBarSegmentContainer, AnalysisBarOuter, AxisLabels, GraphAxisLabel, GraphScoreIndicator, GraphSegment, } from './AnalysisBar.styled';
5
- import { parseStringValToNumber } from './AnalysisBar.utils';
5
+ import { parseStringValToNumber, normalizeSegmentWidths } from './AnalysisBar.utils';
6
6
  import HeatmapGraphSVG from './HeatmapGraphIcon/HeatmapGraphSVG';
7
7
  const Segments = ({ segments, isCutoff }) => {
8
8
  if (!segments) {
@@ -18,7 +18,7 @@ const Segments = ({ segments, isCutoff }) => {
18
18
  if (segment.color === 'jet') {
19
19
  return _jsx(HeatmapGraphSVG, { width: `${segment.width}%` });
20
20
  }
21
- return (_jsx(GraphSegment, { color: segment.color, width: segment.width, isFirstSegment: i === 0, isLastSegment: i === segments.length - 1, isCutoff: isCutoff, combinedWidth: combinedWidth, isNextSegment: onePercentSegmentIndex >= 0 && i === onePercentSegmentIndex + 1, isOnePercentSegment: segment.width <= 1 }, i));
21
+ return (_jsx(GraphSegment, { color: segment.color, width: segment.width, isFirstSegment: i === 0, isLastSegment: i === validSegments.length - 1, isCutoff: isCutoff, combinedWidth: combinedWidth, isNextSegment: onePercentSegmentIndex >= 0 && i === onePercentSegmentIndex + 1, isOnePercentSegment: segment.width <= 1 }, i));
22
22
  }) }));
23
23
  };
24
24
  const AnalysisBar = ({ componentType = 'analysisBar', isCutoff = false, expressionValue, caption, expressionLevels = [], expressionLabels = [], segments, isDimmed = false, }) => {
@@ -26,8 +26,8 @@ const AnalysisBar = ({ componentType = 'analysisBar', isCutoff = false, expressi
26
26
  throw new Error("The 'componentType' prop is required.");
27
27
  if (!expressionLevels.length)
28
28
  throw new Error("The 'expressionLevels' prop must contain at least one item.");
29
- const validSegments = segments.filter((segment) => segment.width > 0);
30
- return (_jsxs(AnalysisBarOuter, { className: "analysis-bar", isDimmed: isDimmed, children: [_jsxs(AnalysisBarSegmentContainer, { children: [expressionValue && (_jsx(GraphScoreIndicator, { value: expressionValue, isCutoff: isCutoff, isJet: segments?.[0].color === 'jet' })), _jsx(Segments, { segments: validSegments, isCutoff: isCutoff })] }), (expressionLevels.length > 0 || caption) && (_jsxs(AxisLabels, { children: [expressionLevels.length > 0 &&
29
+ const normalizedSegments = normalizeSegmentWidths(segments, isCutoff);
30
+ return (_jsxs(AnalysisBarOuter, { className: "analysis-bar", isDimmed: isDimmed, children: [_jsxs(AnalysisBarSegmentContainer, { children: [expressionValue && (_jsx(GraphScoreIndicator, { value: expressionValue, isCutoff: isCutoff, isJet: normalizedSegments?.[0]?.color === 'jet' })), _jsx(Segments, { segments: normalizedSegments, isCutoff: isCutoff })] }), (expressionLevels.length > 0 || caption) && (_jsxs(AxisLabels, { children: [expressionLevels.length > 0 &&
31
31
  expressionLevels.map((val, i) => {
32
32
  return (_jsx(GraphAxisLabel, { label: expressionLabels[i] || val, isCutoff: isCutoff, leftPos: parseStringValToNumber(val) }, i));
33
33
  }), caption && (_jsx(GraphAxisLabel, { label: caption, isCutoff: isCutoff, leftPos: 50 }))] }))] }));
@@ -1,3 +1,16 @@
1
1
  export declare function parseStringValToNumber(val: string): number;
2
2
  export declare function clampNumericalPercentage(value: number): number;
3
3
  export declare function clampBarLabelPosition(value: number): number;
4
+ /**
5
+ * @description normalizeSegmentWidths is a function that normalizes the width of the segments in the analysis bar.
6
+ * @param segments - The segments to normalize.
7
+ * @param isCutoff - Whether the analysis bar is a cutoff.
8
+ * @returns The normalized segments.
9
+ */
10
+ export declare function normalizeSegmentWidths(segments: {
11
+ color: 'jet' | string;
12
+ width: number;
13
+ }[], isCutoff?: boolean): {
14
+ color: 'jet' | string;
15
+ width: number;
16
+ }[];
@@ -16,3 +16,44 @@ export function clampBarLabelPosition(value) {
16
16
  return 1;
17
17
  return value;
18
18
  }
19
+ const round = (value) => Math.round(value * 10) / 10;
20
+ /**
21
+ * @description normalizeSegmentWidths is a function that normalizes the width of the segments in the analysis bar.
22
+ * @param segments - The segments to normalize.
23
+ * @param isCutoff - Whether the analysis bar is a cutoff.
24
+ * @returns The normalized segments.
25
+ */
26
+ export function normalizeSegmentWidths(segments, isCutoff) {
27
+ if (isCutoff)
28
+ return segments;
29
+ let validSegments = segments
30
+ .map((segment) => ({ ...segment, width: round(segment.width) }))
31
+ .filter((segment) => segment.width > 0);
32
+ if (validSegments.length === 0)
33
+ return [];
34
+ const normalize = (segments) => {
35
+ const total = segments.reduce((sum, s) => sum + s.width, 0);
36
+ if (total === 0 || Math.abs(total - 100) < 0.1)
37
+ return segments;
38
+ const normalized = segments.map((segment) => ({
39
+ ...segment,
40
+ width: round((segment.width / total) * 100),
41
+ }));
42
+ const normalizedTotal = normalized.reduce((sum, s) => sum + s.width, 0);
43
+ const difference = 100 - normalizedTotal;
44
+ if (normalized.length > 0 && Math.abs(difference) > 0.01) {
45
+ const lastIndex = normalized.length - 1;
46
+ normalized[lastIndex] = {
47
+ ...normalized[lastIndex],
48
+ width: round(normalized[lastIndex].width + difference),
49
+ };
50
+ }
51
+ return normalized;
52
+ };
53
+ validSegments = normalize(validSegments);
54
+ const filtered = validSegments.filter((segment) => segment.width > 0);
55
+ if (filtered.length < validSegments.length && filtered.length > 0) {
56
+ return normalize(filtered);
57
+ }
58
+ return filtered;
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunit/oui",
3
- "version": "2.4.9",
3
+ "version": "2.4.10",
4
4
  "validate-branch-name": {
5
5
  "pattern": "^(main|develop)|(feature|fix|release|hotfix|qe)/.+$",
6
6
  "errorMsg": "The branch name is not correct. Please check the pattern. (ex. feature/add-something)"