import { colorToRgba , overrideOpacity } from '../../../common/color_library_wrappers' ; import { Line , Stroke } from '../../../geoms/types' ; import { mergePartial , RecursivePartial } from '../../../utils/common' ; import { Size } from '../../../utils/dimensions' ; import { AxisId } from '../../../utils/ids' ; import { Point } from '../../../utils/point' ; import { AxisStyle , Theme } from '../../../utils/themes/theme' ; import { MIN_STROKE_WIDTH } from '../renderer/canvas/primitives/line' ; import { SmallMultipleScales } from '../state/selectors/compute_small_multiple_scales' ; import { isVerticalAxis } from './axis_type_utils' ; import { AxisGeometry , AxisTick } from './axis_utils' ; import { getPanelSize } from './panel' ; import { getPerPanelMap } from './panel_utils' ; import { AxisSpec } from './specs' ; internal export const HIERARCHICAL_GRID_WIDTH = 1 ; internal export const OUTSIDE_RANGE_TOLERANCE = 0.01 ; internal export const HIDE_MINOR_TIME_GRID = false ; internal export interface GridLineGroup { lines : Array < Line > ; stroke : Stroke ; axisId : AxisId ; } internal export type LinesGrid = { panelAnchor : Point ; lineGroups : Array < GridLineGroup > ; } ; internal export function getGridLines ( axesSpecs , axesGeoms , { axes : themeAxisStyle } , scales ) { const panelSize = getPanelSize ( scales ) ; return getPerPanelMap ( scales , ( ) => { const lines = axesGeoms . reduce < Array < GridLineGroup > > ( ( linesAcc , { axis , visibleTicks } ) => { const axisSpec = axesSpecs . find ( ( { id } ) => id === axis . id ) ; if ( ! axisSpec ) { return linesAcc ; } const linesForSpec = getGridLinesForAxis ( axisSpec , visibleTicks , themeAxisStyle , panelSize ) ; return linesForSpec . length === 0 ? linesAcc : [ ... linesAcc , ... linesForSpec ] ; } , [ ] ) ; return { lineGroups : lines } ; } ) ; } function getGridLinesForAxis ( axisSpec , visibleTicks , themeAxisStyle , panelSize ) { const isVertical = isVerticalAxis ( axisSpec . position ) ; const axisStyle = mergePartial ( themeAxisStyle , axisSpec . style as RecursivePartial < AxisStyle > ) ; const gridLineThemeStyle = isVertical ? axisStyle . gridLine . vertical : axisStyle . gridLine . horizontal ; const gridLineStyles = axisSpec . gridLine ? mergePartial ( gridLineThemeStyle , axisSpec . gridLine ) : gridLineThemeStyle ; const showGridLines = axisSpec . showGridLines ?? gridLineStyles . visible ; if ( ! showGridLines ) { return [ ] ; } if ( ! gridLineStyles . stroke || ! gridLineStyles . strokeWidth || gridLineStyles . strokeWidth < MIN_STROKE_WIDTH ) { return [ ] ; } const visibleTicksPerLayer = visibleTicks . reduce ( ( acc , tick ) => { if ( Math . abs ( tick . position - tick . domainClampedPosition ) > OUTSIDE_RANGE_TOLERANCE ) return acc ; if ( typeof tick . layer === 'number' && ! tick . showGrid ) return acc ; if ( HIDE_MINOR_TIME_GRID && typeof tick . layer === 'number' && tick . detailedLayer === 0 ) return acc ; const ticks = acc . get ( tick . detailedLayer ) ; if ( ticks ) { ticks . push ( tick ) ; } else { acc . set ( tick . detailedLayer , [ tick ] ) ; } return acc ; } , new Map ( ) ) ; return [ ... visibleTicksPerLayer ] . sort ( ( [ k1 ] , [ k2 ] ) => ( k1 ?? 0 ) - ( k2 ?? 0 ) ) . map ( ( [ detailedLayer , visibleTicksOfLayer ] ) => { const lines = visibleTicksOfLayer . map < Line > ( ( tick ) => isVertical ? getGridLineForVerticalAxisAt ( tick . position , panelSize ) : getGridLineForHorizontalAxisAt ( tick . position , panelSize ) ) ; const strokeColor = overrideOpacity ( colorToRgba ( gridLineStyles . stroke ) , ( strokeColorOpacity ) => gridLineStyles . opacity !== undefined ? strokeColorOpacity * gridLineStyles . opacity : strokeColorOpacity ) ; const layered = typeof visibleTicksOfLayer [ 0 ] . layer === 'number' ; const multilayerLuma = themeAxisStyle . gridLine . lumaSteps [ detailedLayer ] ; const stroke = { color : layered ? [ multilayerLuma , multilayerLuma , multilayerLuma , 1 ] : strokeColor , width : layered ? HIERARCHICAL_GRID_WIDTH : gridLineStyles . strokeWidth , dash : gridLineStyles . dash , } ; return { lines , stroke , axisId : axisSpec . id } ; } ) ; } param tickPosition param panelSize internal export function getGridLineForVerticalAxisAt ( tickPosition , panelSize ) { return { x1 : 0 , y1 : tickPosition , x2 : panelSize . width , y2 : tickPosition } ; } param tickPosition param panelSize internal export function getGridLineForHorizontalAxisAt ( tickPosition , panelSize ) { return { x1 : tickPosition , y1 : 0 , x2 : tickPosition , y2 : panelSize . height } ; }