Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 1x 1x 1x 1x 1x 1x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import * as React from "react"
import { computed } from "mobx"
import { observer } from "mobx-react"
import { scaleLinear } from "d3-scale"
import { bind } from "decko"
import { max, keyBy } from "../../clientUtils/Util"
import { ScaleLinear } from "d3-scale"
enum BarState {
highlighted = "highlighted",
current = "current",
normal = "normal",
faint = "faint",
}
export interface SparkBarsProps<T> {
data: T[]
x: (d: T) => number
y: (d: T) => number | undefined
xDomain: [number, number]
currentX?: number
highlightedX?: number
renderValue?: (d: T | undefined) => JSX.Element | undefined
onHover?: (d: T | undefined, index: number | undefined) => void
className?: string
color?: string
}
export interface SparkBarsDatum {
time: number
value: number
}
@observer
export class SparkBars<T> extends React.Component<SparkBarsProps<T>> {
static defaultProps = {
onHover: (): undefined => undefined,
className: "spark-bars",
}
@computed get maxY(): number | undefined {
return max(this.bars.map((d) => (d ? this.props.y(d) ?? 0 : 0)))
}
@computed get barHeightScale(): ScaleLinear<number, number> {
const maxY = this.maxY
return scaleLinear()
.domain([0, maxY !== undefined && maxY > 0 ? maxY : 1])
.range([0, 1])
}
@bind barHeight(d: T | undefined): string {
if (d !== undefined) {
const value = this.props.y(d)
if (value !== undefined) {
const ratio = this.barHeightScale(value)
return `${ratio * 100}%`
}
}
return "0%"
}
@bind barState(d: number): BarState {
if (d === this.props.highlightedX) return BarState.highlighted
if (d === this.props.currentX) return BarState.current
if (this.props.highlightedX !== undefined) return BarState.faint
return BarState.normal
}
@computed get bars(): (T | undefined)[] {
const indexed = keyBy(this.props.data, this.props.x)
const [start, end] = this.props.xDomain
const result = []
for (let i = start; i <= end; i++) {
result.push(indexed[i])
}
return result
}
render(): JSX.Element {
return (
<div
className={this.props.className}
onMouseLeave={() => this.props.onHover?.(undefined, undefined)}
>
{this.bars.map((d, i) => (
<div
key={i}
className="bar-wrapper"
onMouseEnter={() => this.props.onHover?.(d, i)}
>
{this.props.highlightedX === i &&
d !== undefined &&
this.props.renderValue && (
<div className="hanging-value highlighted highlighted-color">
{this.props.renderValue(d)}
</div>
)}
<div
className={`bar ${
d
? this.barState(this.props.x(d))
: BarState.normal
}`}
style={{
height: this.barHeight(d),
backgroundColor: this.props.color,
}}
></div>
</div>
))}
</div>
)
}
}
|