HTMLcopy
x
1
<script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-core.min.js"></script>
2
<script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-sparkline.min.js"></script>
3
<script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-data-adapter.min.js"></script>
4
5
<table id="tb-seasons">
6
<thead>
7
<tr class="main-heading">
8
<th id="tg-head" colspan="4">Money Heist IMDb Ratings</th>
9
</tr>
10
<tr class="col-headings">
11
<th id="tg-head">Season</th>
12
<th id="tg-head">Average rating</th>
13
<th id="tg-head">Rating<br>by episode</th>
14
<th id="tg-head">% of average<br>by episode</th>
15
</tr>
16
</thead>
17
<tbody>
18
</tbody>
19
</table>
CSScopy
30
1
html,
2
body {
3
width: 100%;
4
margin:0;
5
padding: 0;
6
}
7
tr {
8
overflow: hidden;
9
height: 14px;
10
white-space: nowrap;
11
}
12
td {
13
text-align: center;
14
vertical-align: middle;
15
}
16
.chart {
17
height: 20px;
18
width: 80px;
19
}
20
.main-heading {
21
font-family: sans-serif;
22
font-size: 14px;
23
}
24
.col-headings {
25
font-family: sans-serif;
26
font-size: 13px;
27
color: #7c868e;
28
font-weight: 300;
29
text-align: left;
30
}
JavaScriptcopy
50
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(([key, value]) => createSeason(key, data));
6
}
7
);
8
});
9
10
function createSeason(season, data){
11
// get a table link
12
var tbodyRef = document.getElementById('tb-seasons').getElementsByTagName('tbody')[0];
13
14
// insert a row at the end of the table
15
var newRow = tbodyRef.insertRow();
16
newRow.setAttribute('height', '30px');
17
18
// append a season name text node to a cell
19
newRow.insertCell().appendChild(document.createTextNode(season));
20
21
// append a season rating cell
22
newRow.insertCell().appendChild(document.createTextNode(data[season].avg));
23
24
// create a line sparkline chart showing ratings
25
var lineDiv = createSparkline(data[season], 'episodeRatings', 'line', '#040102');
26
27
// append it
28
newRow.insertCell().appendChild(lineDiv);
29
30
// create a column sparkline chart showing percentages
31
var columnDiv = createSparkline(data[season], 'percentOfAvg', 'column', '#cf0011');
32
33
// append it
34
newRow.insertCell().appendChild(columnDiv);
35
}
36
37
// create a sparkline chart of a given type, color, and from a given field
38
function createSparkline(data, field, type, color){
39
var sparkline = anychart.sparkline(data[field]);
40
sparkline.seriesType(type);
41
sparkline.fill(color).stroke(color);
42
43
// create a div for the sparklines and assign a css class name
44
var chartDiv = document.createElement('div');
45
chartDiv.setAttribute('class', 'chart');
46
47
sparkline.container(chartDiv);
48
sparkline.draw();
49
return chartDiv;
50
}