-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
on plots tab add sorting for barcharts #4893
Changes from all commits
6cfd7c7
a05006d
51e085b
1c2a8f4
f92d988
ece5a45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,16 @@ export interface IMultipleCategoryBarPlotProps { | |
svgRef?: (svgContainer: SVGElement | null) => void; | ||
pValue: number | null; | ||
qValue: number | null; | ||
sortOption?: string; | ||
} | ||
export interface TotalSumItem { | ||
majorCategory: string; | ||
sum: number; | ||
minorCategory: { | ||
name: string; | ||
count: number; | ||
percentage: number; | ||
}[]; | ||
} | ||
|
||
export interface IMultipleCategoryBarPlotData { | ||
|
@@ -274,6 +284,7 @@ export default class MultipleCategoryBarPlot extends React.Component< | |
} else if (this.props.plotData) { | ||
data = this.props.plotData; | ||
} | ||
|
||
return data; | ||
} | ||
|
||
|
@@ -424,12 +435,56 @@ export default class MultipleCategoryBarPlot extends React.Component< | |
} | ||
|
||
@computed get labels() { | ||
if (this.props.sortOption == 'sortByCount') { | ||
_.forEach(this.data, item => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm curious why you need to sort alphabetical first. you should put comments explaining each step. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was sorting the elements alphabetically first so that all the elements in each object would come in the same order, allowing me to calculate the count corresponding to each element accurately. For example:
Let's say, in the array containing 42 elements in the Male minorCategory, the first index is X, and in the Female minorCategory, the first index is Y in counts. If I calculate sum_of_total_counts = data[0].counts[0].count + data[1].counts[1].count, the sum_of_total_counts calculated is incorrect because it adds the counts for X and Y. However, if I sort it alphabetically, the counts array for each minorCategory will be in the same order, and I can find the total count easily. |
||
item.counts.sort((a, b) => | ||
a.majorCategory.localeCompare(b.majorCategory) | ||
); | ||
}); | ||
const totalSumArray: TotalSumItem[] = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code is quite confusing. it would be clearer if you used _.sortBy(collection, ()=>); where the return of the callback is a _.sum of the item counts within. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic is that I will traverse this.data and will make one totalSumArray array which has a schema like:
Now, I will traverse this.data and go to each Let's take an example. For one configuration, this.data contains:
I will traverse each object, go to the counts array, and create one object for like Melanoma in |
||
_.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, | ||
}, | ||
], | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
totalSumArray.sort((a, b) => b.sum - a.sum); | ||
|
||
const sortedLabels = totalSumArray.map(item => item.majorCategory); | ||
return sortedLabels; | ||
} | ||
|
||
if (this.data.length > 0) { | ||
return sortDataByCategory( | ||
const CategorizedData = sortDataByCategory( | ||
this.data[0].counts.map(c => c.majorCategory), | ||
x => x, | ||
this.majorCategoryOrder | ||
); | ||
return CategorizedData; | ||
} else { | ||
return []; | ||
} | ||
|
@@ -739,7 +794,8 @@ export default class MultipleCategoryBarPlot extends React.Component< | |
this.categoryCoord, | ||
!!this.props.horizontalBars, | ||
!!this.props.stacked, | ||
!!this.props.percentage | ||
!!this.props.percentage, | ||
this.props.sortOption || 'sortByAlphabet' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should probably apply this default inside the makeBarSpec |
||
); | ||
return barSpecs.map(spec => ( | ||
<VictoryBar | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SURAJ-SHARMA27 this is going to actually mutate the sorting of counts in
this.data
. i.e. sort it in place. instead, we should use functional/immutable approach to return a sorted list of labels without mutating underlying data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed the mutable approach and created a separate totalSumArray, which does not make any changes to this.data