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 column chart
3
var chart = anychart.column();
4
5
// set chart title
6
chart.title('Cherry Chart');
7
8
// create data set on our data
9
var dataSet = anychart.data.set([
10
['Apr 2016', 29, 37],
11
['May 2016', 15, 43],
12
['Jun 2016', 29, 47],
13
['Jul 2016', 12, 27],
14
['Aug 2016', 20, 33],
15
['Sep 2016', 35, 44],
16
['Okt 2016', 20, 31],
17
['Nov 2016', 44, 51]
18
]);
19
20
// set chart padding
21
chart.padding().top(50);
22
23
// map data for the series, take name from the zero column,
24
// low value from the first column and high value from the second column of data set
25
var seriesData = dataSet.mapAs({ x: 0, low: 1, high: 2 });
26
27
// create first series with mapped data
28
var series = chart.rangeColumn(seriesData);
29
30
// get shapes
31
var shapes = series.rendering().shapes();
32
33
// add 'line' shape to shapes group
34
shapes.push({
35
// shape name
36
name: 'line',
37
// shape type ('path', 'circle', 'ellipse' or 'rect')
38
shapeType: 'path',
39
// ZIndex for the balance of shapes relative to each other
40
zIndex: 2
41
});
42
43
// choose the color palettes of the fill point
44
series.fill(function () {
45
return anychart.color.lighten(
46
chart.palette().itemAt(this.index),
47
0.25
48
);
49
});
50
// choose the color palettes of the stroke point
51
series.stroke(function () {
52
return anychart.color.darken(chart.palette().itemAt(this.index), 0.1);
53
});
54
// set rendering settings
55
series
56
.rendering()
57
// set point function to drawing
58
.point(drawer)
59
// set update point function to drawing, change the point shape when the state changes
60
.updatePoint(drawer)
61
// set shapes
62
.shapes(shapes);
63
64
// set titles for Y-axis
65
chart.yAxis().title('Forecast Revenue in Dollars');
66
// set minimum for y-scale
67
chart.yScale().minimum(0).ticks({ interval: 5 });
68
// set container id for the chart
69
chart.container('container');
70
// initiate chart drawing
71
chart.draw();
72
});
73
74
function drawer() {
75
// if missing (not correct data), then skipping this point drawing
76
if (this.missing) {
77
return;
78
}
79
80
// get shapes group
81
var shapes = this.shapes || this.getShapesGroup(this.pointState);
82
// column width
83
var columnWidth = 5;
84
// calculate the left value of the x-axis
85
var leftX = this.x - columnWidth / 2;
86
// calculate the right value of the x-axis
87
var rightX = leftX + columnWidth / 2;
88
// stage width
89
var stageWidth = shapes.line.getStage().width();
90
91
// if pointState > 0, then hover or select point state
92
if (this.pointState > 0) {
93
shapes.line
94
// resets all 'line' operations
95
.clear()
96
// draw dash line
97
.moveTo(0, this.low)
98
.lineTo(leftX, this.low)
99
.moveTo(0, this.high)
100
.lineTo(leftX, this.high)
101
.moveTo(rightX, this.low)
102
.lineTo(stageWidth, this.low)
103
.moveTo(rightX, this.high)
104
.lineTo(stageWidth, this.high)
105
// set stroke to line, get color from shapes.path
106
.stroke({
107
color: shapes.path.stroke().color,
108
dash: '5 10'
109
});
110
} else {
111
// pointState === 0, then normal point state
112
// resets all 'line' operations
113
shapes.line.clear();
114
shapes.path
115
// resets all 'path' operations
116
.clear()
117
// draw bulb
118
.moveTo(leftX, this.low)
119
.lineTo(leftX, this.high)
120
.lineTo(rightX, this.high)
121
.lineTo(rightX, this.low)
122
.arcToByEndPoint(
123
leftX,
124
this.low,
125
columnWidth * 2,
126
columnWidth * 2,
127
true,
128
true
129
)
130
// close by connecting the last point with the first straight line
131
.close();
132
}
133
}