Skip to content

Commit 7870a36

Browse files
committed
fix(axis): adding strokeOpacity and missing props
StrokeOpacity added and used instead of opacity. Lots of missing props from axis'. Closes react-financial#29
1 parent 0dee1a6 commit 7870a36

File tree

3 files changed

+224
-156
lines changed

3 files changed

+224
-156
lines changed

packages/charts/src/axes/Axis.tsx

+73-49
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,36 @@ import { AxisZoomCapture } from "./AxisZoomCapture";
99
import { colorToRGBA, first, getStrokeDasharray, identity, isDefined, isNotDefined, last, strokeDashTypes, zipper } from "../utils";
1010

1111
interface AxisProps {
12+
readonly axisZoomCallback?: any; // func
13+
readonly bg: {
14+
h: number;
15+
x: number;
16+
w: number;
17+
y: number;
18+
};
19+
readonly className?: string;
20+
readonly domainClassName?: string;
21+
readonly edgeClip: boolean;
22+
readonly fill?: string;
1223
readonly flexTicks?: boolean;
1324
readonly fontFamily?: string;
1425
readonly fontSize?: number;
1526
readonly fontWeight?: number;
16-
readonly orient?: "top" | "left" | "right" | "bottom";
27+
readonly getMouseDelta: any; // func
28+
readonly getScale: any; // func
1729
readonly innerTickSize?: number;
18-
readonly outerTickSize?: number;
30+
readonly inverted?: boolean;
31+
readonly onContextMenu?: any; // func
32+
readonly onDoubleClick?: any; // func
33+
readonly orient?: "top" | "left" | "right" | "bottom";
34+
readonly outerTickSize: number;
35+
readonly range: number[];
36+
readonly showDomain?: boolean;
37+
readonly showTicks?: boolean;
38+
readonly showTickLabel?: boolean;
39+
readonly stroke: string;
40+
readonly strokeOpacity?: number;
41+
readonly strokeWidth: number;
1942
readonly tickFormat?: (data: any) => string;
2043
readonly tickPadding?: number;
2144
readonly tickSize?: number;
@@ -28,43 +51,28 @@ interface AxisProps {
2851
readonly tickValues?: number[] | any; // func
2952
readonly tickInterval?: number;
3053
readonly tickIntervalFunction?: any; // func
31-
readonly showDomain?: boolean;
32-
readonly showTicks?: boolean;
33-
readonly showTickLabel?: boolean;
34-
readonly className?: string;
35-
readonly axisZoomCallback?: any; // func
54+
readonly transform: number[];
3655
readonly zoomEnabled?: boolean;
37-
readonly inverted?: boolean;
3856
readonly zoomCursorClassName?: string;
39-
readonly transform: number[];
40-
readonly range: number[];
41-
readonly getMouseDelta: any; // func
42-
readonly getScale: any; // func
43-
readonly bg: {
44-
h: number;
45-
x: number;
46-
w: number;
47-
y: number;
48-
};
49-
readonly edgeClip: boolean;
50-
readonly onContextMenu?: any; // func
51-
readonly onDoubleClick?: any; // func
5257
}
5358

5459
class Axis extends React.Component<AxisProps> {
5560

5661
public static defaultProps = {
62+
edgeClip: false,
5763
zoomEnabled: false,
5864
zoomCursorClassName: "",
59-
edgeClip: false,
6065
};
6166

62-
private node;
67+
private node?: GenericChartComponent;
6368

6469
public render() {
65-
const { bg, axisZoomCallback, className, zoomCursorClassName, zoomEnabled, getScale, inverted } = this.props;
66-
const { transform, getMouseDelta, edgeClip } = this.props;
67-
const { onContextMenu, onDoubleClick } = this.props;
70+
const {
71+
bg, axisZoomCallback, className,
72+
zoomCursorClassName, zoomEnabled, getScale,
73+
inverted, transform, getMouseDelta,
74+
edgeClip, onContextMenu, onDoubleClick,
75+
} = this.props;
6876

6977
const zoomCapture = zoomEnabled
7078
? <AxisZoomCapture
@@ -97,12 +105,12 @@ class Axis extends React.Component<AxisProps> {
97105
);
98106
}
99107

100-
private readonly saveNode = (node) => {
108+
private readonly saveNode = (node: GenericChartComponent) => {
101109
this.node = node;
102110
}
103111

104112
private readonly getMoreProps = () => {
105-
return this.node.getMoreProps();
113+
return this.node!.getMoreProps();
106114
}
107115

108116
private readonly renderSVG = (moreProps) => {
@@ -118,13 +126,16 @@ class Axis extends React.Component<AxisProps> {
118126
</g>;
119127
}
120128

121-
private readonly drawOnCanvas = (ctx, moreProps) => {
129+
private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps) => {
122130
const { showDomain, showTicks, transform, range, getScale } = this.props;
123131

124132
ctx.save();
125133
ctx.translate(transform[0], transform[1]);
126134

127-
if (showDomain) { drawAxisLine(ctx, this.props, range); }
135+
if (showDomain) {
136+
drawAxisLine(ctx, this.props, range);
137+
}
138+
128139
if (showTicks) {
129140
const tickProps = tickHelper(this.props, getScale(moreProps));
130141
drawTicks(ctx, tickProps);
@@ -136,14 +147,25 @@ class Axis extends React.Component<AxisProps> {
136147

137148
function tickHelper(props, scale) {
138149
const {
139-
orient, innerTickSize, tickFormat, tickPadding,
140-
tickLabelFill, tickStrokeWidth, tickStrokeDasharray,
141-
fontSize, fontFamily, fontWeight, showTicks, flexTicks,
150+
orient,
151+
innerTickSize,
152+
tickFormat,
153+
tickPadding,
154+
tickLabelFill,
155+
tickStrokeWidth,
156+
tickStrokeDasharray,
157+
fontSize,
158+
fontFamily,
159+
fontWeight,
160+
showTicks,
161+
flexTicks,
142162
showTickLabel,
143-
} = props;
144-
const {
145-
ticks: tickArguments, tickValues: tickValuesProp,
146-
tickStroke, tickStrokeOpacity, tickInterval, tickIntervalFunction,
163+
ticks: tickArguments,
164+
tickValues: tickValuesProp,
165+
tickStroke,
166+
tickStrokeOpacity,
167+
tickInterval,
168+
tickIntervalFunction,
147169
} = props;
148170

149171
let tickValues;
@@ -172,7 +194,7 @@ function tickHelper(props, scale) {
172194

173195
const format = isNotDefined(tickFormat)
174196
? baseFormat
175-
: (d) => tickFormat(d) || "";
197+
: (d: any) => tickFormat(d) || "";
176198

177199
const sign = orient === "top" || orient === "left" ? -1 : 1;
178200
const tickSpacing = Math.max(innerTickSize, 0) + tickPadding;
@@ -266,9 +288,8 @@ function tickHelper(props, scale) {
266288
};
267289
}
268290

269-
function axisLineSVG(props, range) {
270-
const { orient, outerTickSize } = props;
271-
const { domainClassName, fill, stroke, strokeWidth, opacity } = props;
291+
function axisLineSVG(props: AxisProps, range) {
292+
const { domainClassName, orient, outerTickSize, fill, stroke, strokeWidth, strokeOpacity } = props;
272293

273294
const sign = orient === "top" || orient === "left" ? -1 : 1;
274295

@@ -285,22 +306,22 @@ function axisLineSVG(props, range) {
285306
className={domainClassName}
286307
d={d}
287308
fill={fill}
288-
opacity={opacity}
309+
opacity={strokeOpacity}
289310
stroke={stroke}
290311
strokeWidth={strokeWidth} >
291312
</path>
292313
);
293314
}
294315

295-
function drawAxisLine(ctx, props, range) {
316+
function drawAxisLine(ctx: CanvasRenderingContext2D, props: AxisProps, range) {
296317

297-
const { orient, outerTickSize, stroke, strokeWidth, opacity } = props;
318+
const { orient, outerTickSize, stroke, strokeWidth, strokeOpacity } = props;
298319

299320
const sign = orient === "top" || orient === "left" ? -1 : 1;
300321
const xAxis = (orient === "bottom" || orient === "top");
301322

302323
ctx.lineWidth = strokeWidth;
303-
ctx.strokeStyle = colorToRGBA(stroke, opacity);
324+
ctx.strokeStyle = colorToRGBA(stroke, strokeOpacity);
304325

305326
ctx.beginPath();
306327

@@ -409,7 +430,7 @@ function axisTicksSVG(props, scale) {
409430
);
410431
}
411432

412-
function drawTicks(ctx, result) {
433+
function drawTicks(ctx: CanvasRenderingContext2D, result) {
413434

414435
const { tickStroke, tickStrokeOpacity, tickLabelFill } = result;
415436
const { textAnchor, fontSize, fontFamily, fontWeight, ticks, showTickLabel } = result;
@@ -433,19 +454,22 @@ function drawTicks(ctx, result) {
433454
}
434455
}
435456

436-
function drawEachTick(ctx, tick, result) {
457+
function drawEachTick(ctx: CanvasRenderingContext2D, tick, result) {
437458
const { tickStrokeWidth, tickStrokeDasharray } = result;
438459

439460
ctx.beginPath();
440461

441462
ctx.moveTo(tick.x1, tick.y1);
442463
ctx.lineTo(tick.x2, tick.y2);
443464
ctx.lineWidth = tickStrokeWidth;
444-
ctx.setLineDash(getStrokeDasharray(tickStrokeDasharray).split(","));
465+
466+
const lineDash = getStrokeDasharray(tickStrokeDasharray).split(",").map((dash) => Number(dash));
467+
468+
ctx.setLineDash(lineDash);
445469
ctx.stroke();
446470
}
447471

448-
function drawEachTickLabel(ctx, tick, result) {
472+
function drawEachTickLabel(ctx: CanvasRenderingContext2D, tick, result) {
449473
const { canvas_dy, format } = result;
450474

451475
ctx.beginPath();

0 commit comments

Comments
 (0)