Skip to content

Commit

Permalink
Fixes for test app crashes for 12 schema data
Browse files Browse the repository at this point in the history
  • Loading branch information
srmukher committed Jan 2, 2025
1 parent df87760 commit 4de140a
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import { IVerticalBarChartProps } from '../VerticalBarChart/index';

const isDate = (value: any): boolean => !isNaN(Date.parse(value));
const isNumber = (value: any): boolean => !isNaN(parseFloat(value)) && isFinite(value);
export const isDateArray = (array: any[]): boolean => isArrayOrTypedArray(array) && array.every(isDate);
export const isDateArray = (array: any[]): boolean =>
isArrayOrTypedArray(array) && (array.every(isDate) || array.every(parseDateFromString));
export const isNumberArray = (array: any[]): boolean => isArrayOrTypedArray(array) && array.every(isNumber);
export const isMonthArray = (array: any[]): boolean => {
if (array && array.length > 0) {
Expand Down Expand Up @@ -65,6 +66,35 @@ export const updateXValues = (xValues: any[]): any[] => {
});
return xValues;
};

function parseDateFromString(dateString: string): Date | null {
// Regular expression for "YYYY-MM-DD" format
const isoDateRegex = /(\d{4})-(\d{2})-(\d{2})/;
const isoDateMatch = dateString.match(isoDateRegex);
if (isoDateMatch) {
const [_, year, month, day] = isoDateMatch;
return new Date(Number(year), Number(month) - 1, Number(day));
}

// Regular expression for "MM/DD/YYYY" format
const usDateRegex = /(\d{2})\/(\d{2})\/(\d{4})/;
const usDateMatch = dateString.match(usDateRegex);
if (usDateMatch) {
const [_, month, day, year] = usDateMatch;
return new Date(Number(year), Number(month) - 1, Number(day));
}

// Regular expression for "YYYY" format
const yearRegex = /(\d{4})/;
const yearMatch = dateString.match(yearRegex);
if (yearMatch) {
const [_, year] = yearMatch;
return new Date(Number(year), 0, 1);
}

return null;
}

export const getColor = (
legendLabel: string,
colorMap: React.MutableRefObject<Map<string, string>>,
Expand Down Expand Up @@ -315,7 +345,7 @@ export const transformPlotlyJsonToScatterChartProps = (
return {
legend,
data: xValues.map((x: string | number, i: number) => ({
x: isString ? (isXDate ? new Date(x) : isXNumber ? parseFloat(x as string) : x) : x,
x: isString ? (isXDate ? parseDateFromString(x.toString()) : isXNumber ? parseFloat(x as string) : x) : x,
y: series.y[i],
})),
color: lineColor,
Expand Down Expand Up @@ -414,9 +444,15 @@ export const transformPlotlyJsonToHeatmapProps = (jsonObj: any): IHeatMapChartPr

// Convert normalized values to actual values
const domainValuesForColorScale: number[] = firstData.colorscale
? firstData.colorscale.map((arr: any) => arr[0] * (zMax - zMin) + zMin)
? isArrayOrTypedArray(firstData.colorscale)
? firstData.colorscale.map((arr: any) => arr[0] * (zMax - zMin) + zMin)
: []
: [];
const rangeValuesForColorScale: string[] = firstData.colorscale
? isArrayOrTypedArray(firstData.colorscale)
? firstData.colorscale.map((arr: any) => arr[1])
: []
: [];
const rangeValuesForColorScale: string[] = firstData.colorscale ? firstData.colorscale.map((arr: any) => arr[1]) : [];

return {
data: [heatmapData],
Expand Down

0 comments on commit 4de140a

Please sign in to comment.