HTMLcopy
1
<div id="container"></div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
var chart = anychart.polar();
3
4
chart.padding(0);
5
6
// grid settings
7
chart.yGrid(true).xGrid(true);
8
9
chart.xAxis(true);
10
chart.yAxis(true);
11
12
chart.listen('mouseMove', function (event) {
13
if (event.target.paginator) {
14
return;
15
}
16
17
var interactivity = this.interactivity();
18
19
if (interactivity.hoverMode() !== 'bySpot') {
20
return;
21
}
22
23
var spotRadius = interactivity.spotRadius();
24
25
var clientX = event.clientX;
26
var clientY = event.clientY;
27
28
var container = $(this.container().getStage().container());
29
var containerOffset = container.offset();
30
var scrollLeft = $(document).scrollLeft();
31
var scrollTop = $(document).scrollTop();
32
33
var x = clientX - (containerOffset.left - scrollLeft);
34
var y = clientY - (containerOffset.top - scrollTop);
35
36
if (this.getType() === 'polar' || this.getType() === 'radar') {
37
var dataBounds = this.xAxis().getRemainingBounds();
38
var radius = Math.min(dataBounds.width, dataBounds.height) / 2;
39
var cx = Math.round(dataBounds.left + dataBounds.width / 2);
40
var cy = Math.round(dataBounds.top + dataBounds.height / 2);
41
42
var clientRadius = Math.sqrt(
43
Math.pow(cx - x, 2) + Math.pow(cy - y, 2)
44
); // eslint-disable-line no-restricted-properties
45
46
if (clientRadius > radius) {
47
if (this.spotCircle) this.spotCircle.parent(null);
48
if (this.centerLine) this.centerLine.clear();
49
if (this.leftLine) this.leftLine.clear();
50
if (this.rightLine) this.rightLine.clear();
51
return null;
52
}
53
54
if (!this.spotCircle) {
55
this.spotCircle = anychart.graphics
56
.circle()
57
.radius(spotRadius)
58
.zIndex(1000)
59
.stroke('black .5');
60
}
61
62
if (!this.spotCircle.hasParent()) {
63
this.spotCircle.parent(this.container());
64
}
65
66
this.spotCircle.centerX(x).centerY(y);
67
68
if (!this.centerLine) {
69
this.centerLine = this.container()
70
.path()
71
.zIndex(1000)
72
.stroke('black .2')
73
.disablePointerEvents(true);
74
}
75
this.centerLine.clear().moveTo(cx, cy).lineTo(x, y);
76
77
var dx;
78
var dy;
79
var angle;
80
var leftSideRatio;
81
var rightSideRatio;
82
if (clientRadius - spotRadius >= 0) {
83
dx = cx - x;
84
dy = cy - y;
85
86
angle = Math.atan(dx / dy);
87
if (angle <= 0) angle += Math.PI;
88
if (dx < 0 || (angle === Math.PI && dy > 0)) angle += Math.PI;
89
angle += this.startAngle();
90
91
var dAngle = Math.asin(spotRadius / clientRadius);
92
var leftSideAngle = angle + dAngle;
93
var rightSideAngle = angle - dAngle;
94
95
leftSideRatio = 1 - leftSideAngle / (Math.PI * 2);
96
rightSideRatio = 1 - rightSideAngle / (Math.PI * 2);
97
98
var leftA =
99
((this.startAngle() - 90 + 360 * leftSideRatio) * Math.PI) /
100
180;
101
var rightA =
102
((this.startAngle() - 90 + 360 * rightSideRatio) * Math.PI) /
103
180;
104
105
if (!this.leftLine) {
106
this.leftLine = this.container()
107
.path()
108
.zIndex(1000)
109
.stroke('black .2');
110
}
111
this.leftLine
112
.clear()
113
.moveTo(cx, cy)
114
.lineTo(
115
cx + radius * Math.cos(leftA),
116
cy + radius * Math.sin(leftA)
117
);
118
119
if (!this.rightLine) {
120
this.rightLine = this.container()
121
.path()
122
.zIndex(1000)
123
.stroke('black .2');
124
}
125
this.rightLine
126
.clear()
127
.moveTo(cx, cy)
128
.lineTo(
129
cx + radius * Math.cos(rightA),
130
cy + radius * Math.sin(rightA)
131
);
132
} else {
133
if (this.leftLine) this.leftLine.clear();
134
if (this.rightLine) this.rightLine.clear();
135
}
136
} else {
137
var bounds = this.getPixelBounds();
138
if (!bounds.contains({ x: x, y: y })) {
139
if (this.spotCircle) this.spotCircle.parent(null);
140
return null;
141
}
142
143
if (!this.spotCircle) {
144
this.spotCircle = anychart.graphics
145
.circle()
146
.radius(spotRadius)
147
.zIndex(1000)
148
.stroke('black .5');
149
}
150
151
if (!this.spotCircle.parent()) {
152
this.spotCircle.parent(this.container());
153
}
154
155
this.spotCircle.centerX(x).centerY(y);
156
}
157
});
158
chart.listen('mouseOut', function () {
159
if (!(this.getType() === 'polar' || this.getType() === 'radar')) {
160
if (this.spotCircle) this.spotCircle.parent(null);
161
}
162
});
163
164
chart.interactivity({ hoverMode: 'by-spot', spotRadius: 40 });
165
166
var s1 = chart.line([
167
[0, 1],
168
[1, 2],
169
[2, 3],
170
[3, 4],
171
[4, NaN],
172
[0, 6],
173
[1, 7],
174
[2, 8],
175
[3, NaN],
176
[4, 10],
177
[0, 11],
178
[1, 12],
179
[2, NaN],
180
[3, 14],
181
{ x: 4, value: 15, state: 'selected' },
182
[0, 16],
183
[1, NaN],
184
[2, 18],
185
[3, 19],
186
[4, 20]
187
]);
188
189
var s2 = chart.marker([
190
[0.5, 1],
191
[1.5, 2],
192
[2.5, 3],
193
[3.5, 4],
194
[4.5, NaN],
195
[0.5, 6],
196
{ x: 1.5, value: 7, state: 'selected' },
197
[2.5, 8],
198
[3.5, NaN],
199
[4.5, 10],
200
[0.5, 11],
201
[1.5, 12],
202
[2.5, NaN],
203
[3.5, 14],
204
[4.5, 15],
205
[0.5, 16],
206
[1.5, NaN],
207
[2.5, 18],
208
[3.5, 19],
209
[4.5, 20]
210
]);
211
212
s1.labels().enabled(true);
213
s2.labels().enabled(true);
214
215
s1.markers(true);
216
s1.hovered()
217
.markers()
218
.fill('yellow')
219
.size(s1.hovered().markers().size() + 2)
220
.type('star10');
221
222
s2.hovered().fill('yellow');
223
s2.selected()
224
.size(s2.hovered().size() + 2)
225
.type('star10');
226
227
chart.container('container').draw();
228
});