feat(chart): add series styling and visibility controls
This commit is contained in:
@@ -6,12 +6,16 @@ export interface ResolvedWaveformRenderingOptions {
|
||||
downsample: boolean
|
||||
downsampleThreshold: number
|
||||
maxPointsPerPixel: number
|
||||
pointMinSpacing: number
|
||||
errorBarMinSpacing: number
|
||||
}
|
||||
|
||||
export const DEFAULT_WAVEFORM_RENDERING_OPTIONS: ResolvedWaveformRenderingOptions = {
|
||||
downsample: true,
|
||||
downsampleThreshold: 2_000,
|
||||
maxPointsPerPixel: 4,
|
||||
pointMinSpacing: 10,
|
||||
errorBarMinSpacing: 12,
|
||||
}
|
||||
|
||||
const pointBisector = bisector((point: WaveformPoint) => point.x)
|
||||
@@ -21,6 +25,8 @@ export function resolveWaveformRenderingOptions(
|
||||
): ResolvedWaveformRenderingOptions {
|
||||
const threshold = Number(options?.downsampleThreshold)
|
||||
const pointsPerPixel = Number(options?.maxPointsPerPixel)
|
||||
const pointMinSpacing = Number(options?.pointMinSpacing)
|
||||
const errorBarMinSpacing = Number(options?.errorBarMinSpacing)
|
||||
return {
|
||||
downsample: options?.downsample ?? DEFAULT_WAVEFORM_RENDERING_OPTIONS.downsample,
|
||||
downsampleThreshold:
|
||||
@@ -31,6 +37,14 @@ export function resolveWaveformRenderingOptions(
|
||||
Number.isFinite(pointsPerPixel) && pointsPerPixel > 0
|
||||
? pointsPerPixel
|
||||
: DEFAULT_WAVEFORM_RENDERING_OPTIONS.maxPointsPerPixel,
|
||||
pointMinSpacing:
|
||||
Number.isFinite(pointMinSpacing) && pointMinSpacing >= 0
|
||||
? pointMinSpacing
|
||||
: DEFAULT_WAVEFORM_RENDERING_OPTIONS.pointMinSpacing,
|
||||
errorBarMinSpacing:
|
||||
Number.isFinite(errorBarMinSpacing) && errorBarMinSpacing >= 0
|
||||
? errorBarMinSpacing
|
||||
: DEFAULT_WAVEFORM_RENDERING_OPTIONS.errorBarMinSpacing,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,3 +120,96 @@ export function selectRenderablePoints(
|
||||
pushUniquePoint(result, points[end - 1])
|
||||
return result
|
||||
}
|
||||
|
||||
/** Selects real source points for discrete decorations without using line-extrema sampling. */
|
||||
export function selectDecorationPoints(
|
||||
points: WaveformPoint[],
|
||||
domain: [number, number],
|
||||
width: number,
|
||||
minSpacing: number,
|
||||
downsample: boolean,
|
||||
predicate: (point: WaveformPoint) => boolean = () => true,
|
||||
priorityPredicate?: (point: WaveformPoint) => boolean,
|
||||
): WaveformPoint[] {
|
||||
if (!points.length || width <= 0) return []
|
||||
|
||||
const domainStart = Math.min(domain[0], domain[1])
|
||||
const domainEnd = Math.max(domain[0], domain[1])
|
||||
const visibleStart = pointBisector.left(points, domainStart)
|
||||
const visibleEnd = pointBisector.right(points, domainEnd)
|
||||
if (!downsample || minSpacing === 0) {
|
||||
return points.slice(visibleStart, visibleEnd).filter(predicate)
|
||||
}
|
||||
|
||||
const span = domainEnd - domainStart
|
||||
if (span <= 0) {
|
||||
const point = points.slice(visibleStart, visibleEnd).find(predicate)
|
||||
return point ? [point] : []
|
||||
}
|
||||
|
||||
const toPixel = (point: WaveformPoint) => ((point.x - domainStart) / span) * width
|
||||
const sparsePoints: WaveformPoint[] = []
|
||||
let alreadySparse = true
|
||||
let first: WaveformPoint | undefined
|
||||
let last: WaveformPoint | undefined
|
||||
let previousPixel = Number.NEGATIVE_INFINITY
|
||||
let candidateCount = 0
|
||||
|
||||
for (let index = visibleStart; index < visibleEnd; index += 1) {
|
||||
const point = points[index]
|
||||
if (!predicate(point)) continue
|
||||
first ??= point
|
||||
last = point
|
||||
candidateCount += 1
|
||||
if (!alreadySparse) continue
|
||||
const pixel = toPixel(point)
|
||||
if (pixel - previousPixel < minSpacing) {
|
||||
alreadySparse = false
|
||||
sparsePoints.length = 0
|
||||
continue
|
||||
}
|
||||
sparsePoints.push(point)
|
||||
previousPixel = pixel
|
||||
}
|
||||
if (candidateCount <= 2) {
|
||||
if (!first) return []
|
||||
return last && last !== first ? [first, last] : [first]
|
||||
}
|
||||
if (alreadySparse) return sparsePoints
|
||||
|
||||
const bucketCount = Math.max(1, Math.ceil(width / minSpacing))
|
||||
const bucketWidth = width / bucketCount
|
||||
const bucketPoints: Array<WaveformPoint | undefined> = Array.from({ length: bucketCount })
|
||||
const bucketDistances = Array.from({ length: bucketCount }, () => Number.POSITIVE_INFINITY)
|
||||
const priorityBucketPoints: Array<WaveformPoint | undefined> = Array.from({
|
||||
length: bucketCount,
|
||||
})
|
||||
const priorityBucketDistances = Array.from(
|
||||
{ length: bucketCount },
|
||||
() => Number.POSITIVE_INFINITY,
|
||||
)
|
||||
|
||||
for (let index = visibleStart; index < visibleEnd; index += 1) {
|
||||
const point = points[index]
|
||||
if (!predicate(point)) continue
|
||||
const pixel = Math.max(0, Math.min(width, toPixel(point)))
|
||||
const bucket = Math.min(bucketCount - 1, Math.floor(pixel / bucketWidth))
|
||||
const center = (bucket + 0.5) * bucketWidth
|
||||
const distance = Math.abs(pixel - center)
|
||||
if (distance < bucketDistances[bucket]) {
|
||||
bucketPoints[bucket] = point
|
||||
bucketDistances[bucket] = distance
|
||||
}
|
||||
if (priorityPredicate?.(point) && distance < priorityBucketDistances[bucket]) {
|
||||
priorityBucketPoints[bucket] = point
|
||||
priorityBucketDistances[bucket] = distance
|
||||
}
|
||||
}
|
||||
|
||||
const selected = bucketPoints
|
||||
.map((point, index) => priorityBucketPoints[index] ?? point)
|
||||
.filter((point): point is WaveformPoint => point !== undefined)
|
||||
if (first && selected[0] !== first) selected.unshift(first)
|
||||
if (last && selected.at(-1) !== last) selected.push(last)
|
||||
return selected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user