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
// The data used in this sample can be obtained from the CDN
3
// https://cdn.anychart.com/samples/tag-cloud/population-by-countries/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples/tag-cloud/population-by-countries/data.json',
6
function (data) {
7
var dataSet = anychart.data.set(data);
8
var colors = anychart.scales
9
.ordinalColor()
10
.colors(['#26959f', '#f18126', '#3b8ad8', '#60727b', '#e24b26']);
11
12
// create tag cloud
13
var chart = anychart.tagCloud();
14
15
// set chart title
16
chart
17
.title('World Population')
18
// set data with settings
19
.data(dataSet)
20
// set color scale
21
.colorScale(colors)
22
// set array of angles, by which words will be placed
23
.angles([-90, 0, 90]);
24
25
// get the color range
26
var colorRange = chart.colorRange();
27
// enabled color range
28
colorRange
29
.enabled(true)
30
// sets color line size
31
.colorLineSize(15);
32
33
// set container id for the chart
34
chart.container('container');
35
// initiate chart drawing
36
chart.draw();
37
38
// save normal fill function to variable
39
var normalFillFunction = chart.normal().fill();
40
// save hover fill function to variable
41
var hoveredFillFunction = chart.hovered().fill();
42
43
// create custom interactivity to hover colorRange
44
chart.listen('pointsHover', function (e) {
45
if (e.actualTarget === colorRange) {
46
// if points exist
47
if (e.points.length) {
48
// set settings for normal state
49
chart.normal({
50
fill: 'black 0.1'
51
});
52
// set settings for hovered state
53
chart.hovered({
54
// get fill color ratio by its number and set fill to hovered state
55
fill: chart
56
.colorScale()
57
.valueToColor(e.point.get('category'))
58
});
59
} else {
60
// set function for normal state
61
chart.normal({
62
fill: normalFillFunction
63
});
64
// set function for hovered state
65
chart.hovered({
66
fill: hoveredFillFunction
67
});
68
}
69
}
70
});
71
}
72
);
73
});