-
Notifications
You must be signed in to change notification settings - Fork 345
/
axis.ts
262 lines (239 loc) 路 5.98 KB
/
axis.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {isValue, isDefined, isObjectType, isNumber} from "../../module/util";
type AxisOption = {
x?: number | false;
y?: number | false;
y2?: number | false;
} | number | false;
type RangeAxisOption = {
min? : AxisOption;
max?: AxisOption;
};
/**
* Set the min/max value
* @param {Chart} $$ Chart instance
* @param {string} type Set type 'min' or 'max'
* @param {object} value Value to be set
* @private
*/
function setMinMax($$, type: "min" | "max", value: AxisOption): void {
const {config} = $$;
const helper = (key, value) => {
const v = isNumber(value) ? value : (
value === false ? undefined : null
);
if (v !== null) {
config[`axis_${key}_${type}`] = v;
}
};
if (isDefined(value)) {
if (isObjectType(value)) {
Object.keys(value).forEach(key => {
helper(key, value[key]);
});
} else if (isNumber(value) || value === false) {
// shorthand values affects only y and y2.
["y", "y2"].forEach(key => {
helper(key, value);
});
}
$$.redraw({
withUpdateOrgXDomain: true,
withUpdateXDomain: true
});
}
}
/**
* Get the min/max value
* @param {Chart} $$ Chart instance
* @param {string} type Set type 'min' or 'max'
* @returns {{x, y, y2}}
* @private
*/
function getMinMax($$, type: "min" | "max"): {x: number, y: number, y2: number} {
const {config} = $$;
return {
x: config[`axis_x_${type}`],
y: config[`axis_y_${type}`],
y2: config[`axis_y2_${type}`]
};
}
/**
* Define axis
* @ignore
*/
const axis = {
/**
* Get and set axis labels.
* @function axis鈥abels
* @instance
* @memberof Chart
* @param {object} labels specified axis' label to be updated.
* @param {string} [labels.x] x Axis string
* @param {string} [labels.y] y Axis string
* @param {string} [labels.y2] y2 Axis string
* @returns {object|undefined} axis labels text object
* @example
* // Update axis' label
* chart.axis.labels({
* x: "New X Axis Label",
* y: "New Y Axis Label",
* y2: "New Y2 Axis Label"
* });
*
* chart.axis.labels();
* // --> {
* // x: "New X Axis Label",
* // y: "New Y Axis Label",
* // y2: "New Y2 Axis Label"
* // }
*/
labels: function<T>(labels?: {x?: string, y?: string, y2?: string}): T | undefined {
const $$ = this.internal;
let labelText;
if (labels) {
Object.keys(labels).forEach(axisId => {
$$.axis.setLabelText(axisId, labels[axisId]);
});
$$.axis.updateLabels();
}
["x", "y", "y2"].forEach(v => {
const text = $$.axis.getLabelText(v);
if (text) {
!labelText && (labelText = {});
labelText[v] = text;
}
});
return <T>labelText;
},
/**
* Get and set axis min value.
* @function axis鈥in
* @instance
* @memberof Chart
* @param {object} min If min is given, specified axis' min value will be updated.<br>
* If no argument is given, the min values set on generating option for each axis will be returned.
* If not set any min values on generation, it will return `undefined`.<br>
* To unset specific axis max, set `false` to each of them.
* @returns {object|undefined}
* @example
* // Update axis' min
* chart.axis.min({
* x: -10,
* y: 1000,
* y2: 100
* });
*
* // To unset specific axis min, set false to each of them.
* chart.axis.min({
* x: false,
* y: false,
* y2: false
* });
*
* // shorthand (only affects y and y2 axis)
* chart.axis.min(-50);
* chart.axis.min(false);
*/
min: function(min?: AxisOption): object|void {
const $$ = this.internal;
return isValue(min) || min === false ?
setMinMax($$, "min", min as AxisOption) :
getMinMax($$, "min");
},
/**
* Get and set axis max value.
* @function axis鈥ax
* @instance
* @memberof Chart
* @param {object} max If max is given, specified axis' max value will be updated.<br>
* If no argument is given, the max values set on generating option for each axis will be returned.
* If not set any max values on generation, it will return `undefined`.<br>
* To unset specific axis max, set `false` to each of them.
* @returns {object|undefined}
* @example
* // Update axis' label
* chart.axis.max({
* x: 100,
* y: 1000,
* y2: 10000
* });
*
* // To unset specific axis max, set false to each of them.
* chart.axis.max({
* x: false,
* y: false,
* y2: false
* });
*
* // shorthand (only affects y and y2 axis)
* chart.axis.max(10);
* chart.axis.max(false);
*/
max: function(max?: AxisOption): object|void {
const $$ = this.internal;
return isValue(max) || max === false ?
setMinMax($$, "max", max as AxisOption) :
getMinMax($$, "max");
},
/**
* Get and set axis min and max value.
* @function axis鈥ange
* @instance
* @memberof Chart
* @param {object} range If range is given, specified axis' min and max value will be updated.
* If no argument is given, the current min and max values for each axis will be returned.<br>
* To unset specific axis max, set `false` to each of them.
* @returns {object|undefined}
* @example
* // Update axis' label
* chart.axis.range({
* min: {
* x: -10,
* y: -1000,
* y2: -10000
* },
* max: {
* x: 100,
* y: 1000,
* y2: 10000
* },
* });
*
* // To unset specific axis max, set false to each of them.
* chart.axis.range({
* min: {
* x: false,
* y: false,
* y2: false
* },
* max: {
* x: false,
* y: false,
* y2: false
* },
* });
*
* // shorthand (only affects y and y2 axis)
* chart.axis.range({ min: -50, max: 1000 });
* chart.axis.range({ min: false, max: false });
*/
range: function(range: RangeAxisOption): object|void {
const {axis} = this;
if (arguments.length) {
const {min, max} = range;
isDefined(max) && axis.max(max);
isDefined(min) && axis.min(min);
} else {
return {
max: axis.max(),
min: axis.min()
};
}
return undefined;
}
};
export default {axis};