HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create a chart
4
var chart = anychart.scatter();
5
6
// configure scales
7
chart.yScale().minimum(-100).ticks().set([-80, -60, -40, -20, 20, 40, 60, 80]);
8
chart.yScale().maximum(100).ticks().set([-80, -60, -40, -20, 20, 40, 60, 80]);
9
chart.xScale().minimum(-100).ticks().set([-80, -60, -40, -20, 20, 40, 60, 80]);
10
chart.xScale().maximum(100).ticks().set([-80, -60, -40, -20, 20, 40, 60, 80]);
11
12
// configure axes
13
chart.xAxis(0, {
14
ticks: {
15
enabled: true,
16
stroke: 'transparent'
17
},
18
orientation: 'left',
19
labels: {
20
enabled: true,
21
fontColor: 'black',
22
fontSize: '1em'
23
},
24
stroke: 'black'
25
});
26
chart.xAxis(1, {
27
ticks: {
28
enabled: true,
29
stroke: 'transparent'
30
},
31
orientation: 'right',
32
labels: {
33
enabled: true,
34
fontColor: 'black',
35
fontSize: '1em'
36
},
37
stroke: 'black'
38
});
39
chart.yAxis(0, {
40
ticks: {
41
enabled: true,
42
stroke: 'transparent'
43
},
44
orientation: 'bottom',
45
labels: {
46
enabled: true,
47
fontColor: 'black',
48
fontSize: '1em'
49
},
50
stroke: 'black'
51
});
52
chart.yAxis(1, {
53
ticks: {
54
enabled: true,
55
stroke: 'transparent'
56
},
57
orientation: 'top',
58
labels: {
59
enabled: true,
60
fontColor: 'black',
61
fontSize: '1em'
62
},
63
stroke: 'black'
64
});
65
66
// change labels to the absolute value of the axis passed as a parameter
67
function setLabelsPositive(axis){
68
axis.labels().format(function(e){
69
return Math.abs(e.value)
70
})
71
}
72
73
// call function for every axis
74
setLabelsPositive(chart.xAxis(0))
75
setLabelsPositive(chart.xAxis(1))
76
setLabelsPositive(chart.yAxis(0))
77
setLabelsPositive(chart.yAxis(1))
78
79
chart.xGrid().enabled(true).stroke('black 1');
80
chart.xGrid().zIndex(2);
81
chart.yGrid().enabled(true).stroke('black 1');
82
chart.yGrid().zIndex(2);
83
84
85
//creating an annotations controller
86
var controller = chart.annotations();
87
controller.zIndex(1)
88
89
// Create top-right
90
var topRight = controller.rectangle({
91
xAnchor: 0,
92
valueAnchor: 0,
93
secondXAnchor: 97,
94
secondValueAnchor: 97,
95
allowEdit: false,
96
fill: "#d04b57",
97
stroke: null,
98
zIndex: -2,
99
});
100
101
// Create bottom-right annotation
102
var bottomRight = controller.rectangle({
103
xAnchor: 0,
104
valueAnchor: 0,
105
secondXAnchor: 100,
106
secondValueAnchor: -100,
107
allowEdit: false,
108
fill: "#acacac",
109
stroke: null,
110
zIndex: -2,
111
});
112
113
// Create top-left annotation
114
var topLeft = controller.rectangle({
115
xAnchor: 0,
116
valueAnchor: 0,
117
secondXAnchor: -88,
118
secondValueAnchor: 88,
119
allowEdit: false,
120
fill: "#a07c93",
121
stroke: null,
122
zIndex: -2,
123
});
124
125
// Create bottom-left annotation
126
var bottomLeft = controller.rectangle({
127
xAnchor: 0,
128
valueAnchor: 0,
129
secondXAnchor: -73,
130
secondValueAnchor: -73,
131
allowEdit: false,
132
fill: "#f49173",
133
stroke: null,
134
zIndex: -2,
135
});
136
137
// Opacity annotation 1
138
var opacityAnnotation1 = controller.rectangle({
139
xAnchor: 20,
140
valueAnchor: 20,
141
secondXAnchor: -20,
142
secondValueAnchor: -20,
143
allowEdit: false,
144
fill: "white 0.3",
145
stroke: null,
146
zIndex: -1,
147
});
148
// Opacity annotation 2
149
var opacityAnnotation2 = controller.rectangle({
150
xAnchor: 40,
151
valueAnchor: 40,
152
secondXAnchor: -40,
153
secondValueAnchor: -40,
154
allowEdit: false,
155
fill: "white 0.3",
156
stroke: null,
157
zIndex: -1,
158
});
159
// Opacity annotation 3
160
var opacityAnnotation3 = controller.rectangle({
161
xAnchor: 60,
162
valueAnchor: 60,
163
secondXAnchor: -60,
164
secondValueAnchor: -60,
165
allowEdit: false,
166
fill: "white 0.3",
167
stroke: null,
168
zIndex: -1,
169
});
170
// Opacity annotation 3
171
var opacityAnnotation4 = controller.rectangle({
172
xAnchor: 80,
173
valueAnchor: 80,
174
secondXAnchor: -80,
175
secondValueAnchor: -80,
176
allowEdit: false,
177
fill: "white 0.3",
178
stroke: null,
179
zIndex: -1,
180
});
181
182
183
chart.crossing().stroke("black", 1);
184
185
// set the container id
186
chart.container("container");
187
chart.draw();
188
});