import { Position } from '../../../../../utils/common' ; import { innerPad , outerPad } from '../../../../../utils/dimensions' ; import { Point } from '../../../../../utils/point' ; import { isHorizontalAxis } from '../../../utils/axis_type_utils' ; import { getAllAxisLayersGirth , getTitleDimension , shouldShowTicks } from '../../../utils/axis_utils' ; import { AxisProps } from '../axes' ; import { renderText , TextFont } from '../primitives/text' ; import { renderDebugRect } from '../utils/debug' ; type PanelTitleProps = Pick < AxisProps , 'panelTitle' | 'axisSpec' | 'axisStyle' | 'size' | 'dimension' | 'debug' > ; type TitleProps = PanelTitleProps & { anchorPoint : Point ; } ; const titleFontDefaults = { fontVariant : 'normal' , fontStyle : 'normal' , fontWeight : 'bold' , align : 'center' , baseline : 'middle' , } ; internal export function renderTitle ( ctx , panel , { size : { width , height } , dimension : { maxLabelBboxWidth , maxLabelBboxHeight } , axisSpec : { position , hide : hideAxis , title , timeAxisLayerCount } , axisStyle : { axisPanelTitle , axisTitle , tickLabel , tickLine } , panelTitle , debug , anchorPoint , } ) { const titleToRender = panel ? panelTitle : title ; const axisTitleToUse = panel ? axisPanelTitle : axisTitle ; if ( ! titleToRender || ! axisTitleToUse . visible ) { return ; } const otherAxisTitleToUse = panel ? axisTitle : axisPanelTitle ; const otherTitle = panel ? title : panelTitle ; const horizontal = isHorizontalAxis ( position ) ; const font = { ... titleFontDefaults , ... axisTitleToUse , textColor : axisTitleToUse . fill } ; const tickDimension = shouldShowTicks ( tickLine , hideAxis ) ? tickLine . size + tickLine . padding : 0 ; const maxLabelBoxGirth = horizontal ? maxLabelBboxHeight : maxLabelBboxWidth ; const allLayersGirth = getAllAxisLayersGirth ( timeAxisLayerCount , maxLabelBoxGirth , horizontal ) ; const labelPaddingSum = innerPad ( tickLabel . padding ) + outerPad ( tickLabel . padding ) ; const labelSize = tickLabel . visible ? allLayersGirth + labelPaddingSum : 0 ; const otherTitleDimension = otherTitle ? getTitleDimension ( otherAxisTitleToUse ) : 0 ; const titlePadding = panel || ( axisTitleToUse . visible && title ) ? axisTitleToUse . padding : 0 ; const rotation = horizontal ? 0 : - 90 ; const offset = position === Position . Left || position === Position . Top ? outerPad ( titlePadding ) + ( panel ? otherTitleDimension : 0 ) : tickDimension + labelSize + innerPad ( titlePadding ) + ( panel ? 0 : otherTitleDimension ) ; const x = anchorPoint . x + ( horizontal ? 0 : offset ) ; const y = anchorPoint . y + ( horizontal ? offset : height ) ; const textX = horizontal ? width / 2 + ( panel ? 0 : x ) : font . fontSize / 2 + ( panel ? offset : x ) ; const textY = horizontal ? font . fontSize / 2 + ( panel ? offset : y ) : ( panel ? height : - height + 2 * y ) / 2 ; if ( debug ) renderDebugRect ( ctx , { x , y , width : horizontal ? width : height , height : font . fontSize } , rotation ) ; renderText ( ctx , { x : textX , y : textY } , titleToRender ?? '' , font , rotation ) ; }