Skip to content

Commit 12a9294

Browse files
committed
Implement onHighlightChanged listener
1 parent ed4228e commit 12a9294

4 files changed

Lines changed: 75 additions & 17 deletions

File tree

example/src/App.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Chart, {
1010
import { buildSlices } from './helpers/buildSlices'
1111
import { generateTimeSeriesData } from './helpers/generateTimeSeriesData'
1212
import { temperatureData, visits } from './mockData'
13+
import type { HighlightPayload } from '../../src/types'
1314

1415
type TimeDomainType = 'hour' | 'day' | 'week' | 'month'
1516

@@ -142,6 +143,12 @@ export default function App() {
142143
[Measurement.Temperature]
143144
)
144145

146+
const [highlightValuePosition, setHighlightValuePosition] = useState<
147+
'top' | 'tooltip' | 'none'
148+
>('tooltip')
149+
150+
const [currentHighlight, setCurrentHighlight] = useState<HighlightPayload>()
151+
145152
const errorSegments = useMemo<ErrorSegment[]>(() => {
146153
const now = Date.now()
147154
return [
@@ -236,6 +243,19 @@ export default function App() {
236243
</TouchableOpacity>
237244
))}
238245
</View>
246+
<View style={styles.highlightContainer}>
247+
<Text>Highlight listener:</Text>
248+
<Text>
249+
Exact timestamp:
250+
{currentHighlight?.timestamp &&
251+
new Date(currentHighlight.timestamp).toTimeString()}
252+
</Text>
253+
{currentHighlight?.values.map((value, index) => (
254+
<Text key={index} style={{ color: value?.color || '#000' }}>
255+
{`${value?.measurementName}: ${value?.errorMessage ?? value?.value}`}
256+
</Text>
257+
))}
258+
</View>
239259
{/* Measurement toggles */}
240260
{measurementKeys.map((measurement) => (
241261
<View key={measurement} style={styles.switchContainer}>

src/Chart.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type HtmlProps = {
2828
timeDomain: TimeDomain
2929
marginHorizontal: number
3030
highlightPosition: number
31+
hasHighlightListener: boolean
3132
xDividerConfig: XDividerConfig
3233
highlightValuePosition: Required<ChartProps['highlightValuePosition']>
3334
calendar?: CalendarStrings
@@ -51,6 +52,7 @@ export default function Chart({
5152
xDividerConfig = { type: 'tick' },
5253
onZoomEnded,
5354
onZoomStarted,
55+
onHighlightChanged,
5456
}: ChartProps) {
5557
const ref = useRef<WebView>(null)
5658
const propInjection = useRef<string>('')
@@ -78,10 +80,11 @@ export default function Chart({
7880
errorSegments,
7981
marginHorizontal,
8082
highlightPosition,
83+
colors: chartColors,
8184
highlightValuePosition,
8285
calendar: calendarStrings,
8386
zoomEnabled: !!zoomEnabled,
84-
colors: chartColors,
87+
hasHighlightListener: !!onHighlightChanged,
8588
}
8689
currentTimeDomain.current = timeDomain
8790
const injection = `
@@ -109,11 +112,12 @@ export default function Chart({
109112
chartColors,
110113
zoomEnabled,
111114
noDataString,
112-
xDividerConfig,
113115
errorSegments,
116+
xDividerConfig,
114117
calendarStrings,
115118
marginHorizontal,
116119
highlightPosition,
120+
onHighlightChanged,
117121
highlightValuePosition,
118122
])
119123

@@ -132,11 +136,14 @@ export default function Chart({
132136
message.payload === 'end' && onZoomEnded?.()
133137
message.payload === 'start' && onZoomStarted?.()
134138
break
139+
case 'highlightChanged':
140+
onHighlightChanged?.(message.payload)
141+
break
135142
default:
136143
console.log(message)
137144
}
138145
},
139-
[onZoomEnded, onZoomStarted]
146+
[onZoomEnded, onZoomStarted, onHighlightChanged]
140147
)
141148

142149
const source = useMemo(() => {

src/drawFunction.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -708,14 +708,6 @@ window.draw = (props) => {
708708
break
709709
}
710710
711-
console.log(
712-
props.highlightValuePosition,
713-
labelsHolder,
714-
valuesHolder,
715-
clockSpan,
716-
dateSpan
717-
)
718-
719711
selectOrAppend(svg, 'line', 'highlight')
720712
.style('stroke', colors.highlightLine)
721713
.style('stroke-width', 1)
@@ -974,6 +966,12 @@ window.draw = (props) => {
974966
)
975967
976968
var highlightTime = null
969+
970+
/**
971+
* Only used to send highlight values to listener. Only populated if hasHighlightListener
972+
*/
973+
const highlightValues = []
974+
977975
props.datasets.forEach((dataset, index) => {
978976
const { unit, decimals } = dataset
979977
const { definedData, y } = operators[index]
@@ -984,6 +982,9 @@ window.draw = (props) => {
984982
.style('color', getDatasetColor(dataset))
985983
.html(props.noDataString)
986984
985+
if (props.hasHighlightListener) {
986+
highlightValues.push(null)
987+
}
987988
return
988989
}
989990
@@ -1019,20 +1020,39 @@ window.draw = (props) => {
10191020
.attr('stroke', color)
10201021
.attr('opacity', tooFar ? 0 : 1)
10211022
1023+
const errorMessage = isInErrorSegment
1024+
? highlightedErrorSegment.message
1025+
: tooFar
1026+
? props.noDataString
1027+
: null
1028+
1029+
if (props.hasHighlightListener) {
1030+
highlightValues.push({
1031+
value: errorMessage ? null : highlight.value,
1032+
color,
1033+
errorMessage,
1034+
timestamp: highlight.date.valueOf(),
1035+
measurementName: dataset.measurementName,
1036+
})
1037+
}
1038+
10221039
if (props.highlightValuePosition === 'none') return
10231040
10241041
valuesHolder
10251042
.select('span#highlightvalue' + index)
10261043
.style('color', color)
10271044
.html(
1028-
isInErrorSegment
1029-
? highlightedErrorSegment.message
1030-
: tooFar
1031-
? props.noDataString
1032-
: d3.format('.' + decimals + 'f')(highlight.value) + ' ' + unit
1045+
errorMessage ?? d3.format('.' + decimals + 'f')(highlight.value) + ' ' + unit
10331046
)
10341047
})
10351048
1049+
if (props.hasHighlightListener) {
1050+
postMessage('highlightChanged', {
1051+
values: highlightValues,
1052+
timestamp: highlightExactDate.valueOf(),
1053+
})
1054+
}
1055+
10361056
if (props.highlightValuePosition === 'none') return
10371057
10381058
const date = highlightTime ?? highlightExactDate

src/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ type XDividerSegment = {
109109

110110
export type XDividerConfig = XDividerTick | XDividerSegment
111111

112+
export type HighlightPayload = {
113+
timestamp: number
114+
values: ({
115+
value: Point['value']
116+
timestamp: Point['timestamp']
117+
color: string
118+
errorMessage: string | null
119+
measurementName: Dataset['measurementName']
120+
} | null)[]
121+
}
122+
112123
export type ChartProps = {
113124
width: number
114125
height: number
@@ -142,5 +153,5 @@ export type ChartProps = {
142153
highlightValuePosition?: 'top' | 'tooltip' | 'none'
143154
onZoomEnded?: () => void
144155
onZoomStarted?: () => void
145-
onHighlightChanged?: ({ timestamp, value }: Point) => void
156+
onHighlightChanged?: (payload: HighlightPayload) => void
146157
}

0 commit comments

Comments
 (0)