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
var rawData = [
4
['x', 'Coal', 'Natural Gas', 'Nuclear', 'Hydroelectric'],
5
[1990, 2500, 1000, 1500, 990],
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
// get preprocessed data
37
var preprocessedData = preprocessData(rawData);
38
39
// create chart
40
var chart = anychart.area();
41
42
chart.palette(['#67614c', '#e49333', '#9e70c2', '#7ee3e9']);
43
44
// create and adjust series
45
for (var i = 0; i < preprocessedData.length; i++) {
46
var series = chart.rangeSplineArea(preprocessedData[i]);
47
series.name(rawData[0][i + 1]);
48
series.hovered().markers(false);
49
series.selected().markers(false);
50
51
// add labels
52
var leftValue = preprocessedData[i][0].high - preprocessedData[i][0].low;
53
var leftLabel = {
54
format: leftValue.toFixed(2) + '%',
55
anchor: "left-top",
56
fontColor: 'black'
57
};
58
59
var lastPointIndex = preprocessedData[i].length - 1;
60
var rightValue = preprocessedData[i][lastPointIndex].high - preprocessedData[i][lastPointIndex].low;
61
var rightLabel = {
62
format: rightValue.toFixed(2) + '%',
63
anchor: "right-top",
64
fontColor: 'black'
65
};
66
67
series.getPoint(0).set("label", leftLabel);
68
series.getPoint(lastPointIndex).set("label", rightLabel);
69
}
70
71
// adjust scales
72
chart.yScale().minimum(0);
73
chart.yScale().maximum(100);
74
chart.xScale().mode('continuous');
75
76
// adjust UI elements
77
chart.yAxis().labels().format('{%value}%');
78
chart.tooltip(false);
79
chart.legend(true);
80
81
// render the chart
82
chart.container("container").draw();
83
});
84
85
function preprocessData(rawData) {
86
var seriesCount = rawData[0].length - 1;
87
var tempArr = [];
88
var finalArr = [];
89
//create datasets for all series
90
for (var i = 0; i < seriesCount; i++) {
91
finalArr.push([]);
92
}
93
94
// calculate range value in percents for every point
95
// go through years
96
for ( i = 1; i < rawData.length; i++) {
97
var totalValue = 0;
98
tempArr.push([])
99
// go through series to calculate total value
100
for (var k = 1; k <= seriesCount; k++) {
101
totalValue += rawData[i][k];
102
}
103
// go throug series to create dataSet with percent ranges
104
for (k = 1; k <= seriesCount; k++) {
105
var percentValue = rawData[i][k] * 100 / totalValue;
106
tempArr[i-1].push(percentValue);
107
}
108
}
109
110
// calculate high/low and sort them
111
// go through years
112
for (i = 0; i < tempArr.length; i++) {
113
var previousLow = 100;
114
// go through series
115
for (k = 0; k < seriesCount; k++) {
116
//find max value among series
117
var currentMax = Math.max.apply(null, tempArr[i]);
118
//find the series index which value is considered as current max for this year
119
var currentIndex = tempArr[i].indexOf(currentMax);
120
// drop this value to 0 to not take it into account later
121
tempArr[i][currentIndex] = 0;
122
// create point for the series with the current max value for the current year
123
// previousLow is requred to calculate value offset for the next series
124
var point = {
125
x: rawData[i + 1][0],
126
high: previousLow.toFixed(2),
127
low: (previousLow - currentMax).toFixed(2)
128
};
129
previousLow -= currentMax;
130
// add the point to the series data set
131
finalArr[currentIndex].push(point);
132
}
133
}
134
return finalArr;
135
}
136
137
138
139
140