-
Notifications
You must be signed in to change notification settings - Fork 7
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
[WIP] added closest area and line strategies for tooltip tracking #170
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<html> | ||
<head> | ||
<title>Yagr</title> | ||
<script src="../../dist/yagr.iife.js"></script> | ||
<script src="../helpers.js"></script> | ||
<link rel="stylesheet" href="../../dist/index.css" /> | ||
<style> | ||
.container { | ||
margin-bottom: 26px; | ||
height: 400px; | ||
} | ||
#root { | ||
display: grid; | ||
grid-gap: 12px; | ||
grid-template-columns: 1fr 1fr; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Tooltip tracking strategies</h1> | ||
<div id="root"> | ||
<div id="chart1" class="container"></div> | ||
<div id="chart2" class="container"></div> | ||
<div id="chart3" class="container"></div> | ||
<div id="chart4" class="container"></div> | ||
</div> | ||
|
||
<script> | ||
const negate = (x) => -x; | ||
|
||
new Yagr(chart1, { | ||
title: { | ||
text: 'sticky', | ||
}, | ||
timeline: [1, 2, 3, 4], | ||
series: [ | ||
{data: [1, 2, 3, 2], color: 'green'}, | ||
{data: [2, 1, 1, 3], color: 'red'}, | ||
{data: [4, 3, 0.3, 4], color: 'blue'}, | ||
{data: [3, 2, 5, 1], color: 'orange'}, | ||
], | ||
axes: { | ||
r: {side: 'right'}, | ||
y: {}, | ||
}, | ||
scales: { | ||
r: {}, | ||
y: { | ||
max: 0, | ||
transform: negate, | ||
}, | ||
}, | ||
tooltip: { | ||
tracking: 'sticky', | ||
}, | ||
}); | ||
|
||
new Yagr(chart2, { | ||
title: { | ||
text: 'line-closest', | ||
}, | ||
timeline: [1, 2, 3, 4], | ||
series: [ | ||
{data: [1, 2, 3, 2], color: 'green'}, | ||
{data: [2, 1, 1, 3], color: 'red'}, | ||
{data: [4, 3, 0.3, 4], color: 'blue'}, | ||
{data: [3, 2, 5, 1], color: 'orange'}, | ||
], | ||
axes: { | ||
r: {side: 'right'}, | ||
y: {}, | ||
}, | ||
scales: { | ||
r: {}, | ||
y: { | ||
max: 0, | ||
transform: negate, | ||
}, | ||
}, | ||
tooltip: { | ||
tracking: 'line-closest', | ||
}, | ||
}); | ||
|
||
new Yagr(chart3, { | ||
title: { | ||
text: 'area', | ||
}, | ||
chart: { | ||
series: {type: 'area'}, | ||
}, | ||
scales: { | ||
y: {stacking: true}, | ||
}, | ||
timeline: [1, 2, 3, 4], | ||
series: [ | ||
{data: [4, 3, 0.3, 4], color: 'blue', stackGroup: 0}, | ||
|
||
{data: [2, 1, 1, 3], color: 'red', transform: negate, name: 'transformed 2', stackGroup: 1}, | ||
{data: [1, 2, 3, 2], color: 'green', transform: negate, name: 'transformed 1', stackGroup: 1}, | ||
|
||
{data: [1, 2, 2, 1], color: 'orange', stackGroup: 0}, | ||
], | ||
tooltip: { | ||
tracking: 'area', | ||
sum: false, | ||
}, | ||
}); | ||
|
||
new Yagr(chart4, { | ||
title: { | ||
text: 'area-closest', | ||
}, | ||
chart: { | ||
series: {type: 'area'}, | ||
}, | ||
scales: { | ||
y: {stacking: true}, | ||
}, | ||
timeline: [1, 2, 3, 4], | ||
series: [ | ||
{data: [4, 3, 0.3, 4], color: 'blue', stackGroup: 0}, | ||
|
||
{data: [2, 1, 1, 3], color: 'red', transform: negate, name: 'transformed 2', stackGroup: 1}, | ||
{data: [1, 2, 3, 2], color: 'green', transform: negate, name: 'transformed 1', stackGroup: 1}, | ||
|
||
{data: [1, 2, 2, 1], color: 'orange', stackGroup: 0}, | ||
], | ||
tooltip: { | ||
tracking: 'area-closest', | ||
sum: false, | ||
}, | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import {AlignedData} from 'uplot'; | ||
|
||
export function getOptionValue<T>(option: T | {[key in string]: T}, scale: string): T { | ||
return (typeof option === 'object' ? (option as {[key in string]: T})[scale] : option) as T; | ||
} | ||
|
@@ -8,3 +10,65 @@ export function escapeHTML(html: string) { | |
elem.innerText = html; | ||
return elem.innerHTML; | ||
} | ||
|
||
type FindClosestAreaOptions = { | ||
/** cursor X */ | ||
x: number; | ||
/** cursor Y */ | ||
y: number; | ||
series: AlignedData; | ||
}; | ||
|
||
export const findClosestLines = ({x, y, series}: FindClosestAreaOptions) => { | ||
const [timeline, ...areas] = series as number[][]; | ||
|
||
let i1 = 0, | ||
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 believe you can find |
||
i2 = 0; | ||
|
||
// finding current timeline | ||
timeline.forEach((t, i) => { | ||
if (i1 || i2) { | ||
return; | ||
} | ||
if (t === x) { | ||
i1 = i2 = i; | ||
return; | ||
} | ||
if (t > x) { | ||
i1 = i - 1; | ||
i2 = i; | ||
} | ||
}); | ||
|
||
let minLower = Number.MAX_VALUE, | ||
minHigher = Number.MAX_VALUE; | ||
let iLower: number | null = null, | ||
iHigher: number | null = null; | ||
|
||
const x1 = timeline[i1]; | ||
const x2 = timeline[i2]; | ||
|
||
areas | ||
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. Let's try to reduce number of possible iterations. I understand that in most cases it doesn't do any issues, but I'd like to keep calculations as fast as possible. I mean let's do it in single loop |
||
.map((a) => [a[i1], a[i2]]) | ||
.forEach(([y1, y2], i) => { | ||
// finding intersection between line and vertical X = x | ||
const k = (y2 - y1) / (x2 - x1); | ||
const b = y1 - k * x1; | ||
const yInter = k * x + b; | ||
|
||
const distance = Math.abs(y - yInter); | ||
|
||
if (yInter < y && distance <= minLower) { | ||
minLower = distance; | ||
iLower = i; | ||
} else if (yInter >= y && distance <= minHigher) { | ||
minHigher = distance; | ||
iHigher = i; | ||
} | ||
}); | ||
|
||
return { | ||
lower: {index: iLower, distance: minLower}, | ||
higher: {index: iHigher, distance: minHigher}, | ||
}; | ||
}; |
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.
Can you add unit tests for this util, mostly to ensure valid calculation of closest area/line between two X coordinates?