|
| 1 | +import { scaleLinear, scaleTime, line, extent, max, min } from 'd3'; |
| 2 | +import { memo, useMemo } from 'react'; |
| 3 | + |
| 4 | +import { formatPrice } from '../../components/formatPrice'; |
| 5 | + |
| 6 | +const TICK_LENGTH = 5; |
| 7 | +const AXIS_HEIGHT = 20; |
| 8 | + |
| 9 | +function LineChart({ data, width, height }: Props) { |
| 10 | + const graphDetails = { |
| 11 | + xScale: scaleTime().range([0, width]), |
| 12 | + yScale: scaleLinear().range([height - AXIS_HEIGHT, 0]), |
| 13 | + lineGenerator: line<{ timestamp: number; price_open: number }>(), |
| 14 | + }; |
| 15 | + const xRange: [number, number] = extent( |
| 16 | + data, |
| 17 | + candle => new Date(1000 * candle.timestamp), |
| 18 | + ) as any; |
| 19 | + const yExtent = [ |
| 20 | + min(data, candle => candle.price_open), |
| 21 | + max(data, candle => candle.price_open), |
| 22 | + ] as [number, number]; |
| 23 | + graphDetails.xScale.domain(xRange); |
| 24 | + graphDetails.yScale.domain(yExtent); |
| 25 | + graphDetails.lineGenerator.x((d, i) => |
| 26 | + graphDetails.xScale(new Date(1000 * d.timestamp)), |
| 27 | + ); |
| 28 | + graphDetails.lineGenerator.y(d => graphDetails.yScale(d.price_open)); |
| 29 | + const path = graphDetails.lineGenerator(data); |
| 30 | + |
| 31 | + const ticks = useMemo(() => { |
| 32 | + const pixelsPerTick = 5000; |
| 33 | + const width = xRange[1] - xRange[0]; |
| 34 | + const numberOfTicksTarget = 5; |
| 35 | + |
| 36 | + return graphDetails.xScale.ticks(numberOfTicksTarget).map(value => ({ |
| 37 | + value, |
| 38 | + xOffset: graphDetails.xScale(value), |
| 39 | + })); |
| 40 | + }, [graphDetails.xScale]); |
| 41 | + |
| 42 | + if (!path) return <div>Failed to generate path</div>; |
| 43 | + |
| 44 | + return ( |
| 45 | + <svg |
| 46 | + viewBox={`0 0 ${width} ${height}`} |
| 47 | + width="100%" |
| 48 | + height={height} |
| 49 | + preserveAspectRatio="none" |
| 50 | + > |
| 51 | + <path d={path} fill="none" stroke="currentColor" strokeWidth="2" /> |
| 52 | + {/* <text |
| 53 | + stroke="#fff" |
| 54 | + style={{ |
| 55 | + fontSize: '10px', |
| 56 | + textAnchor: 'middle', |
| 57 | + transform: 'translate(30px,10px)', |
| 58 | + }} |
| 59 | + > |
| 60 | + {formatPrice.format(yExtent[1])} |
| 61 | + </text> |
| 62 | + <text |
| 63 | + stroke="#fff" |
| 64 | + style={{ |
| 65 | + fontSize: '10px', |
| 66 | + textAnchor: 'middle', |
| 67 | + transform: `translate(30px,${height - AXIS_HEIGHT}px)`, |
| 68 | + }} |
| 69 | + > |
| 70 | + {formatPrice.format(yExtent[0])} |
| 71 | + </text> */} |
| 72 | + {ticks.map(({ value, xOffset }, i) => ( |
| 73 | + <g key={i} transform={`translate(${xOffset}, ${height - AXIS_HEIGHT})`}> |
| 74 | + <line y2={TICK_LENGTH} stroke="currentColor" /> |
| 75 | + <text |
| 76 | + stroke="currentColor" |
| 77 | + style={{ |
| 78 | + fontSize: '10px', |
| 79 | + textAnchor: 'middle', |
| 80 | + transform: 'translateY(20px)', |
| 81 | + }} |
| 82 | + > |
| 83 | + {formatter.format(value)} |
| 84 | + </text> |
| 85 | + </g> |
| 86 | + ))} |
| 87 | + </svg> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +LineChart.defaultProps = { |
| 92 | + width: 500, |
| 93 | + height: 400, |
| 94 | +}; |
| 95 | + |
| 96 | +interface Props { |
| 97 | + data: { timestamp: number; price_open: number }[]; |
| 98 | + width: number; |
| 99 | + height: number; |
| 100 | +} |
| 101 | + |
| 102 | +const formatter = new Intl.DateTimeFormat('en-US', { |
| 103 | + timeStyle: 'medium', |
| 104 | +}); |
| 105 | + |
| 106 | +export default memo(LineChart); |
0 commit comments