|
|
@@ -1,15 +1,19 @@
|
|
|
-import React, { useMemo, useCallback } from 'react';
|
|
|
-import { AreaClosed, Line, Bar } from '@visx/shape';
|
|
|
-import appleStock, { AppleStock } from '@visx/mock-data/lib/mocks/appleStock';
|
|
|
-import { curveMonotoneX } from '@visx/curve';
|
|
|
-import { GridRows, GridColumns } from '@visx/grid';
|
|
|
-import { scaleTime, scaleLinear } from '@visx/scale';
|
|
|
-import { withTooltip, Tooltip, TooltipWithBounds, defaultStyles } from '@visx/tooltip';
|
|
|
-import { WithTooltipProvidedProps } from '@visx/tooltip/lib/enhancers/withTooltip';
|
|
|
-import { localPoint } from '@visx/event';
|
|
|
-import { LinearGradient } from '@visx/gradient';
|
|
|
-import { max, extent, bisector } from 'd3-array';
|
|
|
-import { timeFormat } from 'd3-time-format';
|
|
|
+import React, { useMemo, useCallback } from "react";
|
|
|
+import { AreaClosed, Line, Bar } from "@visx/shape";
|
|
|
+import appleStock, { AppleStock } from "@visx/mock-data/lib/mocks/appleStock";
|
|
|
+import { curveMonotoneX } from "@visx/curve";
|
|
|
+import { scaleTime, scaleLinear } from "@visx/scale";
|
|
|
+import {
|
|
|
+ withTooltip,
|
|
|
+ Tooltip,
|
|
|
+ TooltipWithBounds,
|
|
|
+ defaultStyles,
|
|
|
+} from "@visx/tooltip";
|
|
|
+import { WithTooltipProvidedProps } from "@visx/tooltip/lib/enhancers/withTooltip";
|
|
|
+import { localPoint } from "@visx/event";
|
|
|
+import { LinearGradient } from "@visx/gradient";
|
|
|
+import { max, extent, bisector } from "d3-array";
|
|
|
+import { timeFormat } from "d3-time-format";
|
|
|
|
|
|
/*
|
|
|
export const accentColor = '#f5cb42';
|
|
|
@@ -19,15 +23,15 @@ export const accentColorDark = '#949eff';
|
|
|
type TooltipData = AppleStock;
|
|
|
|
|
|
const stock = appleStock.slice(800);
|
|
|
-export const background = '#3b697800';
|
|
|
-export const background2 = '#20405100';
|
|
|
-export const accentColor = '#949eff';
|
|
|
-export const accentColorDark = '#949eff';
|
|
|
+export const background = "#3b697800";
|
|
|
+export const background2 = "#20405100";
|
|
|
+export const accentColor = "#949eff";
|
|
|
+export const accentColorDark = "#949eff";
|
|
|
const tooltipStyles = {
|
|
|
...defaultStyles,
|
|
|
background,
|
|
|
- border: '1px solid white',
|
|
|
- color: 'white',
|
|
|
+ border: "1px solid white",
|
|
|
+ color: "white",
|
|
|
};
|
|
|
|
|
|
// util
|
|
|
@@ -36,7 +40,7 @@ const formatDate = timeFormat("%b %d, '%y");
|
|
|
// accessors
|
|
|
const getDate = (d: AppleStock) => new Date(d.date);
|
|
|
const getStockValue = (d: AppleStock) => d.close;
|
|
|
-const bisectDate = bisector<AppleStock, Date>(d => new Date(d.date)).left;
|
|
|
+const bisectDate = bisector<AppleStock, Date>((d) => new Date(d.date)).left;
|
|
|
|
|
|
export type AreaProps = {
|
|
|
width: number;
|
|
|
@@ -68,7 +72,7 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
range: [margin.left, innerWidth + margin.left],
|
|
|
domain: extent(stock, getDate) as [Date, Date],
|
|
|
}),
|
|
|
- [innerWidth, margin.left],
|
|
|
+ [innerWidth, margin.left]
|
|
|
);
|
|
|
const stockValueScale = useMemo(
|
|
|
() =>
|
|
|
@@ -77,12 +81,16 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
domain: [0, (max(stock, getStockValue) || 0) + innerHeight / 3],
|
|
|
nice: true,
|
|
|
}),
|
|
|
- [margin.top, innerHeight],
|
|
|
+ [margin.top, innerHeight]
|
|
|
);
|
|
|
|
|
|
// tooltip handler
|
|
|
const handleTooltip = useCallback(
|
|
|
- (event: React.TouchEvent<SVGRectElement> | React.MouseEvent<SVGRectElement>) => {
|
|
|
+ (
|
|
|
+ event:
|
|
|
+ | React.TouchEvent<SVGRectElement>
|
|
|
+ | React.MouseEvent<SVGRectElement>
|
|
|
+ ) => {
|
|
|
const { x } = localPoint(event) || { x: 0 };
|
|
|
const x0 = dateScale.invert(x);
|
|
|
const index = bisectDate(stock, x0, 1);
|
|
|
@@ -90,7 +98,11 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
const d1 = stock[index];
|
|
|
let d = d0;
|
|
|
if (d1 && getDate(d1)) {
|
|
|
- d = x0.valueOf() - getDate(d0).valueOf() > getDate(d1).valueOf() - x0.valueOf() ? d1 : d0;
|
|
|
+ d =
|
|
|
+ x0.valueOf() - getDate(d0).valueOf() >
|
|
|
+ getDate(d1).valueOf() - x0.valueOf()
|
|
|
+ ? d1
|
|
|
+ : d0;
|
|
|
}
|
|
|
showTooltip({
|
|
|
tooltipData: d,
|
|
|
@@ -98,7 +110,7 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
tooltipTop: stockValueScale(getStockValue(d)),
|
|
|
});
|
|
|
},
|
|
|
- [showTooltip, stockValueScale, dateScale],
|
|
|
+ [showTooltip, stockValueScale, dateScale]
|
|
|
);
|
|
|
|
|
|
return (
|
|
|
@@ -112,12 +124,21 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
fill="url(#area-background-gradient)"
|
|
|
rx={14}
|
|
|
/>
|
|
|
- <LinearGradient id="area-background-gradient" from={background} to={background2} />
|
|
|
- <LinearGradient id="area-gradient" from={accentColor} to={accentColor} toOpacity={0} />
|
|
|
+ <LinearGradient
|
|
|
+ id="area-background-gradient"
|
|
|
+ from={background}
|
|
|
+ to={background2}
|
|
|
+ />
|
|
|
+ <LinearGradient
|
|
|
+ id="area-gradient"
|
|
|
+ from={accentColor}
|
|
|
+ to={accentColor}
|
|
|
+ toOpacity={0}
|
|
|
+ />
|
|
|
<AreaClosed<AppleStock>
|
|
|
data={stock}
|
|
|
- x={d => dateScale(getDate(d)) ?? 0}
|
|
|
- y={d => stockValueScale(getStockValue(d)) ?? 0}
|
|
|
+ x={(d) => dateScale(getDate(d)) ?? 0}
|
|
|
+ y={(d) => stockValueScale(getStockValue(d)) ?? 0}
|
|
|
yScale={stockValueScale}
|
|
|
strokeWidth={1}
|
|
|
stroke="url(#area-gradient)"
|
|
|
@@ -184,12 +205,12 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
left={tooltipLeft}
|
|
|
style={{
|
|
|
...defaultStyles,
|
|
|
- background: '#26272f',
|
|
|
- color: '#aaaabb',
|
|
|
+ background: "#26272f",
|
|
|
+ color: "#aaaabb",
|
|
|
width: 100,
|
|
|
paddingTop: 35,
|
|
|
- textAlign: 'center',
|
|
|
- transform: 'translateX(-60px)',
|
|
|
+ textAlign: "center",
|
|
|
+ transform: "translateX(-60px)",
|
|
|
}}
|
|
|
>
|
|
|
{formatDate(getDate(tooltipData))}
|
|
|
@@ -198,5 +219,5 @@ export default withTooltip<AreaProps, TooltipData>(
|
|
|
)}
|
|
|
</div>
|
|
|
);
|
|
|
- },
|
|
|
-);
|
|
|
+ }
|
|
|
+);
|