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/bullet-chart/horizontal-bullet-chart-with-negative-values/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples/bullet-chart/horizontal-bullet-chart-with-negative-values/data.json',
6
function (data) {
7
var dataSet = data;
8
9
var table = anychart.standalones.table();
10
table.getCol(1).width(100).hAlign('right').cellPadding(0, 10, 0, 0);
11
table
12
.getCol(3)
13
.width(60)
14
.useHtml(true)
15
.hAlign('center')
16
.cellPadding(0, 0, 0, 0);
17
table.getRow(0).fontSize(15);
18
table.cellBorder(null);
19
table.getRow(1).cellBorder().bottom('1px #CECECE');
20
table.getCol(1).cellBorder().right('1px #CECECE');
21
table.getCol(2).cellPadding(0, 0, 0, 10);
22
23
table.contents([
24
[null, null, null, null, null],
25
[null, 'Region', null, null, null],
26
[
27
null,
28
'Alabama',
29
createBulletChart('Alabama'),
30
calculatePercentToTarget('Alabama'),
31
null
32
],
33
[
34
null,
35
'Alaska',
36
createBulletChart('Alaska'),
37
calculatePercentToTarget('Alaska'),
38
null
39
],
40
[
41
null,
42
'Arizona',
43
createBulletChart('Arizona'),
44
calculatePercentToTarget('Arizona'),
45
null
46
],
47
[
48
null,
49
'Idaho',
50
createBulletChart('Idaho'),
51
calculatePercentToTarget('Idaho'),
52
null
53
],
54
[
55
null,
56
'Illinois',
57
createBulletChart('Illinois'),
58
calculatePercentToTarget('Illinois'),
59
null
60
],
61
[
62
null,
63
'Indiana',
64
createBulletChart('Indiana'),
65
calculatePercentToTarget('Indiana'),
66
null
67
],
68
[
69
null,
70
'Ohio',
71
createBulletChart('Ohio'),
72
calculatePercentToTarget('Ohio'),
73
null
74
],
75
[
76
null,
77
'Oklahoma',
78
createBulletChart('Oklahoma'),
79
calculatePercentToTarget('Oklahoma'),
80
null
81
],
82
[
83
null,
84
'Oregon',
85
createBulletChart('Oregon'),
86
calculatePercentToTarget('Oregon'),
87
null
88
],
89
[
90
null,
91
'Vermont',
92
createBulletChart('Vermont'),
93
calculatePercentToTarget('Vermont'),
94
null
95
],
96
[
97
null,
98
'Virginia',
99
createBulletChart('Virginia'),
100
calculatePercentToTarget('Virginia'),
101
null
102
],
103
[
104
null,
105
'Washington',
106
createBulletChart('Washington'),
107
calculatePercentToTarget('Washington'),
108
null
109
],
110
[null, null, null, null, null]
111
]);
112
table.getCol(0).width(40);
113
table.getCol(4).width(40);
114
115
table
116
.getCell(0, 1)
117
.colSpan(3)
118
.content('ACME corp. Sales by Region')
119
.hAlign('center')
120
.vAlign('bottom')
121
.fontColor('#212121')
122
.fontSize(16);
123
table.getCell(0, 1).colSpan(3).border().right('white');
124
125
table.getCell(1, 0).border().bottom('white');
126
table.getCell(1, 4).border().bottom('white');
127
table.getCell(14, 1).border().right('white');
128
129
table
130
.getCell(1, 2)
131
.colSpan(2)
132
.content('Variance from target')
133
.hAlign('right');
134
135
table.container('container');
136
table.vAlign('middle');
137
table.draw();
138
139
function createBulletChart(name) {
140
var data = dataSet[name];
141
var target = summ(data.toGoal);
142
var actual = summ(data.actualSales);
143
var bullet = anychart.bullet([
144
{
145
value: Math.round(actual),
146
type: 'bar',
147
gap: 0.7,
148
fill: '#545f69',
149
stroke: null
150
},
151
{
152
value: Math.round(target),
153
type: 'line',
154
gap: 0.2,
155
fill: '#545f69',
156
stroke: {
157
thickness: 2,
158
color: '#545f69'
159
}
160
}
161
]);
162
bullet
163
.margin(5, 55, 5, 0)
164
.axis(null)
165
.title(false)
166
.padding(0, -1)
167
.layout('horizontal');
168
169
return bullet;
170
}
171
172
function calculatePercentToTarget(name) {
173
var data = dataSet[name];
174
var target = summ(data.toGoal);
175
var actual = summ(data.actualSales);
176
var diff = 100 - Math.round((actual * 100) / target);
177
if (diff < 0) {
178
return (
179
'<span style="color: #ef6c00"> +' +
180
Math.abs(diff) +
181
'% </span>'
182
);
183
}
184
return (
185
'<span style="color: #1976d2">-' + Math.abs(diff) + '%</span>'
186
);
187
}
188
189
function summ(arr) {
190
var result = 0;
191
for (var i = 0; i < arr.length; i++) {
192
result += arr[i];
193
}
194
return result;
195
}
196
}
197
);
198
});