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 stage
4
stage = anychart.graphics.create("container");
5
6
// set colors for sample
7
textColor = "#7C868E";
8
lineColor = "#CECECE";
9
chart_palette = anychart.palettes.distinctColors().items(["#bC7C63", "#6594AD", "#585953"]);
10
11
// layer for the text elements
12
layer_text = stage.layer();
13
14
// legend on the chart
15
chartLegend = createLegend([
16
{index: 0, text: "Visits", iconFill: "#bC7C63"},
17
{index: 1, text: "Feeding", iconFill: "#6594AD"},
18
{index: 2, text: "Mating", iconFill: "#585953"}
19
]);
20
21
chartLegend.container(layer_text).draw();
22
23
// create main title
24
layer_text.text(20, 20, "Animals activitiy", {fontSize:20});
25
// create subtitle
26
layer_text.text(20, 45, "Abies sibirica, 7 days observation period", {fontSize:18, color:"#998552", opacity:20});
27
28
// jay, woodpecker, squirrel, mouse
29
dataSet = [
30
["1", 4, 0, 0, 4, 4, 0, 3, 1, 1, 4, 10, 2],
31
["2", 2, 1, 0, 6, 5, 0, 2, 1, 0, 9, 11, 3],
32
["3", 2, 1, 0, 2, 1, 0, 4, 1, 1, 7, 8, 4],
33
["4", 2, 0, 0, 2, 2, 0, 4, 2, 0, 1, 12, 0],
34
["5", 3, 1, 1, 0, 0, 0, 1, 0, 0, 0, 10, 6],
35
["6", 1, 1, 0, 3, 2, 1, 0, 0, 0, 8, 10, 5],
36
["7", 2, 0, 1, 6, 3, 2, 2, 0, 1, 0, 7, 0]
37
];
38
39
// map all data as table into one series
40
series = anychart.data.mapAsTable(dataSet);
41
42
// settings for the block describing jay
43
jaySettings = {
44
// latin name of the animal
45
name: "Garrulus glandarius",
46
// position of the text
47
textX: 350,
48
textY: 175,
49
// the name of the picture of the animal in the folder with pictures
50
picture: "jay.jpg",
51
// the picture position
52
pictureY: 130,
53
// the path of the line connecting the picture with its part of the fur-tree
54
linePath: [[210, 190], [300, 130], [370, 130]],
55
// Y-position of the chart
56
chartY: 80,
57
// sets the data of this particular animal from the series
58
data: [series[0], series[1], series[2]],
59
// coordinates of the fur-tree piece where this animal lives
60
treePath: [[175, 120], [250, 240], [100, 240]],
61
// coordinates of the shadow of the fir-tree piece where this animal lives
62
shadowPath: [[175, 165], [250, 290], [100, 290]],
63
// the color of the fur-tree piece where this animal lives
64
treeColor: "#7CB342",
65
// makes this layer overlap others
66
zIndex: 10
67
};
68
// creates a
69
createSpecies(jaySettings);
70
71
// settings for the block describing woodpecker
72
woodpeckerSettings = {
73
name: "Dendrocopos analis",
74
textX: 350,
75
textY: 290,
76
picture: "woodpecker.jpg",
77
pictureY: 245,
78
linePath: [[250, 290], [300, 240], [370, 240]],
79
chartY: 195,
80
data: [series[3], series[4], series[5]],
81
treePath: [[175, 165], [280, 340], [70, 340]],
82
shadowPath: [[175, 240], [280, 395], [70, 395]],
83
treeColor: "#689F38",
84
zIndex: 8
85
};
86
87
createSpecies(woodpeckerSettings);
88
89
squirrelSettings = {
90
name: "Sciurus vulgaris",
91
textX: 360,
92
textY: 405,
93
picture: "sqirrel.jpg",
94
pictureY: 360,
95
linePath: [[260, 365], [370, 365]],
96
chartY: 310,
97
data: [series[6], series[7], series[8]],
98
treePath: [[175, 240], [310, 440], [40, 440]],
99
shadowPath: [[135, 400], [215, 400], [215, 520], [135, 520], [135, 400]],
100
treeColor: "#558B2F",
101
zIndex: 6
102
};
103
104
createSpecies(squirrelSettings);
105
106
mouseSettings = {
107
name: "Apodemus uralensis",
108
textX: 340,
109
textY: 520,
110
picture: "mouse.jpg",
111
pictureY: 475,
112
linePath: [[210, 465], [370, 465]],
113
chartY: 425,
114
data: [series[9], series[10], series[11]],
115
treePath: [[135, 400], [215, 400], [215, 520], [135, 520], [135, 400]],
116
shadowPath: [[135, 400], [215, 400], [215, 570], [135, 570], [135, 400]],
117
treeColor: "#6D4C41",
118
zIndex: 4
119
};
120
121
createSpecies(mouseSettings);
122
123
});
124
125
// an external function for creating legends
126
function createLegend(items) {
127
var legend = anychart.standalones.legend();
128
legend.align("left");
129
legend.margin(75, 0, 20, 10);
130
legend.items(items);
131
return legend;
132
}
133
134
// creates everything connected to the animal
135
function createSpecies(e){
136
var layer = stage.layer();
137
layer.text(e.textX, e.textY, e.name).color(textColor);
138
var pic = draw_picture(layer, 400, [e.pictureY, 40], lineColor, e.picture);
139
var pic_shadow = layer.circle(400, e.pictureY, 60);
140
pic_shadow.fill(["black 0.5","black 0"], .5, .5, null, .5, 0.5, 0.5);
141
pic_shadow.zIndex(-2);
142
pic_shadow.stroke(null);
143
var line = draw_line(layer, e.linePath, lineColor, -1);
144
var line_shadow = draw_line(layer, e.linePath, textColor, -1);
145
line_shadow.visible(false);
146
draw_chart(layer, e.data, [460, e.chartY, 250, 100]);
147
var tree_piece = draw_tree(layer, e.treePath, e.treeColor, 1, false);
148
var tree_shadow = draw_tree(layer, e.shadowPath, "#212121", -1, true);
149
tree_shadow.visible(false);
150
tree_shadow.disablePointerEvents(true);
151
on_hover(tree_piece, tree_shadow, line, line_shadow, pic, pic_shadow);
152
layer.zIndex(e.zIndex);
153
}
154
155
// an external function for drawing a piece of tree
156
function draw_tree(layer, points, color, z_index, shadow){
157
if (shadow) fill = [color + " 0.5", color + " 0.4", color + " 0"]
158
else fill = color;
159
var piece = layer.path().moveTo(points[0][0], points[0][1]).zIndex(z_index).fill(fill, 270).stroke(null);
160
for (var i = 0; i < points.length; i++) piece.lineTo(points[i][0], points[i][1]);
161
return piece;
162
}
163
164
// an external function switching the behaviour
165
function on_hover(piece, shadow, line, lineShadow, img, imgShadow) {
166
toggle(false);
167
piece.listen("mouseOver", function() {toggle(true);});
168
piece.listen("mouseOut", function() {toggle(false);});
169
line.listen("mouseOver", function() {toggle(true);});
170
line.listen("mouseOut", function() {toggle(false);});
171
img.listen("mouseOver", function() {toggle(true);});
172
img.listen("mouseOut", function() {toggle(false);});
173
/* enables and disables all shadows when the mouse
174
is moved over an element or out of it */
175
function toggle(e){
176
shadow.visible(e);
177
lineShadow.visible(e);
178
imgShadow.visible(e);
179
}
180
}
181
182
// draws a chart with passed parameters of the particular animal
183
function draw_chart(container, data, bounds){
184
var chart = anychart.line();
185
chart.palette(chart_palette);
186
187
setupSeries("Visits", data[0], chart);
188
setupSeries("Feeding", data[1], chart);
189
setupSeries("Mating", data[2], chart);
190
191
chart.bounds(bounds);
192
chart.yScale().ticks().interval(2);
193
chart.yAxis().drawFirstLabel(true);
194
chart.xAxis(false);
195
chart.tooltip().title(false);
196
chart.tooltip().separator(false);
197
chart.container(container).draw();
198
}
199
200
// creates series
201
function setupSeries(name, data, chart){
202
var series = chart.line(data);
203
series.name(name);
204
series.clip(false);
205
}
206
207
// draws a line connecting picture of animal and the fur-tree part
208
function draw_line(layer, points, color, z_index){
209
var line = layer.path().moveTo(points[0][0], points[0][1]).stroke(color).zIndex(z_index);
210
for (var i = 0; i < points.length; i++) {
211
line.lineTo(points[i][0], points[i][1]);
212
}
213
return line;
214
}
215
216
// draws a picture for an animal taken from the url
217
function draw_picture(layer, radius, coordinates, color, pic_name) {
218
layer.circle(radius, coordinates[0], coordinates[1])
219
.stroke(color)
220
.fill({src: "https://static.anychart.com/images/graphics_stage_demo/" + pic_name});
221
return layer;
222
}