HTMLcopy
x
1
<div id="container">
2
<div class="container-title">
3
<h1>Annotated Scatter Chart with Interval</h1>
4
<div class="form-group">
5
<label for="first-border">First border: </label><input type="number" id="first-border" value="75" max="200" min="10" class="annotation-input" data-number-annotations="0">
6
</div>
7
8
<div class="form-group">
9
<label for="second-border">Second border: </label><input type="number" id="second-border" value="100" max="200" min="10" class="annotation-input" data-number-annotations="1">
10
</div>
11
12
<br>
13
<p>Points in the interval<span class="result"></span></p>
14
</div>
15
</div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
121
1
anychart.onDocumentReady(function () {
2
var chart = anychart.scatter();
3
4
// create markers
5
chart.marker(getRandomArbitrary(10, 200, 300));
6
chart.padding([15, 20, 20, 20]);
7
chart.xScale().maximum(215);
8
9
// create annotation
10
var annotation = chart.annotations();
11
// create annotation line - bottom
12
annotation
13
.line({
14
// X - part of the first anchor
15
xAnchor: 5,
16
// Y - part of the first anchor
17
valueAnchor: 75,
18
// X - part of the second anchor
19
secondXAnchor: 210,
20
// Y - part of the second anchor
21
secondValueAnchor: 75,
22
// set stroke settings
23
stroke: {
24
thickness: 2,
25
color: '#60727B'
26
}
27
// disable interaction with Annotation
28
})
29
.allowEdit(false);
30
31
// create annotation line - top
32
annotation
33
.line({
34
// X - part of the first anchor
35
xAnchor: 5,
36
// Y - part of the first anchor
37
valueAnchor: 100,
38
// X - part of the second anchor
39
secondXAnchor: 210,
40
// Y - part of the second anchor
41
secondValueAnchor: 100,
42
// set stroke settings
43
stroke: {
44
thickness: 2,
45
color: '#60727B'
46
}
47
// disable interaction with Annotation
48
})
49
.allowEdit(false);
50
51
// set container id for the chart
52
chart.container('container');
53
// initiate chart drawing
54
chart.draw();
55
56
$('.annotation-input').on('change', function (e) {
57
var annotation = chart
58
.annotations()
59
.getAnnotationAt(e.currentTarget.dataset.numberAnnotations);
60
annotation
61
.valueAnchor($(this).val())
62
.secondValueAnchor($(this).val());
63
64
selectPointsInInterval(chart);
65
});
66
67
selectPointsInInterval(chart);
68
});
69
70
function selectPointsInInterval(chart) {
71
var $annotationFirst = $('[data-number-annotations="0"]');
72
var $annotationSecond = $('[data-number-annotations="1"]');
73
74
$('.result').text(function () {
75
var result = [];
76
var seriesToSelect = [];
77
var count = -1;
78
var data;
79
var topVal;
80
var bottomVal;
81
82
if (
83
Number($annotationFirst.val()) <= Number($annotationSecond.val())
84
) {
85
bottomVal = $annotationFirst.val();
86
topVal = $annotationSecond.val();
87
} else {
88
bottomVal = $annotationSecond.val();
89
topVal = $annotationFirst.val();
90
}
91
92
for (var i = 0; i < chart.getSeriesCount(); i++) {
93
var series = chart.getSeriesAt(i);
94
data = chart.getSeriesAt(i).data();
95
var iter = data.getIterator();
96
while (iter.advance()) {
97
count++;
98
if (
99
iter.get('value') >= bottomVal &&
100
iter.get('value') <= topVal
101
) {
102
result.push([iter.get('x'), iter.get('value')]);
103
seriesToSelect.push(count);
104
}
105
}
106
series.select(seriesToSelect);
107
}
108
return ': ' + result.length;
109
});
110
}
111
112
function getRandomArbitrary(min, max, count) {
113
var result = [];
114
for (var i = 0; i < count; i++) {
115
result.push([
116
Math.ceil(Math.random() * (max - min) + min),
117
Math.ceil(Math.random() * (max - min) + min)
118
]);
119
}
120
return result;
121
}