Skip to content

Commit

Permalink
* This change fixes a cypress test that failed. The problem was that …
Browse files Browse the repository at this point in the history
…in GraphContentModel I made the `binDetails` method an action because I thought it was a convenient place to not only compute a new binWidth, but call `setBinWidth`. But this was a bad idea since it ended up appending a new, not undoable item to the history, covering up the action that was still unwinding. So we back out of that change and reinstate `binDetails` as a view, not an action.
  • Loading branch information
bfinzer committed Dec 14, 2024
1 parent 1ae2f85 commit 8f83e51
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions v3/src/components/graph/models/graph-content-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,34 @@ export const GraphContentModel = DataDisplayContentModel
yScale
}
},
binDetails(options?: { initialize?: boolean }) {
const { initialize = false } = options ?? {}
const { dataset, primaryAttributeID } = self.dataConfiguration
const caseDataArray = self.dataConfiguration.getCaseDataArray(0)
const minValue = caseDataArray.reduce((min, aCaseData) => {
return Math.min(min, dataDisplayGetNumericValue(dataset, aCaseData.caseID, primaryAttributeID) ?? min)
}, Infinity)
const maxValue = caseDataArray.reduce((max, aCaseData) => {
return Math.max(max, dataDisplayGetNumericValue(dataset, aCaseData.caseID, primaryAttributeID) ?? max)
}, -Infinity)
const binWidth = (initialize || !self.binWidth)
? self.binWidthFromData(minValue, maxValue) : self.binWidth
if (minValue === Infinity || maxValue === -Infinity || binWidth === undefined) {
return { binAlignment: 0, binWidth: undefined, minBinEdge: 0, maxBinEdge: 0, minValue: 0, maxValue: 0,
totalNumberOfBins: 0 }
}

const binAlignment = initialize || !self.binAlignment
? Math.floor(minValue / binWidth) * binWidth
: self.binAlignment
const minBinEdge = binAlignment - Math.ceil((binAlignment - minValue) / binWidth) * binWidth
// Calculate the total number of bins needed to cover the range from the minimum data value
// to the maximum data value, adding a small constant to ensure the max value is contained.
const totalNumberOfBins = Math.ceil((maxValue - minBinEdge) / binWidth + 0.000001)
const maxBinEdge = minBinEdge + (totalNumberOfBins * binWidth)

return { binAlignment, binWidth, minBinEdge, maxBinEdge, minValue, maxValue, totalNumberOfBins }
},
}))
.actions(self => ({
afterCreate() {
Expand Down Expand Up @@ -264,37 +292,6 @@ export const GraphContentModel = DataDisplayContentModel
.actions(self => ({
updateAfterSharedModelChanges(sharedModel: ISharedModel | undefined, type: SharedModelChangeType) {
},
binDetails(options?: { initialize?: boolean }) {
const { initialize = false } = options ?? {}
const { dataset, primaryAttributeID } = self.dataConfiguration
const caseDataArray = self.dataConfiguration.getCaseDataArray(0)
const minValue = caseDataArray.reduce((min, aCaseData) => {
return Math.min(min, dataDisplayGetNumericValue(dataset, aCaseData.caseID, primaryAttributeID) ?? min)
}, Infinity)
const maxValue = caseDataArray.reduce((max, aCaseData) => {
return Math.max(max, dataDisplayGetNumericValue(dataset, aCaseData.caseID, primaryAttributeID) ?? max)
}, -Infinity)
if (initialize || !self.binWidth) {
self.setBinWidth(self.binWidthFromData(minValue, maxValue))
}
const binWidth = self.binWidth

if (minValue === Infinity || maxValue === -Infinity || binWidth === undefined) {
return { binAlignment: 0, binWidth: undefined, minBinEdge: 0, maxBinEdge: 0, minValue: 0, maxValue: 0,
totalNumberOfBins: 0 }
}

const binAlignment = initialize || !self.binAlignment
? Math.floor(minValue / binWidth) * binWidth
: self.binAlignment
const minBinEdge = binAlignment - Math.ceil((binAlignment - minValue) / binWidth) * binWidth
// Calculate the total number of bins needed to cover the range from the minimum data value
// to the maximum data value, adding a small constant to ensure the max value is contained.
const totalNumberOfBins = Math.ceil((maxValue - minBinEdge) / binWidth + 0.000001)
const maxBinEdge = minBinEdge + (totalNumberOfBins * binWidth)

return { binAlignment, binWidth, minBinEdge, maxBinEdge, minValue, maxValue, totalNumberOfBins }
},
setBinAlignment(alignment: number) {
self._binAlignment = isFiniteNumber(alignment) ? alignment : undefined
self.dynamicBinAlignment = undefined
Expand All @@ -305,7 +302,7 @@ export const GraphContentModel = DataDisplayContentModel
binnedAxisTicks(formatter?: (value: number) => string): { tickValues: number[], tickLabels: string[] } {
const tickValues: number[] = []
const tickLabels: string[] = []
const { binWidth, totalNumberOfBins, minBinEdge } = this.binDetails()
const { binWidth, totalNumberOfBins, minBinEdge } = self.binDetails()
if (binWidth !== undefined) {
let currentStart = minBinEdge
let binCount = 0

Check warning on line 308 in v3/src/components/graph/models/graph-content-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/models/graph-content-model.ts#L307-L308

Added lines #L307 - L308 were not covered by tests
Expand Down

0 comments on commit 8f83e51

Please sign in to comment.