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/sparkline-charts/area-chart/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples/sparkline-charts/area-chart/data.json',
6
function (data) {
7
// create line chart
8
var lineChart = anychart.line();
9
10
lineChart.title('Web Analytics for April 2015');
11
lineChart.xAxis().labels(false).ticks(false).stroke(null);
12
13
lineChart.crosshair().enabled(true).yStroke(null).yLabel(false);
14
15
lineChart
16
.legend()
17
.enabled(true)
18
.title('Visits for months')
19
.inverted(true);
20
21
// create line series based on 'sessions' data
22
lineChart.line().name('Current Month').data(data.sessions);
23
24
// create line series based on 'lastMonthSessions' data
25
lineChart.line().name('Last Month').data(data.lastMonthSessions);
26
27
lineChart.tooltip().title('Visits');
28
29
// create table for sparkline charts layout
30
var sparklineTable = anychart.standalones.table();
31
32
// set content for sparklineTable
33
sparklineTable.contents([
34
[
35
'Session Metrics',
36
'30 Days',
37
'4/10/2015',
38
null,
39
'Traffic Metrics',
40
'30 Days',
41
'4/10/2015'
42
],
43
[
44
'Unique Sessions',
45
createSparkLine(data.uniqueSessions),
46
sum(data.uniqueSessions),
47
null,
48
'Direct Traffic',
49
createSparkLine(data.directTraffic),
50
21
51
],
52
[
53
'Page Sessions',
54
createSparkLine(data.pageSessions),
55
sum(data.pageSessions),
56
null,
57
'Referring Sites',
58
createSparkLine(data.referringSites),
59
19
60
],
61
[
62
'Page/Session',
63
createSparkLine(data.pageToSession),
64
sum(data.pageToSession),
65
null,
66
'Search Engines',
67
createSparkLine(data.searchEngine),
68
19
69
],
70
[
71
'New Sessions',
72
createSparkLine(data.newSessions),
73
sum(data.newSessions),
74
null,
75
'Goal Metrics',
76
'30 Days',
77
'4/10/2015'
78
],
79
[
80
'Bounce Rate',
81
createSparkLine(data.bonusRate),
82
'27%',
83
null,
84
'Goal 1',
85
createSparkLine(data.goal1),
86
18
87
],
88
[
89
'Avg. Time on Site',
90
createSparkLine(data.avgTime),
91
sum(data.avgTime) / 30 + 'ms',
92
null,
93
'Goal 2',
94
createSparkLine(data.goal2),
95
17
96
]
97
]);
98
sparklineTable
99
.hAlign('center')
100
.vAlign('middle')
101
.fontSize(11)
102
.cellBorder(null);
103
104
sparklineTable.getCol(0).hAlign('left');
105
sparklineTable.getCol(4).hAlign('left');
106
sparklineTable.getCol(2).width(70);
107
sparklineTable.getCol(6).width(70);
108
sparklineTable.getCol(3).width(30);
109
110
setupCell(sparklineTable.getRow(0));
111
setupCell(sparklineTable.getCell(4, 4));
112
setupCell(sparklineTable.getCell(4, 5));
113
setupCell(sparklineTable.getCell(4, 6));
114
115
sparklineTable
116
.getCell(0, 3)
117
.border()
118
.bottom({ color: '#fff', thickness: 1 });
119
120
// create table with four rows and three columns
121
// this table provides layout for the line chart and sparkline charts table
122
var layoutTable = anychart.standalones.table(4, 3);
123
124
// disable borders for all table cells
125
layoutTable.cellBorder(null);
126
127
// set table parameters
128
layoutTable.getCol(1).width('95%');
129
layoutTable.getRow(1).height(20);
130
layoutTable.getRow(0).minHeight(200);
131
layoutTable.getRow(2).maxHeight(250);
132
layoutTable.getRow(3).height(20);
133
134
// set content for the tables cells
135
layoutTable.getCell(0, 1).content(lineChart);
136
layoutTable.getCell(2, 1).content(sparklineTable);
137
138
// set container id for the table
139
layoutTable.container('container');
140
// draw the table and all elements it contains
141
// (you don't need to call the draw method for internal tables and charts)
142
layoutTable.draw();
143
}
144
);
145
});
146
147
/**
148
* Helper function, setups header cell.
149
* @param {Cell.<anychart.standalones.table.cell>} Cell to setup
150
*/
151
function setupCell(cell) {
152
cell.vAlign('bottom');
153
cell.fontColor('#545f69');
154
cell.border().bottom({ color: '#EAEAEA', thickness: 1, dash: '1 1 1' });
155
}
156
157
/**
158
* Helper function, creates sparkline chart using anychart.sparkline method,
159
* sets passed data and visual preference.
160
* @param {Array.<number>} array Sparkline data.
161
* @return {anychart.charts.Sparkline} New sparkline chart with passed data.
162
*/
163
function createSparkLine(array) {
164
// create sparkline chart with passed data
165
var sparkLine = anychart.sparkline(array);
166
sparkLine.seriesType('area');
167
// enable minimum point
168
sparkLine.minMarkers().enabled(true);
169
return sparkLine;
170
}
171
172
/**
173
* Calculate sum of values in passed Array.
174
* @param {Array.<number>} array List of numbers to calculate.
175
* @return {number} Calculation result.
176
*/
177
function sum(array) {
178
var summ = 0;
179
for (var i = 0, count = array.length; i < count; i++) {
180
summ += array[i];
181
}
182
return summ;
183
}