-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxis.js
213 lines (191 loc) · 6.83 KB
/
Axis.js
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
/**
* @file 蜡烛图坐标轴
* @author liuliang<[email protected]>
*/
import React, {Component} from 'react';
import {
Circle,
G,
Path,
Text
} from 'react-native-svg';
import {util} from './utils';
import _ from 'lodash';
import pathjs from './Path';
class AxisStruct {
constructor(scale, options, chartArea, horizontal, curves) {
this.scale = scale;
this.options = options;
this.chartArea = chartArea;
this.margin = chartArea.margin;
this.horizontal = horizontal;
this.curves = curves;
}
static calcStepSize(range, targetSteps) {
const tempStep = range / targetSteps;
const mag = Math.floor(Math.log(tempStep) / Math.log(10));
const magPow = Math.pow(10, mag);
let magMsd = Math.round(tempStep / magPow + 0.5);
if (magMsd > 5.0) {
magMsd = 10.0;
}
else if (magMsd > 2.0) {
magMsd = 5.0;
}
else if (magMsd > 1.0) {
magMsd = 2.0;
}
return magMsd * magPow;
}
static getTickValues(axis, tickCount) {
const tickStep = AxisStruct.calcStepSize((axis.maxValue - axis.minValue), tickCount);
return _.range(axis.minValue, axis.maxValue + 1, tickStep);
}
axis() {
const horizontal = this.horizontal;
const xAxis = this.chartArea.x;
const yAxis = this.chartArea.y;
const currentAxis = horizontal ? xAxis : yAxis;
const tickInterval = this.options.tickCount || 10;
const ticks = util.isSetValue(this.options.tickValues)
&& this.options.tickValues.length !== 0 ? _.map(this.options.tickValues, function (v) {
return v.value;
}) : AxisStruct.getTickValues(currentAxis, tickInterval);
const fixed = this.options.zeroAxis ? this.scale(0) : horizontal ? yAxis.min : xAxis.min;
const start = {x: horizontal ? xAxis.min : fixed, y: horizontal ? fixed : yAxis.min};
const end = {x: horizontal ? xAxis.max : fixed, y: horizontal ? fixed : yAxis.max};
const tailLength = Number(this.options.tailLength || 10);
const margin = this.margin;
if (util.isSetValue(margin)) {
if (horizontal) {
start.x += (margin.left - tailLength) || 0;
start.y += margin.top || 0;
end.x += (margin.left) || 0;
end.y += margin.top || 0;
}
else {
start.x += margin.left || 0;
start.y += (margin.top + tailLength) || 0;
end.x += margin.left || 0;
end.y += (margin.top - tailLength) || 0;
}
}
return {
item: currentAxis,
path: pathjs().moveto(start).lineto(end).closepath(),
ticks: ticks,
lines: ticks.map((c, i) => {
const scaleBase = isNaN(c) ? i : c;
const lineStart = {
x: horizontal ? this.curves[c].centroid + margin.left : xAxis.min + margin.left,
y: horizontal ? yAxis.min + margin.top : this.scale(scaleBase) + margin.top
};
const lineEnd = {
x: horizontal ? lineStart.x : xAxis.max + margin.left,
y: horizontal ? yAxis.max + (margin.top - tailLength) : lineStart.y
};
return pathjs().moveto(lineStart).lineto(lineEnd);
}, this)
};
}
}
export default class Axis extends Component {
render() {
const {
chartArea,
options,
scale,
curves
} = this.props;
const horizontal = {top: true, bottom: true}[options.orient];
const axis = new AxisStruct(scale, options, chartArea, horizontal, curves).axis();
let textAnchor = 'start';
let xy = [0, 0];
switch (options.orient) {
case 'top': {
textAnchor = 'middle';
xy = [0, -5];
break;
}
case 'bottom': {
textAnchor = 'middle';
xy = [0, 5];
break;
}
case 'left': {
textAnchor = 'end';
xy = [-5, -10];
break;
}
case 'right': {
textAnchor = 'start';
xy = [5, 5];
break;
}
}
const textStyle = util.fontAdapt(options.label);
const ticks = _.map(axis.ticks, function (c, i) {
let label = horizontal ? curves[c].item[0].name
: util.isSetValue(options.labelFunction) ? options.labelFunction.call(this, c) : c;
let {length} = axis.ticks;
let scaleBase = isNaN(c) ? i : c;
let gxy = horizontal ? [curves[c].centroid, chartArea.y.min] : [chartArea.x.min, scale(scaleBase)];
const createText = () => (
<Text
x={horizontal ? 0 : xy[0]}
y={horizontal ? 0 : xy[1]}
fontFamily={textStyle.fontFamily}
fontSize={textStyle.fontSize}
fontWeight={textStyle.fontWeight}
fontStyle={textStyle.fontStyle}
fill={options.isSetAxisYLabelFillFunction ? options.isSetAxisYLabelFillFunction(i, length)
: textStyle.fill}
textAnchor={textAnchor}>
{label}
</Text>
);
const createReturnValue = () => (
<G key={i} x={gxy[0]} y={gxy[1]}>
{options.showTicks && <Circle r="2" cx="0" cy="0" stroke="grey" fill="grey" />}
{options.showLabels && createText()}
</G>
);
return createReturnValue();
});
const gridlines = options.showLines ? _.map(axis.lines, function (c, i) {
return (
<Path
key={'gridLines' + i}
d={c.print()}
stroke="#EDEDED"
strokeWidth={1}
fill="none" />
);
}) : [];
let offset = {
x: chartArea.margin.left * -1,
y: chartArea.margin.top * -1
// x: 0,
// y: 0
};
const createPath = () => (
<Path
d={axis.path.print()}
stroke="#EDEDED"
strokeWidth={1}
fill="none" />
);
const createV = () => (
<G>
<G x={offset.x} y={offset.y}>
{options.showAxis && createPath()}
</G>
{ticks}
<G x={offset.x} y={offset.y}>
{gridlines}
</G>
</G>
);
return createV();
}
}