Skip to content

Commit

Permalink
enhancements and optimizations done
Browse files Browse the repository at this point in the history
  • Loading branch information
SURAJ-SHARMA27 committed Apr 30, 2024
1 parent 1c2a8f4 commit f92d988
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 88 deletions.
1 change: 0 additions & 1 deletion src/pages/groupComparison/ClinicalData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,6 @@ export default class ClinicalData extends React.Component<
? this.highlightedRow.qValue
: null
}
sortOption={'sortByAlphabet'}
/>
);
}
Expand Down
73 changes: 36 additions & 37 deletions src/pages/groupComparison/MultipleCategoryBarPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,47 +435,46 @@ export default class MultipleCategoryBarPlot extends React.Component<
}

@computed get labels() {
_.forEach(this.data, item => {
// Sorting counts within each item
item.counts.sort((a, b) =>
a.majorCategory.localeCompare(b.majorCategory)
);
});
const totalSumArray: TotalSumItem[] = [];
_.forEach(this.data, item => {
_.forEach(item.counts, countItem => {
const existingItem = _.find(
totalSumArray,
sumItem => sumItem.majorCategory === countItem.majorCategory
if (this.props.sortOption == 'sortByCount') {
_.forEach(this.data, item => {
item.counts.sort((a, b) =>
a.majorCategory.localeCompare(b.majorCategory)
);
if (existingItem) {
existingItem.sum += countItem.count;
existingItem.minorCategory.push({
name: item.minorCategory,
count: countItem.count,
percentage: countItem.percentage,
});
} else {
totalSumArray.push({
majorCategory: countItem.majorCategory,
sum: countItem.count,
minorCategory: [
{
name: item.minorCategory,
count: countItem.count,
percentage: countItem.percentage,
},
],
});
}
});
});

totalSumArray.sort((a, b) => b.sum - a.sum);
const totalSumArray: TotalSumItem[] = [];
_.forEach(this.data, item => {
_.forEach(item.counts, countItem => {
const existingItem = _.find(
totalSumArray,
sumItem =>
sumItem.majorCategory === countItem.majorCategory
);
if (existingItem) {
existingItem.sum += countItem.count;
existingItem.minorCategory.push({
name: item.minorCategory,
count: countItem.count,
percentage: countItem.percentage,
});
} else {
totalSumArray.push({
majorCategory: countItem.majorCategory,
sum: countItem.count,
minorCategory: [
{
name: item.minorCategory,
count: countItem.count,
percentage: countItem.percentage,
},
],
});
}
});
});

const sortedLabels = totalSumArray.map(item => item.majorCategory);
totalSumArray.sort((a, b) => b.sum - a.sum);

if (this.props.sortOption == 'sortByCount') {
const sortedLabels = totalSumArray.map(item => item.majorCategory);
return sortedLabels;
}

Expand Down
91 changes: 41 additions & 50 deletions src/shared/components/plots/MultipleCategoryBarPlotUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { IStringAxisData } from './PlotsTabUtils';
import _ from 'lodash';
import { IMultipleCategoryBarPlotData } from '../../../pages/groupComparison/MultipleCategoryBarPlot';
import {
IMultipleCategoryBarPlotData,
TotalSumItem,
} from '../../../pages/groupComparison/MultipleCategoryBarPlot';
import { marginLeft } from 'pages/patientView/trialMatch/style/trialMatch.module.scss';

export function makePlotData(
Expand Down Expand Up @@ -80,13 +83,7 @@ export function makePlotData(
);
return data;
}
interface TotalSumItem {
majorCategory: string;
sum: number;
minorCategory: { name: string; count: number; percentage: number }[];
}

export { TotalSumItem };
export function sortDataByCategory<D>(
data: D[],
getCategory: (d: D) => string,
Expand Down Expand Up @@ -168,55 +165,51 @@ export function makeBarSpecs(
});

totalSumArray.sort((a, b) => b.sum - a.sum);
const minorCategoryArrays: {
[key: string]: {
majorCategory: string;
count: number;
percentage: number;
}[];
} = {};
data.forEach(item => {
// Extract the minorCategory from the current item
const minorCategory = item.minorCategory;

// Check if the minorCategory already exists in minorCategoryArrays
if (!minorCategoryArrays[minorCategory]) {
// If it doesn't exist, create a new array for it
minorCategoryArrays[minorCategory] = [];
}

// Find corresponding items in totalSumArray and add them to the array
totalSumArray.forEach(totalItem => {
totalItem.minorCategory.forEach(minorItem => {
if (minorItem.name === minorCategory) {
minorCategoryArrays[minorCategory].push({
majorCategory: totalItem.majorCategory,
count: minorItem.count,
percentage: minorItem.percentage,
});
}
});
});
});
return data.map(({ minorCategory, counts }) => {
const fill = getColor(minorCategory);
const sortedCounts = sortDataByCategory(
counts,
d => d.majorCategory,
majorCategoryOrder
);

const finals = minorCategoryArrays[minorCategory];

let categoriezedCounts;
let categorizedCounts;
if (sortOption == 'sortByCount') {
categoriezedCounts = finals;
const minorCategoryArrays: {
[key: string]: {
majorCategory: string;
count: number;
percentage: number;
}[];
} = {};
data.forEach(item => {
// Extract the minorCategory from the current item
if (!minorCategoryArrays[item.minorCategory]) {
minorCategoryArrays[item.minorCategory] = [];
}

// Find corresponding items in totalSumArray and add them to the array
totalSumArray.forEach(totalItem => {
totalItem.minorCategory.forEach(minorItem => {
if (minorItem.name === item.minorCategory) {
minorCategoryArrays[item.minorCategory].push({
majorCategory: totalItem.majorCategory,
count: minorItem.count,
percentage: minorItem.percentage,
});
}
});
});
});

categorizedCounts = minorCategoryArrays[minorCategory];
} else {
categoriezedCounts = sortedCounts;
categorizedCounts = sortDataByCategory(
counts,
d => d.majorCategory,
majorCategoryOrder
);
}
const countByCategory = {

return {
fill,
data: categoriezedCounts.map((obj, index) => ({
data: categorizedCounts.map((obj, index) => ({
x: categoryCoord(index),
y: percentage ? obj.percentage : obj.count,
majorCategory: obj.majorCategory,
Expand All @@ -225,7 +218,5 @@ export function makeBarSpecs(
percentage: obj.percentage,
})),
};

return countByCategory;
});
}

0 comments on commit f92d988

Please sign in to comment.