HTMLcopy
x
1
<button onclick='accumulated()'>Accumulated</button>
2
<button onclick='split()'>Per Month Split</button>
3
<button onclick='area.yScale().stackMode("percent");'>Percent</button>
4
<button onclick='area.yScale().stackMode("value");'>Value</button>
5
6
<div id="container1"></div>
7
<div id="container2"></div>
8
<div id="container3"></div>
9
<div id="container4"></div>
CSScopy
52
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
8
#container1 {
9
/* border: 1px solid blue; */
10
width: 65%;
11
height: 100%;
12
float:left;
13
/*float: left;*/
14
/*width: 600px;*/
15
/*height: 400px;*/
16
/*margin-left: 50px;*/
17
/*margin-top: 30px;*/
18
}
19
20
#container2 {
21
/* border: 1px solid blue; */
22
width: 30%;
23
height: 33%;
24
display: inline-block;
25
float: left;
26
/*width: 600px;*/
27
/*height: 400px;*/
28
/*margin-left: 50px;*/
29
/*margin-top: 30px;*/
30
}
31
32
#container3 {
33
/* border: 1px solid blue; */
34
width: 30%;
35
height: 33%;
36
float: left;
37
/*width: 600px;*/
38
/*height: 400px;*/
39
/*margin-left: 50px;*/
40
/*margin-top: 30px;*/
41
}
42
#container4 {
43
/* border: 1px solid blue; */
44
width: 30%;
45
height: 34%;
46
float: left;
47
/*width: 600px;*/
48
/*height: 400px;*/
49
/*margin-left: 50px;*/
50
/*margin-top: 30px;*/
51
}
52
JavaScriptcopy
273
1
2
let treeMap;
3
let area;
4
let pie;
5
let circlePacking;
6
7
let rawData = [
8
['Jan 2022', 2000, 0, 1200, 500],
9
['Feb 2022', 2000, 0, 1100, 700],
10
['Mar 2022', 2000, 250, 1300, 1100],
11
['Apr 2022', 2000, 450, 2900, 750],
12
['May 2022', 2000, 650, 1550, 0],
13
['Jun 2022', 2000, 350, 1150, 0],
14
['Jul 2022', 2000, 0, 0, 0],
15
['Aug 2022', 2000, 0, 0, 0],
16
['Sep 2022', 2000, 0, 0, 0],
17
['Oct 2022', 2000, 0, 0, 0],
18
['Nov 2022', 2000, 0, 0, 0],
19
['Dec 2022', 2000, 0, 0, 0],
20
];
21
22
23
24
let dataSetClear = anychart.data.set(rawData);
25
let dataSetAcc = accumulate(rawData);
26
27
function split(data){
28
createArea(dataSetClear);
29
}
30
31
function accumulated(data){
32
createArea(dataSetAcc);
33
}
34
35
function accumulate(data){
36
var arrAccumulated = accumulateValues(data);
37
return anychart.data.set(arrAccumulated);
38
}
39
40
function accumulateValues(inputArray) {
41
let accumulatedArray = [];
42
accumulatedArray.push(inputArray[0]);
43
44
for (let i = 1; i < inputArray.length; i++) {
45
let accumulatedRow = [];
46
accumulatedRow.push(inputArray[i][0]);
47
for (let j = 1; j < inputArray[0].length; j++) {
48
accumulatedValue = accumulatedArray[i-1][j] + inputArray[i][j];
49
accumulatedRow.push(accumulatedValue);
50
}
51
52
accumulatedArray.push(accumulatedRow);
53
}
54
55
return accumulatedArray;
56
}
57
58
function createArea(dataSet) {
59
// map data for the first series, take x from the zero area and value from the first area of data set
60
var seriesData_1 = dataSet.mapAs({ 'x': 0, 'value': 1 });
61
var seriesData_2 = dataSet.mapAs({ 'x': 0, 'value': 2 });
62
var seriesData_3 = dataSet.mapAs({ 'x': 0, 'value': 3 });
63
var seriesData_4 = dataSet.mapAs({ 'x': 0, 'value': 4 });
64
var seriesData_5 = dataSet.mapAs({ 'x': 0, 'value': 5 });
65
66
if (area) area.dispose();
67
68
// create area chart
69
area = anychart.column();
70
71
area.minPointLength(10);
72
73
var crosshair = area.crosshair();
74
crosshair.enabled(true).yStroke(null).xStroke('#fff').zIndex(39);
75
crosshair.yLabel().enabled(false);
76
77
area.yScale().stackMode('value');
78
79
// set chart title text settings
80
area.title('Financial expenses');
81
82
area.padding().left(100);
83
area.pointWidth("100%");
84
85
// helper function to setup label settings for all series
86
function setupSeriesLabels(series, name) {
87
series.name(name)
88
.stroke('0 #fff 1')
89
.fill(function () {
90
return this.sourceColor + ' 0.8'
91
});
92
series.hovered().stroke('3 #fff 1');
93
series.hovered().markers()
94
.enabled(true)
95
.type('circle')
96
.size(4)
97
.stroke('1.5 #fff');
98
series.markers().zIndex(100);
99
series.labels().enabled(true);
100
series.labels().position('left-center');
101
series.labels().anchor('right-center');
102
series.clip(false);
103
104
series.labels().format(function(){
105
if (this.index==0) return this.seriesName;
106
});
107
};
108
109
var series1 = area.column(seriesData_1);
110
setupSeriesLabels(series1, 'QLIK Portal');
111
112
// create second series with mapped data
113
var series2 = area.column(seriesData_2);
114
setupSeriesLabels(series2, 'Circular gauge');
115
116
// create third series with mapped data
117
var series3 = area.column(seriesData_3);
118
setupSeriesLabels(series3, 'Decomposition tree');
119
120
// create fourth series with mapped data
121
var series4 = area.column(seriesData_4);
122
setupSeriesLabels(series4, 'Combo Chart');
123
124
// turn on legend
125
area.legend().enabled(true);
126
127
// set titles for axises
128
area.xAxis().title(false);
129
area.xAxis().orientation('top');
130
131
area.yAxis().title('Spent, $');
132
133
// enable grids
134
area.yGrid().stroke('#ddd');
135
area.xGrid().stroke('#ddd');
136
137
/*
138
area.tooltip().titleFormat(function () {
139
const total = this.points.reduce((acc, curr) => {
140
return acc + curr.value;
141
}, 0)
142
return `${this.x}, sum: ${total}$`;
143
});*/
144
145
// interactivity and tooltip settings
146
area.interactivity().hoverMode('by-x');
147
148
// set container id for the chart
149
area.container('container1');
150
151
// initiate chart drawing
152
area.draw();
153
154
// area.yAxis().scale().inverted(true);
155
156
area.listen('pointmousemove', e => {
157
const { pointIndex } = e;
158
159
// console.log(e);
160
161
var seriesList = [];
162
for (var i = 0; i < area.getSeriesCount(); i++) {
163
seriesList.push(area.getSeriesAt(i));
164
}
165
166
var x = '';
167
const points = seriesList.map(series => {
168
const point = series.getPoint(pointIndex);
169
x = point.get('x');
170
return point.get('value');
171
});
172
173
// console.log(points);
174
175
var pieData = points.map((val, index) => {
176
return {
177
x: area.getSeriesAt(index).name(),
178
value: val || 0.001
179
}
180
});
181
pie.title(x);
182
pie.data(pieData);
183
184
var cpData = points.map((val, index) => {
185
return {
186
name: area.getSeriesAt(index).name(),
187
value: val || 0.001
188
}
189
});
190
circlePacking.data(cpData, 'as-tree');
191
192
var tmData = [{name: 'projects', children: cpData}];
193
treeMap.data(tmData, 'as-tree');
194
})
195
}
196
197
function createPie() {
198
pie = anychart.pie([
199
{ x: 'QLIK Portal', value: 1000 },
200
{ x: 'Circular gauge', value: 0 },
201
{ x: 'Decomposition tree', value: 1200 },
202
{ x: 'Combo chart', value: 500 },
203
]);
204
205
pie.labels()
206
.format('{%X}\n{%PercentValue}{decimalsCount:1,zeroFillDecimals:true}%')
207
.fontSize(8);
208
209
pie.legend(false);
210
pie.title('Jan 2022');
211
pie.container('container2');
212
pie.draw();
213
}
214
215
function createCirclePacking() {
216
// create data
217
var data = [
218
{ name: 'QLIK Prortal', value: 1000 },
219
{ name: 'Circular gauge', value: 0 },
220
{ name: 'Decomposition tree', value: 1200 },
221
{ name: 'Combo chart', value: 500 },
222
];
223
224
// create a chart and set the data
225
circlePacking = anychart.circlePacking(data, 'as-tree');
226
227
// set the container id
228
circlePacking.container("container3");
229
230
circlePacking.labels().enabled(true).fontSize(12).position('center').anchor('center').format(function () {
231
const { name, value } = this;
232
return value > 0.001 ? `${name}\n${value}$` : '';
233
});
234
235
// initiate drawing the chart
236
circlePacking.draw();
237
};
238
239
function createTreemap() {
240
// create data
241
var data = [
242
{name: 'projects', children:[
243
{ name: 'QLIK Portal', value: 1000 },
244
{ name: 'Circular gauge', value: 0 },
245
{ name: 'Decomposition tree', value: 1200 },
246
{ name: 'Combo chart', value: 500 }
247
]
248
}
249
];
250
251
// create a chart and set the data
252
treeMap = anychart.treeMap(data, 'as-tree');
253
254
// set the container id
255
treeMap.container("container4");
256
257
/* circlePacking.labels().enabled(true).fontSize(12).position('center').anchor('center').format(function () {
258
const { name, value } = this;
259
return value > 0.001 ? `${name}\n${value}$` : '';
260
});
261
*/
262
263
// initiate drawing the chart
264
treeMap.draw();
265
};
266
267
anychart.onDocumentReady(function () {
268
createArea(dataSetClear);
269
createPie();
270
createCirclePacking();
271
createTreemap();
272
273
});