HTMLcopy
1
<table id="tb-seasons">
2
<thead>
3
<tr class="main-heading">
4
<th colspan="4">Money Heist IMDb Ratings</th>
5
</tr>
6
<tr class="col-headings">
7
<th>Season</th>
8
<th>Average rating</th>
9
<th>Rating<br>by episode</th>
10
<th>% of average<br>by episode</th>
11
</tr>
12
</thead>
13
<tbody>
14
</tbody>
15
</table>
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
anychart.data.loadJsonFile('https://gist.githubusercontent.com/shacheeswadia/21ddfa122938e676bf9e7911e1417d9a/raw/d28d7a22b968bb39d755285d28ce949b31276628/sparklineChartData.json',
3
function(data) {
4
// go through the data and add each season's info and charts to the table
5
Object.entries(data).forEach(function(item) {
6
createSeason(item[0], data);
7
});
8
}
9
);
10
});
11
12
function createSeason(season, data) {
13
// get a table link
14
var tbodyRef = document.getElementById('tb-seasons').getElementsByTagName('tbody')[0];
15
// insert a row at the end of the table
16
var newRow = tbodyRef.insertRow();
17
newRow.setAttribute('height', '30px');
18
// append a season name text node to a cell
19
newRow.insertCell().appendChild(document.createTextNode(season));
20
// append a season rating cell
21
newRow.insertCell().appendChild(document.createTextNode(data[season].avg));
22
// create a line sparkline chart showing ratings
23
var lineDiv = createSparkline(data[season], 'episodeRatings', 'line', '#040102', 'Rating', '');
24
// append it
25
newRow.insertCell().appendChild(lineDiv);
26
// create a column sparkline chart showing percentages
27
var columnDiv = createSparkline(data[season], 'percentOfAvg', 'column', '#cf0011', 'Percent of average', '%');
28
// append it
29
newRow.insertCell().appendChild(columnDiv);
30
}
31
32
// create a sparkline chart of a given type, color, and from a given field
33
function createSparkline(data, field, type, color, tooltipText, tooltipPostfix) {
34
var sparkline = anychart.sparkline(data[field]);
35
sparkline.seriesType(type);
36
sparkline.fill(color).stroke(color);
37
sparkline.tooltip().format(function() {
38
var x = (this.x) + 1;
39
return 'E' + x + '-' + this.value + tooltipPostfix;
40
});
41
// create a div for the sparklines and assign a css class name
42
var chartDiv = document.createElement('div');
43
chartDiv.setAttribute('class', 'chart');
44
sparkline.container(chartDiv);
45
sparkline.draw();
46
return chartDiv;
47
}