HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// year | coal | gas | nuclear | hydroelectric
4
var rawData = [
5
[1990, 2500, 1000, 1500, 1000],
6
[1991, 2520, 1150, 1550, 1100],
7
[1992, 2530, 1100, 1540, 1100],
8
[1993, 2540, 1100, 1530, 1000],
9
[1994, 2500, 1150, 1500, 1050],
10
[1995, 2300, 1200, 1600, 1060],
11
[1996, 2250, 1250, 1650, 1070],
12
[1997, 2200, 1200, 1600, 1080],
13
[1998, 2150, 1200, 1500, 1050],
14
[1999, 2100, 1250, 1500, 1010],
15
[2000, 2100, 1250, 1650, 1100],
16
[2001, 2100, 1300, 1600, 1090],
17
[2002, 2100, 1350, 1650, 1050],
18
[2003, 2100, 1300, 1670, 1040],
19
[2004, 2150, 1300, 1680, 1070],
20
[2005, 2200, 1500, 1700, 1030],
21
[2006, 2150, 1500, 1710, 1030],
22
[2007, 2100, 1600, 1710, 1020],
23
[2008, 1700, 1700, 1850, 1010],
24
[2009, 1750, 1700, 1800, 1000],
25
[2010, 1700, 1750, 1800, 1000],
26
[2011, 1650, 1700, 1850, 1010],
27
[2012, 1600, 1750, 1850, 1020],
28
[2013, 1600, 2000, 1750, 1040],
29
[2014, 1500, 2100, 1700, 1050],
30
[2015, 1509, 2050, 1700, 1030],
31
[2016, 1520, 2000, 1650, 1020],
32
[2017, 1510, 2050, 1650, 1010],
33
[2018, 1430, 2100, 1600, 1020]
34
];
35
36
var preprocessedData = preprocessData(rawData);
37
38
39
40
41
var chart = anychart.area();
42
43
44
var series1 = chart.rangeSplineArea(preprocessedData[0]);
45
var series2 = chart.rangeSplineArea(preprocessedData[1]);
46
var series3 = chart.rangeSplineArea(preprocessedData[2]);
47
var series4 = chart.rangeSplineArea(preprocessedData[3]);
48
49
chart.container("container").draw();
50
51
});
52
53
function preprocessData(rawData) {
54
var preprocessedSeriesArr = [];
55
var seriesCount = rawData[0].length - 1;
56
var tempArr = [];
57
var finalArr = [];
58
//create datasets for all series
59
for (var i = 0; i < seriesCount; i++) {
60
finalArr.push([]);
61
}
62
63
/*
64
finalArr = [
65
[{x, h, l}, {x, h, l}],
66
[{}, {}]
67
];
68
*/
69
70
// go through years
71
for ( i = 0; i < rawData.length; i++) {
72
var totalValue = 0;
73
// tempArr.push([rawData[i][0]])
74
tempArr.push([])
75
// go through series to calculate total value
76
for (var k = 1; k <= seriesCount; k++) {
77
totalValue += rawData[i][k];
78
}
79
80
// go throug series to create dataSet with percent ranges
81
for (k = 1; k <= seriesCount; k++) {
82
var percentValue = rawData[i][k] * 100 / totalValue;
83
tempArr[i].push(percentValue);
84
}
85
}
86
// console.log(tempArr);
87
//debugger;
88
// go through years
89
for (i = 0; i < tempArr.length; i++) {
90
var previousLow = 100;
91
// go through series
92
for (k = 0; k < seriesCount; k++) {
93
//find max value
94
var currentMax = Math.max.apply(null, tempArr[i]);
95
//find the series index which value is considered as current max for this year
96
var currentIndex = tempArr[i].indexOf(currentMax);
97
// console.log(currentIndex, currentMax);
98
// drop this value to 0 to not take it into account later
99
tempArr[i][currentIndex] = 0;
100
101
// create point for the series with the current max value for the current year
102
var point = {
103
x: rawData[i][0],
104
high: previousLow,
105
low: previousLow - currentMax
106
};
107
previousLow -= currentMax;
108
finalArr[currentIndex].push(point);
109
}
110
}
111
return finalArr;
112
}
113
114
115
116
117