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
// create bar chart
3
var chart = anychart.bar();
4
5
var dataSet = anychart.data.set([
6
['2015-12', 10350],
7
['2015-11', 9233],
8
['2015-10', 8610],
9
['2015-09', 7875],
10
['2015-08', 7571],
11
['2015-07', 7266],
12
['2015-06', 6599],
13
['2015-05', 6256],
14
['2015-04', 5808],
15
['2015-03', 5623],
16
['2015-02', 4898],
17
['2015-01', 4482]
18
]);
19
20
// map data for the series
21
var seriesData = dataSet.mapAs({ x: 0, value: 1 });
22
23
// get iterator
24
var iterator = seriesData.getIterator();
25
26
// creates palette
27
var palette = anychart.palettes.rangeColors();
28
palette
29
.items(['#ff0400', '#0600ff'])
30
// get the number of rows in the view
31
.count(iterator.getRowsCount());
32
33
// create series with mapped data
34
var series = chart.bar(seriesData);
35
// set settings for series labels
36
series
37
.labels()
38
.enabled(true)
39
.position('center')
40
.fontColor('#fff')
41
// set labels position formatter
42
.positionFormatter(function () {
43
return {
44
x:
45
series.getPixelBounds().width / 2 +
46
series.getPixelBounds().left,
47
y: this.value.y
48
};
49
})
50
// format labels
51
.format(function () {
52
return (
53
Math.round(
54
(this.value * 100) / this.series.getPoint(0).getStat('value')
55
) + '%'
56
);
57
});
58
59
// set series fill
60
series.fill(function () {
61
// get point fill by index
62
return palette.itemAt(this.index);
63
});
64
65
// disabled y-axis
66
chart.yAxis(false);
67
68
// set settings for x-axis
69
chart
70
.xAxis(0)
71
.title()
72
.enabled(true)
73
.padding({ bottom: 15 })
74
.text('Month')
75
.fontWeight(700);
76
77
// set settings for an additional x-axis
78
var xAxis = chart.xAxis(1);
79
xAxis.orientation('right');
80
xAxis
81
.title()
82
.enabled(true)
83
.padding({ bottom: 15 })
84
.text('Order Count')
85
.fontWeight(700);
86
// format x-axis labels
87
xAxis.labels().format(function () {
88
return (
89
'$' +
90
anychart.format.number(seriesData.get(this.index, 'value'), {
91
groupsSeparator: ','
92
})
93
);
94
});
95
96
// set tooltip format
97
chart.tooltip().format(function () {
98
return (
99
'Percent Value: ' +
100
Math.round(
101
(this.value * 100) / this.series.getPoint(0).getStat('value')
102
) +
103
'%'
104
);
105
});
106
107
var shapes = series.rendering().shapes();
108
109
// set rendering settings
110
series
111
.rendering()
112
// set point function to drawing
113
.point(drawer)
114
// set update point function to drawing, change the point shape when the state changes
115
.updatePoint(drawer)
116
// set shapes
117
.shapes(shapes);
118
119
// set container id for the chart
120
chart.container('container');
121
// initiate chart drawing
122
chart.draw();
123
124
function drawer() {
125
// if missing (not correct data), then skipping this point drawing
126
if (this.missing) {
127
return;
128
}
129
130
// get shapes group
131
var shapes = this.shapes || this.getShapesGroup(this.pointState);
132
// get chart bounds
133
var bounds = series.getPixelBounds();
134
// calculate margin
135
var margin = (bounds.width - this.value) / 2;
136
137
var index;
138
var angleValue;
139
140
// reset iterator, because the drawer function is also used for other states of points (hover/select)
141
iterator.reset();
142
// advances iterator
143
iterator.advance();
144
// returns the index of the item to which iterator points to
145
index = iterator.getIndex();
146
147
// for other states of points (hover/select)
148
// find index of current point
149
while (
150
this.value !==
151
this.series.transformY(this.series.getPoint(index).getStat('value'))
152
) {
153
index++;
154
}
155
156
// increase that would get the index of the next point
157
index++;
158
159
// if this is the last point, then the drawing angle will be 0
160
if (index < iterator.getRowsCount()) {
161
angleValue =
162
(this.value -
163
this.series.transformY(
164
this.series.getPoint(index).getStat('value')
165
)) /
166
2;
167
} else {
168
angleValue = 0;
169
}
170
171
// uncomment to turn off smoothed bars
172
// angleValue = 0;
173
174
// draw point
175
shapes.path
176
// resets all 'line' operations
177
.clear()
178
// set color stroke
179
.stroke('#fff')
180
// move to left bottom position of the point
181
.moveTo(
182
this.zero + margin + angleValue,
183
this.x + this.categoryWidth / 2
184
)
185
// line to left top position of the point
186
.lineTo(this.zero + margin, this.x - this.categoryWidth / 2)
187
// line to right top position of the point
188
.lineTo(
189
this.zero + margin + this.value,
190
this.x - this.categoryWidth / 2
191
)
192
// line to right bottom position of the point
193
.lineTo(
194
this.zero + margin - angleValue + this.value,
195
this.x + this.categoryWidth / 2
196
)
197
// close by connecting the last point with the first straight line
198
.close();
199
}
200
});