Skip to main content

Crosshair

Basic configuration

The Crosshair component is a special tooltip designed to work in an XYContainer. When a user is interacting with the XYContainer and a crosshair is provided, the Crosshair will appear as a vertical line and render circles on the corresponding y values in the dataset.

component.tsx
import { VisXYContainer, VisLine, VisAxis, VisAxis, VisCrosshair } from '@unovis/react'

function Component(props) {
const data: DataRecord[] = props.data
const x = (d: DataRecord) => d.x
const y = [
(d: DataRecord) => d.y,
(d: DataRecord) => d.y1,
(d: DataRecord) => d.y2
]

return (
<VisXYContainer data={data}>
<VisLine x={x} y={y}/>
<VisAxis type="x"/>
<VisAxis type="y"/>
<VisCrosshair/>
</VisXYContainer>
)
}
Loading...

X and Y accessors

Like other components in you can supply x and y accessors to the Crosshair component to control where it appears in your container. There's also a dedicated yStacked property for dealing with stacked values.

By default, Crosshair automatically takes the x, y and yStacked settings from XYContainer. But as soon as you manually specify any of them, the component will expect the rest to be provided as well. For example, if you supply the x accessor function to your crosshair you'll also have to supply the y or yStacked settings depending on your chart configuration.

See the following example which moves the position of the crosshair line to the right of each bar.

const x: ((d: DataRecord) => number) = d.x + 0.5
const yStacked: ((d: DataRecord) => number)[] = [d => d.y, d => d.y1, d => d.y2]
<VisCrosshair x={x} yStacked={yStacked}/>
Loading...

Hiding the crosshair

By default, the Crosshair component will render if the cursor is within a certain distance in pixels from a valid x value. You can disable this feature using the hideWhenFarFromPointer attribute.

<VisCrosshair hideWhenFarFromPointer={true}/>
Loading...

hideWhenFarFromPointerDistance

Use the hideWhenFarFromPointerDistance attribute with a length (in pixels) that represents the minimum horizontal distance the cursor must be from a datapoint before hiding.

<VisCrosshair hideWhenFarFromPointerDistance={50}/>
Loading...

Custom Color

Provide a string or color accessor function to the color attribute to customize the crosshair's point color:

component.tsx
function Component(props) {
const data: DataRecord[] = props.data
const color = (d: DataRecord, i: number) => ['red','green','blue'][i]

return (
<VisCrosshair color={color}/>
)
}
Loading...

Displaying Custom Content

You can render text content for your Crosshair component by providing it with a template property and a Tooltip component within the same container.

component.tsx
import { VisXYContainer, VisScatter, VisTooltip, VisAxis, VisAxis, VisCrosshair } from '@unovis/react'

function Component(props) {
const data: DataRecord[] = props.data
const x = (d: DataRecord) => d.x
const y = (d: DataRecord) => d.y
const template = (d: DataRecord) => [d.x,d.y].join(', ')

return (
<VisXYContainer data={data}>
<VisScatter x={x} y={y}/>
<VisTooltip/>
<VisAxis type="x"/>
<VisAxis type="y"/>
<VisCrosshair template={template}/>
</VisXYContainer>
)
}
Loading...

Additional Styling: CSS Variables

The Crosshair component supports additional styling via CSS variables that you can define for your visualization container. For example:

styles.css
.visualization-container-div {
--vis-crosshair-line-stroke-color: #f88080;
--vis-crosshair-circle-stroke-color: #000000;
}
Loading...
All supported CSS variables and their default values
--vis-crosshair-line-stroke-color: #888;
--vis-crosshair-circle-stroke-color: #fff;
NameTypeDescription
* required property