import React, { useState, memo } from 'react'; import { FlexibleXYPlot, HorizontalGridLines, LineSeries, XAxis, YAxis, Crosshair } from 'react-vis'; import moment from 'moment'; import numeral from 'numeral'; import shortId from 'shortid'; import isEmpty from 'lodash/isEmpty'; import capitalize from 'lodash/capitalize'; import uniq from 'lodash/uniq'; import './LineSeriesPlot.css'; interface IProps { lines: any; xProps: any; yProps: any; } /** * ATTENTION: React-vis don't have typescript support, so we can't define * props with right interface */ export function LineSeriesPlot({ lines, xProps, yProps, ...rest }: IProps) { const [crosshairValues, changeCrosshairValues] = useState([]); const handleOnNearestX = (value: any) => { let values: any[] = []; lines?.map((line: any) => { const data = line.data.filter((l) => l.x === value.x); if (!isEmpty(data)) { values = [ ...values, { key: line.key, items: data, ...data[0] } ]; } }); changeCrosshairValues(values); }; const yAxisProps = { title: 'Totale', tickFormat: (value: any) => numeral(value).format('0a'), ...yProps }; let xAxisProps = { title: 'Tempo', // tickFormat: value => value > 0 && moment(value).format('DD/MM/YY'), ...xProps }; const possibileValues: any[] = []; lines.forEach((line: any) => line.data.forEach((l: any) => { possibileValues.push(l.x); }) ); if (!isEmpty(possibileValues) && possibileValues.length <= 35) { xAxisProps = { ...xAxisProps, tickValues: uniq(possibileValues) }; } return ( changeCrosshairValues([])} {...rest}> {lines && lines.map((line: any) => ( ))} {!isEmpty(crosshairValues) && ( ({ title: 'Data', value: i[0] && i[0].items && i[0].items[0] && i[0].items[0].x && moment(Number(i[0].items[0].x)).format('DD/MM/YY') })} itemsFormat={(itemsFormatMap: any) => itemsFormatMap.map((i: any) => ({ title: i.key && capitalize(i.key), value: i.items && i.items[0] && numeral(i.items[0].y).format('0a') })) } values={crosshairValues} /> )} ); } export default memo(LineSeriesPlot);