Skip to content

Commit

Permalink
feat: Eliminated unsafe lifecycle methods, rewrote GenericComponent a…
Browse files Browse the repository at this point in the history
…nd GenericChartComponent
  • Loading branch information
nathan-knight authored and markmcdowell committed Jul 12, 2022
1 parent 5da4fd0 commit 7841001
Show file tree
Hide file tree
Showing 12 changed files with 645 additions and 595 deletions.
2 changes: 1 addition & 1 deletion packages/annotations/src/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Label extends React.Component<LabelProps> {

const { xScale, chartConfig, xAccessor } = moreProps;

const yScale = Array.isArray(chartConfig) ? undefined : chartConfig.yScale;
const yScale = Array.isArray(chartConfig) || !chartConfig ? undefined : chartConfig.yScale;

const { xPos, yPos, fillStyle, text } = this.helper(moreProps, xAccessor, xScale, yScale);

Expand Down
21 changes: 16 additions & 5 deletions packages/axes/src/Axis.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
first,
getAxisCanvas,
GenericChartComponent,
GenericComponentRef,
getAxisCanvas,
getStrokeDasharrayCanvas,
last,
strokeDashTypes,
Expand Down Expand Up @@ -57,14 +58,24 @@ interface AxisProps {
readonly zoomCursorClassName?: string;
}

interface Tick {
value: number;
x1: number;
y1: number;
x2: number;
y2: number;
labelX: number;
labelY: number;
}

export class Axis extends React.Component<AxisProps> {
public static defaultProps = {
edgeClip: false,
zoomEnabled: false,
zoomCursorClassName: "",
};

private readonly chartRef = React.createRef<GenericChartComponent>();
private readonly chartRef = React.createRef<GenericComponentRef>();

public render() {
const {
Expand Down Expand Up @@ -138,7 +149,7 @@ export class Axis extends React.Component<AxisProps> {
}

if (showGridLines) {
tickProps.ticks.forEach((tick: any) => {
tickProps.ticks.forEach((tick) => {
drawGridLine(ctx, tick, tickProps, moreProps);
});
}
Expand Down Expand Up @@ -209,7 +220,7 @@ const tickHelper = (props: AxisProps, scale: ScaleContinuousNumeric<number, numb
const sign = orient === "top" || orient === "left" ? -1 : 1;
const tickSpacing = Math.max(innerTickSize, 0) + tickPadding;

let ticks;
let ticks: Tick[];
let dy;
// tslint:disable-next-line: variable-name
let canvas_dy;
Expand Down Expand Up @@ -346,7 +357,7 @@ const drawTicks = (ctx: CanvasRenderingContext2D, result: any) => {
});
};

const drawGridLine = (ctx: CanvasRenderingContext2D, tick: any, result: any, moreProps: any) => {
const drawGridLine = (ctx: CanvasRenderingContext2D, tick: Tick, result: any, moreProps: any) => {
const { orient, gridLinesStrokeWidth, gridLinesStrokeStyle, gridLinesStrokeDasharray } = result;

const { chartConfig } = moreProps;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const Chart = React.memo((props: React.PropsWithChildren<ChartProps>) =>
} = props;

const chartCanvasContextValue = React.useContext(ChartCanvasContext);
const { subscribe, unsubscribe, chartConfig } = chartCanvasContextValue;
const { subscribe, unsubscribe, chartConfigs } = chartCanvasContextValue;

const listener = React.useCallback(
(type: string, moreProps: any, _: any, e: React.MouseEvent) => {
Expand Down Expand Up @@ -89,7 +89,7 @@ export const Chart = React.memo((props: React.PropsWithChildren<ChartProps>) =>
return () => unsubscribe(`chart_${id}`);
}, [subscribe, unsubscribe, id, listener]);

const config = chartConfig.find(({ id }) => id === props.id)!;
const config = chartConfigs.find(({ id }) => id === props.id)!;
const contextValue = React.useMemo(() => {
return {
...chartCanvasContextValue,
Expand All @@ -104,7 +104,9 @@ export const Chart = React.memo((props: React.PropsWithChildren<ChartProps>) =>

return (
<ChartContext.Provider value={contextValue}>
<g transform={`translate(${x}, ${y})`}>{props.children}</g>
<g transform={`translate(${x}, ${y})`} id={`chart_${id}`}>
{props.children}
</g>
</ChartContext.Provider>
);
});
Expand Down
Loading

0 comments on commit 7841001

Please sign in to comment.