Skip to content

Commit 209e92e

Browse files
authored
Merge pull request #1846 from concord-consortium/CODAP-20-Hover-date-precision-v3
Data tips respect date and numeric precision
2 parents 613f3f1 + 46ab6c1 commit 209e92e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

v3/src/components/data-display/models/data-display-content-model.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import {comparer, reaction} from "mobx"
66
import {addDisposer, Instance, types} from "mobx-state-tree"
77
import { format } from "d3"
8+
import { formatDate } from "../../../utilities/date-utils"
9+
import { IValueType } from "../../../models/data/attribute-types"
10+
import { IAttribute } from "../../../models/data/attribute"
811
import {IDataSet} from "../../../models/data/data-set"
912
import {ISharedCaseMetadata} from "../../../models/shared/shared-case-metadata"
1013
import {ISharedDataSet} from "../../../models/shared/shared-data-set"
@@ -68,14 +71,39 @@ export const DataDisplayContentModel = TileContentModel
6871
return getTileContentInfo(self.type)?.getFormulaAdapters?.(self) ?? []
6972
},
7073
caseTipText(attributeIDs: string[], caseID: string, dataset?: IDataSet) {
74+
75+
const getValueToDisplay = (numValue: number | undefined, value: IValueType, attribute?: IAttribute) => {
76+
if (!attribute) return ''
77+
switch (attribute.type) {
78+
case 'numeric': {
79+
const numPrecision = attribute.numPrecision
80+
const showUnits = attribute.units && attribute.units !== ""
81+
const unitsString = showUnits ? ` ${attribute.units}` : ""
82+
if (numValue && isFinite(numValue) && numPrecision) {
83+
const formatStr = `.${attribute.numPrecision}~f`
84+
const formatter = format(formatStr)
85+
return `${formatter ? `${formatter(numValue)}` : `${numValue}`}${unitsString}`
86+
}
87+
return `${value}${unitsString}`
88+
}
89+
case 'date': {
90+
const datePrecision = attribute.datePrecision
91+
return value && datePrecision ? formatDate(String(value), datePrecision) : value
92+
}
93+
default:
94+
return value
95+
}
96+
}
97+
7198
const float = format('.3~f')
7299
const attrArray = (attributeIDs?.map(attrID => {
73100
const attribute = dataset?.attrFromID(attrID),
74101
name = attribute?.name,
75102
numValue = dataset?.getNumeric(caseID, attrID),
76103
value = numValue != null && isFinite(numValue) ? float(numValue)
77-
: dataset?.getValue(caseID, attrID)
78-
return value ? `${name}: ${value}` : ''
104+
: dataset?.getValue(caseID, attrID),
105+
displayedValue = getValueToDisplay(numValue, value, attribute)
106+
return value ? `${name}: ${displayedValue}` : ''
79107
}))
80108
// Caption attribute can also be one of the plotted attributes, so we remove dups and join into html string
81109
return Array.from(new Set(attrArray)).filter(anEntry => anEntry !== '').join('<br>')

v3/src/utilities/js-utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ export function hashStringSet(strings: string[]) {
170170
export function hashStringSets(stringSets: Array<string[]>) {
171171
return stringSets
172172
.map(hashStringSet)
173+
// XOR all individual hashes with an index-based multiplier and a large initial value
173174
// eslint-disable-next-line no-bitwise
174-
.reduce((acc, hash, index) => acc ^ (hash * (index + 1)), 0x9e3779b9) // XOR all individual hashes with an index-based multiplier and a large initial value
175+
.reduce((acc, hash, index) => acc ^ (hash * (index + 1)), 0x9e3779b9)
175176
}
176177

177178
/*

0 commit comments

Comments
 (0)