Skip to content

Commit dce50aa

Browse files
committed
fix(charts): arrowWidth was being ignored
Setting arrowWidth now actually draws an arrow. Closes react-financial#75
1 parent 4c5a0e1 commit dce50aa

File tree

6 files changed

+133
-22
lines changed

6 files changed

+133
-22
lines changed

package-lock.json

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/charts/src/coordinates/EdgeCoordinateV3.tsx

+19-2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export function drawOnCanvas(ctx: CanvasRenderingContext2D, props) {
224224
ctx.setLineDash([]);
225225
if (isDefined(edge.coordinateBase)) {
226226
const {
227+
arrowWidth,
227228
rectWidth,
228229
rectHeight,
229230
rectRadius,
@@ -241,12 +242,28 @@ export function drawOnCanvas(ctx: CanvasRenderingContext2D, props) {
241242
ctx.lineWidth = edge.coordinateBase.strokeWidth;
242243
}
243244

244-
const x = edge.coordinateBase.edgeXRect;
245+
let x = edge.coordinateBase.edgeXRect;
245246
const y = edge.coordinateBase.edgeYRect;
247+
const halfHeight = rectHeight / 2;
246248

247249
ctx.beginPath();
248250

249-
if (rectRadius) {
251+
if (arrowWidth > 0 && edge.orient === "right") {
252+
x -= arrowWidth;
253+
ctx.moveTo(x, y + halfHeight);
254+
ctx.lineTo(x + arrowWidth, y);
255+
ctx.lineTo(x + rectWidth + arrowWidth, y);
256+
ctx.lineTo(x + rectWidth + arrowWidth, y + rectHeight);
257+
ctx.lineTo(x + arrowWidth, y + rectHeight);
258+
ctx.closePath();
259+
} else if (arrowWidth > 0 && edge.orient === "left") {
260+
ctx.moveTo(x, y);
261+
ctx.lineTo(x + rectWidth, y);
262+
ctx.lineTo(x + rectWidth + arrowWidth, y + halfHeight);
263+
ctx.lineTo(x + rectWidth, y + rectHeight);
264+
ctx.lineTo(x, y + rectHeight);
265+
ctx.closePath();
266+
} else if (rectRadius) {
250267
roundRect(ctx, x, y, rectWidth, rectHeight, 3);
251268
} else {
252269
ctx.rect(x + 0.5, y + 0.5, rectWidth, rectHeight);

packages/charts/src/coordinates/EdgeIndicator.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import { drawOnCanvas, renderSVG } from "./EdgeCoordinateV3";
88
import { first, functor, isDefined, last, noop, strokeDashTypes } from "../utils";
99

1010
interface EdgeIndicatorProps {
11-
readonly yAccessor?: any; // func
12-
readonly type?: "horizontal";
11+
readonly arrowWidth?: number;
1312
readonly className?: string;
13+
readonly displayFormat?: any; // func
14+
readonly edgeAt?: "left" | "right";
1415
readonly fill?: string | any; // func
15-
readonly lineStroke?: string | any; // func
16-
readonly textFill?: string | any; // func
1716
readonly itemType: "first" | "last";
17+
readonly lineStroke?: string | any; // func
18+
readonly lineStrokeDasharray?: strokeDashTypes;
1819
readonly orient?: "left" | "right";
19-
readonly edgeAt?: "left" | "right";
20-
readonly displayFormat?: any; // func
2120
readonly rectHeight?: number;
2221
readonly rectWidth?: number;
23-
readonly arrowWidth?: number;
24-
readonly lineStrokeDasharray?: strokeDashTypes;
22+
readonly textFill?: string | any; // func
23+
readonly type?: "horizontal";
24+
readonly yAccessor?: any; // func
2525
}
2626

2727
export class EdgeIndicator extends React.Component<EdgeIndicatorProps> {
@@ -36,7 +36,7 @@ export class EdgeIndicator extends React.Component<EdgeIndicatorProps> {
3636
yAxisPad: 0,
3737
rectHeight: 20,
3838
rectWidth: 50,
39-
arrowWidth: 10,
39+
arrowWidth: 0,
4040
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
4141
fontSize: 13,
4242
dx: 0,

packages/charts/src/coordinates/MouseCoordinateY.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,31 @@ import { drawOnCanvas, renderSVG } from "./EdgeCoordinateV3";
88
import { isNotDefined } from "../utils";
99

1010
interface MouseCoordinateYProps {
11-
readonly displayFormat: any; // func
12-
readonly yAxisPad?: number;
13-
readonly rectWidth?: number;
14-
readonly rectHeight?: number;
15-
readonly orient?: "bottom" | "top" | "left" | "right";
11+
readonly arrowWidth?: number;
1612
readonly at?: "bottom" | "top" | "left" | "right";
13+
readonly displayFormat: any; // func
1714
readonly dx?: number;
18-
readonly fill?: string;
19-
readonly opacity?: number;
2015
readonly fontFamily?: string;
2116
readonly fontSize?: number;
17+
readonly fill?: string;
18+
readonly opacity?: number;
19+
readonly orient?: "bottom" | "top" | "left" | "right";
20+
readonly rectWidth?: number;
21+
readonly rectHeight?: number;
2222
readonly textFill?: string;
23+
readonly yAxisPad?: number;
2324
}
2425

2526
export class MouseCoordinateY extends React.Component<MouseCoordinateYProps> {
2627

2728
public static defaultProps = {
29+
arrowWidth: 0,
2830
yAxisPad: 0,
2931
rectWidth: 50,
3032
rectHeight: 20,
3133
orient: "right",
3234
at: "right",
3335
dx: 0,
34-
arrowWidth: 10,
3536
fill: "#37474F",
3637
opacity: 1,
3738
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { format } from "d3-format";
2+
import * as React from "react";
3+
import { Chart, ChartCanvas } from "react-financial-charts";
4+
import { XAxis, YAxis } from "react-financial-charts/lib/axes";
5+
import { MouseCoordinateY } from "react-financial-charts/lib/coordinates";
6+
import { discontinuousTimeScaleProviderBuilder } from "react-financial-charts/lib/scale";
7+
import { CandlestickSeries } from "react-financial-charts/lib/series";
8+
import { withDeviceRatio } from "react-financial-charts/lib/utils";
9+
import { IOHLCData, withOHLCData, withSize } from "../../data";
10+
11+
interface ChartProps {
12+
readonly arrowWidth?: number;
13+
readonly data: IOHLCData[];
14+
readonly height: number;
15+
readonly ratio: number;
16+
readonly width: number;
17+
}
18+
19+
class Coordinates extends React.Component<ChartProps> {
20+
21+
private readonly margin = { left: 0, right: 48, top: 0, bottom: 24 };
22+
private readonly xScaleProvider = discontinuousTimeScaleProviderBuilder()
23+
.inputDateAccessor((d: IOHLCData) => d.date);
24+
private readonly pricesDisplayFormat = format(".2f");
25+
26+
public render() {
27+
28+
const {
29+
arrowWidth,
30+
data: initialData,
31+
height,
32+
ratio,
33+
width,
34+
} = this.props;
35+
36+
const { margin, xScaleProvider } = this;
37+
38+
const {
39+
data,
40+
xScale,
41+
xAccessor,
42+
displayXAccessor,
43+
} = xScaleProvider(initialData);
44+
45+
const start = xAccessor(data[data.length - 1]);
46+
const end = xAccessor(data[Math.max(0, data.length - 100)]);
47+
const xExtents = [start, end];
48+
49+
return (
50+
<ChartCanvas
51+
height={height}
52+
ratio={ratio}
53+
width={width}
54+
margin={margin}
55+
data={data}
56+
displayXAccessor={displayXAccessor}
57+
seriesName="Data"
58+
xScale={xScale}
59+
xAccessor={xAccessor}
60+
xExtents={xExtents}>
61+
<Chart
62+
id={1}
63+
yExtents={this.yExtents}>
64+
<CandlestickSeries />
65+
<MouseCoordinateY
66+
arrowWidth={arrowWidth}
67+
displayFormat={this.pricesDisplayFormat} />
68+
<XAxis ticks={6} />
69+
<YAxis ticks={5} />
70+
</Chart>
71+
</ChartCanvas>
72+
);
73+
}
74+
75+
private readonly yExtents = (data: IOHLCData) => {
76+
return [data.high, data.low];
77+
}
78+
}
79+
80+
export default withOHLCData()(withSize()(withDeviceRatio()(Coordinates)));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from "react";
2+
import { MouseCoordinateY } from "react-financial-charts/lib/coordinates";
3+
import Coordinates from "./coordinates";
4+
5+
export default {
6+
component: MouseCoordinateY,
7+
title: "Features|Coordinates",
8+
};
9+
10+
export const edge = () => <Coordinates />;
11+
12+
export const arrows = () => <Coordinates arrowWidth={10} />;

0 commit comments

Comments
 (0)