import { functor, GenericChartComponent, last, MoreProps } from "@react-financial-charts/core"; import { format } from "d3-format"; import * as React from "react"; import { ToolTipText } from "./ToolTipText"; import { ToolTipTSpanLabel } from "./ToolTipTSpanLabel"; export interface SingleMAToolTipProps { readonly color: string; readonly displayName: string; readonly fontFamily?: string; readonly fontSize?: number; readonly fontWeight?: number; readonly forChart: number | string; readonly labelFill?: string; readonly labelFontWeight?: number; readonly onClick?: (event: React.MouseEvent, details: any) => void; readonly options: any; readonly origin: [number, number]; readonly textFill?: string; readonly value: string; } export class SingleMAToolTip extends React.Component { public render() { const { color, displayName, fontSize, fontFamily, fontWeight, textFill, labelFill, labelFontWeight, value } = this.props; const translate = "translate(" + this.props.origin[0] + ", " + this.props.origin[1] + ")"; return ( {displayName} {value} ); } private readonly onClick = (event: React.MouseEvent) => { const { onClick, forChart, options } = this.props; if (onClick !== undefined) { onClick(event, { chartId: forChart, ...options }); } }; } interface MovingAverageTooltipProps { readonly className?: string; readonly displayFormat: (value: number) => string; readonly origin: number[]; readonly displayInit?: string; readonly displayValuesFor?: (props: MovingAverageTooltipProps, moreProps: any) => any; readonly onClick?: (event: React.MouseEvent) => void; readonly textFill?: string; readonly labelFill?: string; readonly fontFamily?: string; readonly fontSize?: number; readonly fontWeight?: number; readonly width?: number; readonly options: { yAccessor: (data: any) => number; type: string; stroke: string; windowSize: number; }[]; } // tslint:disable-next-line: max-classes-per-file export class MovingAverageTooltip extends React.Component { public static defaultProps = { className: "react-financial-charts-tooltip react-financial-charts-moving-average-tooltip", displayFormat: format(".2f"), displayInit: "n/a", displayValuesFor: (_: any, props: any) => props.currentItem, origin: [0, 10], width: 65, }; public render() { return ; } private readonly renderSVG = (moreProps: MoreProps) => { const { chartId, chartConfig, chartConfig: { height = 0 } = {}, fullData } = moreProps; const { className, displayInit = MovingAverageTooltip.defaultProps.displayInit, onClick, width = 65, fontFamily, fontSize, fontWeight, textFill, labelFill, origin: originProp, displayFormat, displayValuesFor = MovingAverageTooltip.defaultProps.displayValuesFor, options, } = this.props; const currentItem = displayValuesFor(this.props, moreProps) ?? last(fullData); const config = chartConfig!; const origin = functor(originProp); const [x, y] = origin(width, height); const [ox, oy] = config.origin; return ( {options.map((each, idx) => { const yValue = currentItem && each.yAccessor(currentItem); const tooltipLabel = `${each.type} (${each.windowSize})`; const yDisplayValue = yValue ? displayFormat(yValue) : displayInit; return ( ); })} ); }; }