This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
/
XYChartLayout.tsx
185 lines (162 loc) · 5.45 KB
/
XYChartLayout.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* eslint-disable sort-keys, no-magic-numbers */
import React, { ReactNode } from 'react';
import collectScalesFromProps from '@data-ui/xy-chart/esm/utils/collectScalesFromProps';
import { XAxis, YAxis } from '@data-ui/xy-chart';
import { ChartTheme } from '@data-ui/theme';
import { Margin, mergeMargin } from '@superset-ui/dimension';
import createTickComponent from './createTickComponent';
import ChartFrame from '../components/ChartFrame';
import ChannelEncoder from '../encodeable/ChannelEncoder';
import { AxisOrient } from '../encodeable/types/Axis';
import { XFieldDef, YFieldDef } from '../encodeable/types/ChannelDef';
import { PlainObject } from '../encodeable/types/Data';
import { DEFAULT_LABEL_ANGLE } from './constants';
// Additional margin to avoid content hidden behind scroll bar
const OVERFLOW_MARGIN = 8;
interface Input {
width: number;
height: number;
minContentWidth?: number;
minContentHeight?: number;
margin: Margin;
xEncoder: ChannelEncoder<XFieldDef>;
yEncoder: ChannelEncoder<YFieldDef>;
children: ReactNode[];
theme: ChartTheme;
}
export default class XYChartLayout {
chartWidth: number;
chartHeight: number;
containerWidth: number;
containerHeight: number;
margin: Margin;
spec: Input;
xLayout?: {
labelOffset: number;
labelOverlap: string;
labelAngle: number;
tickTextAnchor?: string;
minMargin: Partial<Margin>;
orient: AxisOrient;
};
yLayout?: {
labelOffset: number;
minMargin: Partial<Margin>;
orient: AxisOrient;
};
// eslint-disable-next-line complexity
constructor(spec: Input) {
this.spec = spec;
const {
width,
height,
minContentWidth = 0,
minContentHeight = 0,
margin,
xEncoder,
yEncoder,
children,
theme,
} = spec;
const { xScale, yScale } = collectScalesFromProps({
width,
height,
margin,
xScale: xEncoder.definition.scale || {},
yScale: yEncoder.definition.scale || {},
theme,
children,
});
if (typeof yEncoder.scale !== 'undefined') {
yEncoder.scale.setDomain(yScale.domain());
}
if (typeof yEncoder.axis !== 'undefined') {
this.yLayout = yEncoder.axis.computeLayout({
axisWidth: Math.max(height - margin.top - margin.bottom),
// @ts-ignore
tickLength: theme.yTickStyles.length || theme.yTickStyles.tickLength,
tickTextStyle: theme.yTickStyles.label.right,
});
}
const secondMargin = this.yLayout ? mergeMargin(margin, this.yLayout.minMargin) : margin;
const innerWidth = Math.max(width - secondMargin.left - secondMargin.right, minContentWidth);
if (typeof xEncoder.scale !== 'undefined') {
xEncoder.scale.setDomain(xScale.domain());
}
if (typeof xEncoder.axis !== 'undefined') {
this.xLayout = xEncoder.axis.computeLayout({
axisWidth: innerWidth,
labelAngle: this.recommendXLabelAngle(xEncoder.axis.config.orient as 'top' | 'bottom'),
// @ts-ignore
tickLength: theme.xTickStyles.length || theme.xTickStyles.tickLength,
tickTextStyle: theme.xTickStyles.label.bottom,
});
}
const finalMargin = this.xLayout
? mergeMargin(secondMargin, this.xLayout.minMargin)
: secondMargin;
const innerHeight = Math.max(height - finalMargin.top - finalMargin.bottom, minContentHeight);
const chartWidth = Math.round(innerWidth + finalMargin.left + finalMargin.right);
const chartHeight = Math.round(innerHeight + finalMargin.top + finalMargin.bottom);
const isOverFlowX = chartWidth > width;
const isOverFlowY = chartHeight > height;
if (isOverFlowX) {
finalMargin.bottom += OVERFLOW_MARGIN;
}
if (isOverFlowY) {
finalMargin.right += OVERFLOW_MARGIN;
}
this.chartWidth = isOverFlowX ? chartWidth + OVERFLOW_MARGIN : chartWidth;
this.chartHeight = isOverFlowY ? chartHeight + OVERFLOW_MARGIN : chartHeight;
this.containerWidth = width;
this.containerHeight = height;
this.margin = finalMargin;
}
recommendXLabelAngle(xOrient: 'top' | 'bottom' = 'bottom') {
const { axis } = this.spec.yEncoder;
return !this.yLayout ||
(typeof axis !== 'undefined' &&
((axis.config.orient === 'right' && xOrient === 'bottom') ||
(axis.config.orient === 'left' && xOrient === 'top')))
? DEFAULT_LABEL_ANGLE
: -DEFAULT_LABEL_ANGLE;
}
renderChartWithFrame(renderChart: (input: { width: number; height: number }) => ReactNode) {
return (
<ChartFrame
width={this.containerWidth}
height={this.containerHeight}
contentWidth={this.chartWidth}
contentHeight={this.chartHeight}
renderContent={renderChart}
/>
);
}
renderXAxis(props?: PlainObject) {
const { axis } = this.spec.xEncoder;
return axis && this.xLayout ? (
<XAxis
label={axis.getTitle()}
labelOffset={this.xLayout.labelOffset}
numTicks={axis.config.tickCount}
orientation={axis.config.orient}
tickComponent={createTickComponent(this.xLayout)}
tickFormat={axis.getFormat()}
{...props}
/>
) : null;
}
renderYAxis(props?: PlainObject) {
const { axis } = this.spec.yEncoder;
return axis && this.yLayout ? (
<YAxis
label={axis.getTitle()}
labelOffset={this.yLayout.labelOffset}
numTicks={axis.config.tickCount}
orientation={axis.config.orient}
tickFormat={axis.getFormat()}
{...props}
/>
) : null;
}
}