HTMLcopy
1
<div id="anychart-embed-src-bar-charts-bar-chart" class="anychart-embed anychart-embed-src-bar-charts-bar-chart">
2
<div id="container"></div>
3
</div>
CSScopy
x
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
9
10
.anychart-embed-src-bar-charts-bar-chart {
11
max-width: 800px;
12
min-height: 450px;
13
width: 80vw;
14
height: 80vh;
15
margin: 0 auto;
16
}
JavaScriptcopy
134
1
anychart.onDocumentReady(function () {
2
// settings
3
let containerId = "container",
4
highlightLabel = "Average Tuition";
5
6
// theme settings
7
let customTheme = {
8
bar: {
9
defaultFontSettings: {
10
fontFamily: "sofia-pro, sans-serif",
11
bar: {
12
normal: {
13
labels: {
14
enabled: true,
15
fontColor: "#ffffff"
16
}
17
}
18
}
19
},
20
title: {
21
text: "Tuition and Fees 2018 - 2019",
22
fontSize: "20px",
23
fontColor: "#003a70",
24
fontWeight: "bold",
25
fontFamily: "sofia-pro, sans-serif",
26
enabled: true
27
},
28
palette: {
29
type: "distinct",
30
items: ["#003a70"]
31
}
32
}
33
};
34
35
// create bar series with passed data
36
let data = anychart.data.set([
37
{ x: "W&M", title: "William & Mary", value: "23400" },
38
{ x: "VMI", title: "Virginia Military Institute", value: "18862" },
39
{ x: "UVA", title: "University of Virginia", value: "16520" },
40
{ x: "CNU", title: "Christopher Newport", value: "14754" },
41
{ x: "VT", title: "Virginia Tech", value: "13620" },
42
{ x: "LWU", title: "Longwood", value: "13590" },
43
{
44
x: "Average Tuition",
45
title: "Average Tuition & Fees",
46
value: "13210",
47
fill: "#009933",
48
stroke: null,
49
label: { enabled: true, fontColor: "#93328e" }
50
},
51
{ x: "UMW", title: "Mary Washington", value: "12714" },
52
{ x: "GMU", title: "George Mason", value: "12462" },
53
{ x: "JMU", title: "James Madison", value: "12016" },
54
{ x: "RU", title: "Radford", value: "11210" },
55
{ x: "ODU", title: "Old Dominion", value: "10872" },
56
{ x: "UVA-Wise", title: "University of Virginia - Wise", value: "10119" },
57
{ x: "NSU", title: "Norfolk State", value: "9490" },
58
{ x: "VSU", title: "Virginia State University", value: "9056" }
59
]);
60
61
// map the data
62
const seriesData = data.mapAs({
63
x: "x",
64
value: "value",
65
fill: "fill",
66
stroke: "stroke",
67
title: "title",
68
label: "label",
69
yAxis: "yAxis"
70
});
71
72
anychart.theme(customTheme);
73
74
// create bar chart
75
const chart = anychart.bar();
76
77
const series = chart.bar(seriesData);
78
79
chart.animation(true);
80
81
chart.padding([10, 40, 5, 20]);
82
83
// set tooltip settings
84
series
85
.tooltip()
86
.position("right")
87
.anchor("left-center")
88
.offsetX(5)
89
.offsetY(0)
90
.titleFormat("{%Title}")
91
.format("${%Value}{groupsSeparator:\\,}");
92
93
//Enable and format the bar labels
94
chart.labels(true);
95
chart.labels().format("${%Value}{groupsSeparator:\\,}");
96
97
// set yAxis labels formatter
98
chart.yAxis().labels().format("${%Value}{groupsSeparator:\\,}");
99
100
// set titles for axises
101
chart.yAxis().title("One year of tuition");
102
chart.interactivity().hoverMode("by-x");
103
chart.tooltip().positionMode("point");
104
105
// set scale minimum
106
chart.yScale().minimum(0);
107
108
// set container id for the chart
109
chart.container(containerId);
110
111
// initiate chart drawing
112
chart.draw();
113
114
// wait until chart is displayed
115
chart.listen("chartDraw", function () {
116
// get a number of labels on the Y axis
117
var count = chart.xAxis().labels().getLabelsCount();
118
119
// go to through all labels
120
for (var i = 0; i < count; i++) {
121
// get label object
122
let label = chart.xAxis().labels().getLabel(i);
123
124
// get value of the label of this bar
125
let value = chart.xAxis().scale().ticks().get()[i];
126
127
if (value === highlightLabel) {
128
label.fontColor("#93328e");
129
label.draw();
130
}
131
}
132
});
133
});
134