// @flow /* eslint-disable react/no-multi-comp */ import React, { Fragment, PureComponent } from 'react' import { VictoryChart, VictoryArea, VictoryAxis, VictoryTooltip, VictoryVoronoiContainer, VictoryLabel, } from 'victory' import { radii, space, colors, fontSizes } from 'styles/theme' import { ff } from 'styles/helpers' import Box from 'components/base/Box' const ANIMATION_DURATION = 600 const DEFAULT_PROPS = { color: 'blue', padding: 0, } type Props = { height: number, render: Function, } type State = { isAnimationActive: boolean, width: number, } export class WrapperChart extends PureComponent { state = { isAnimationActive: true, width: 0, } componentDidMount() { this._timeout = setTimeout( () => this.setState({ isAnimationActive: false, }), ANIMATION_DURATION * 2, ) if (this._node) { this._ro = new ResizeObserver(entries => { const entry = entries.find(entry => this._node === entry.target) if (entry) { this.setState({ width: entry.contentRect.width, }) } }) this._ro.observe(this._node) } } componentWillUnmount() { clearTimeout(this._timeout) if (this._ro) { this._ro.disconnect() } } _ro = undefined _node = undefined _timeout = undefined render() { const { render, height } = this.props const { isAnimationActive, width } = this.state return ( (this._node = n)} style={{ height }}> {render({ isAnimationActive, height, width })} ) } } function getLinearGradient({ linearGradient, id, color, }: { linearGradient: LinearGradient, id: string, color: string, }) { return linearGradient.length > 0 ? ( {linearGradient.map((g, i) => ( ))} ) : null } type LinearGradient = Array> type GenericChart = { id: string, linearGradient: LinearGradient, strokeWidth: number, height: number, padding: Object | number, color: string, data: Array, } type Chart = GenericChart & { renderLabels: Function, renderTickX: Function, renderTickY: Function, } export const SimpleAreaChart = ({ linearGradient, height, data, strokeWidth, id, padding, color, }: GenericChart) => ( ( {getLinearGradient({ linearGradient, id, color, })} )} /> ) SimpleAreaChart.defaultProps = { height: 50, id: 'simple-chart', linearGradient: [], strokeWidth: 1, ...DEFAULT_PROPS, } const areaChartTooltip = ({ renderLabels }: { renderLabels: Function }) => ( } flyoutStyle={{ fill: colors.dark, stroke: null, }} width={a => space[2] * 2 + renderLabels(a).length * 5.2} // Approximatif size of char for calculate Tooltip width /> ) const AreaChartContainer = export class AreaChart extends PureComponent { static defaultProps = { height: 100, id: 'chart', linearGradient: [[5, 0.2], [50, 0]], strokeWidth: 2, renderLabels: (d: Object) => d.y, renderTickX: (t: any) => t, renderTickY: (t: any) => t, ...DEFAULT_PROPS, } _tooltip = areaChartTooltip({ renderLabels: this.props.renderLabels, }) render() { const { color, data, height, id, linearGradient, padding, renderLabels, renderTickX, renderTickY, strokeWidth, } = this.props const tickLabelsStyle = { fill: colors.grey, fontSize: fontSizes[4], fontFamily: 'inherit', fontWeight: 'inherit', } return ( ( {getLinearGradient({ linearGradient, id, color, })} )} /> ) } }