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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 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 8x 8x 8x 8x 8x 8x 8x 8x 11x 11x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 25x 1x 1x 8x 8x 8x 8x 8x 8x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 8x 8x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 23x 23x 23x 23x 2x 2x 23x 23x 2x 2x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x | import * as React from "react"
import { sum, max } from "../../clientUtils/Util"
import { computed } from "mobx"
import { observer } from "mobx-react"
import { TextWrap } from "../text/TextWrap"
import { BASE_FONT_SIZE } from "../core/GrapherConstants"
import { Color } from "../../coreTable/CoreTableConstants"
export interface VerticalColorLegendManager {
maxLegendWidth?: number
fontSize?: number
legendItems: LegendItem[]
title?: string
onLegendMouseOver?: (color: string) => void
onLegendClick?: (color: string) => void
onLegendMouseLeave?: () => void
legendX?: number
legendY?: number
activeColors: Color[]
focusColors?: Color[]
}
export interface LegendItem {
label?: string
minText?: string
maxText?: string
color: Color
}
interface SizedLegendSeries {
textWrap: TextWrap
color: Color
width: number
height: number
}
@observer
export class VerticalColorLegend extends React.Component<{
manager: VerticalColorLegendManager
}> {
@computed get manager(): VerticalColorLegendManager {
return this.props.manager
}
@computed private get maxLegendWidth(): number {
return this.manager.maxLegendWidth ?? 100
}
@computed private get fontSize(): number {
return 0.7 * (this.manager.fontSize ?? BASE_FONT_SIZE)
}
@computed private get rectSize(): number {
return Math.round(this.fontSize / 1.4)
}
private rectPadding = 5
private lineHeight = 5
@computed private get title(): TextWrap | undefined {
if (!this.manager.title) return undefined
return new TextWrap({
maxWidth: this.maxLegendWidth,
fontSize: this.fontSize,
fontWeight: 700,
text: this.manager.title,
})
}
@computed private get titleHeight(): number {
if (!this.title) return 0
return this.title.height + 5
}
@computed private get series(): SizedLegendSeries[] {
const { manager, fontSize, rectSize, rectPadding } = this
return manager.legendItems
.map((series) => {
let label = series.label
// infer label for numeric bins
if (!label && series.minText && series.maxText) {
label = `${series.minText} – ${series.maxText}`
}
const textWrap = new TextWrap({
maxWidth: this.maxLegendWidth,
fontSize,
text: label ?? "",
})
return {
textWrap,
color: series.color,
width: rectSize + rectPadding + textWrap.width,
height: Math.max(textWrap.height, rectSize),
}
})
.filter((v) => !!v) as SizedLegendSeries[]
}
@computed get width(): number {
const widths = this.series.map((series) => series.width)
if (this.title) widths.push(this.title.width)
return max(widths) ?? 0
}
@computed get height(): number {
return (
this.titleHeight +
sum(this.series.map((series) => series.height)) +
this.lineHeight * this.series.length
)
}
render(): JSX.Element {
const {
title,
titleHeight,
series,
rectSize,
rectPadding,
lineHeight,
manager,
} = this
const {
focusColors,
activeColors,
onLegendMouseOver,
onLegendMouseLeave,
onLegendClick,
} = manager
const x = manager.legendX ?? 0
const y = manager.legendY ?? 0
let markOffset = titleHeight
return (
<>
{title && title.render(x, y, { fontWeight: 700 })}
<g
className="ScatterColorLegend clickable"
style={{ cursor: "pointer" }}
>
{series.map((series, index) => {
const isActive = activeColors.includes(series.color)
const isFocus =
focusColors?.includes(series.color) ?? false
const mouseOver = onLegendMouseOver
? (): void => onLegendMouseOver(series.color)
: undefined
const mouseLeave = onLegendMouseLeave || undefined
const click = onLegendClick
? (): void => onLegendClick(series.color)
: undefined
const result = (
<g
key={index}
className="legendMark"
onMouseOver={mouseOver}
onMouseLeave={mouseLeave}
onClick={click}
fill={!isActive ? "#ccc" : undefined}
>
<rect
x={x}
y={y + markOffset - lineHeight / 2}
width={series.width}
height={series.height + lineHeight}
fill="#fff"
opacity={0}
/>
<rect
x={x}
y={
y +
markOffset +
(series.height - rectSize) / 2
}
width={rectSize}
height={rectSize}
fill={isActive ? series.color : undefined}
/>
{series.textWrap.render(
x + rectSize + rectPadding,
y + markOffset,
isFocus
? { style: { fontWeight: "bold" } }
: undefined
)}
</g>
)
markOffset += series.height + lineHeight
return result
})}
</g>
</>
)
}
}
|