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
136
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
chart.labels().background().enabled(true).fill('white 0.7');
97
chart.labels().background().cornerType("round").corners(5, 5, 5, 5);
98
99
// set yAxis labels formatter
100
chart.yAxis().labels().format("${%Value}{groupsSeparator:\\,}");
101
102
// set titles for axises
103
chart.yAxis().title("One year of tuition");
104
chart.interactivity().hoverMode("by-x");
105
chart.tooltip().positionMode("point");
106
107
// set scale minimum
108
chart.yScale().minimum(0);
109
110
// set container id for the chart
111
chart.container(containerId);
112
113
// initiate chart drawing
114
chart.draw();
115
116
// wait until chart is displayed
117
chart.listen("chartDraw", function () {
118
// get a number of labels on the Y axis
119
var count = chart.xAxis().labels().getLabelsCount();
120
121
// go to through all labels
122
for (var i = 0; i < count; i++) {
123
// get label object
124
let label = chart.xAxis().labels().getLabel(i);
125
126
// get value of the label of this bar
127
let value = chart.xAxis().scale().ticks().get()[i];
128
129
if (value === highlightLabel) {
130
label.fontColor("#93328e");
131
label.draw();
132
}
133
}
134
});
135
});
136