Stacked area compensado
author(s): Torstein Hønsi
by jpeter06
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<figure class="highcharts-figure">
<div id="parent">
<div id="boton" onclick="showHide()"></div>
<div id="container"> </div>
</div>
<p class="highcharts-description">
Añadimos boton (div id="boton" para mostrar y ocultar linea).
</p>
</figure>
CSS
#container {
height: 400px;
}
#boton{
position:absolute;
color:red;
top:60px;
right:10px;
z-index:10;
width:10px;
height:10px;
border-radius:10px;
box-shadow: 1px 1px 5px black;
cursor:pointer;
border:6px solid white;
background:red;
}
.highcharts-figure,
.highcharts-data-table table {
min-width: 310px;
max-width: 800px;
margin: 1em auto;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #ebebeb;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
background: #f8f8f8;
}
.highcharts-data-table tr:hover {
background: #f1f7ff;
}
JavaScript
var chart = Highcharts.chart('container', {
chart: {
type: 'area'
},
title: {
text: 'Greenhouse gases from Norwegian economic activity'
},
subtitle: {
text: 'Source: ' +
'<a href="https://www.ssb.no/en/statbank/table/09288/"' +
'target="_blank">SSB</a>'
},
yAxis: {
title: {
useHTML: true,
text: 'Million tonnes CO<sub>2</sub>-equivalents'
}
},
tooltip: {
enabled:true,
shared: true,
formatter: function () {
const points = this.points;
return this.points.reduce(function (s, point) {
const i = points.findIndex(e => e.series.name === point.series.name + '_neg');
let addValue = 0;
if (i > -1) {
addValue = points[i].y; console.log(point.series.name, addValue)
}
return s +
(point.series.name.endsWith('_neg') ? '':
'<br/><b>' + point.series.name + '</b>: ' + (point.y + addValue) + 'm');
},
'<b>' + this.x + '</b>');
},
// headerFormat: '<span style="font-size:12px"><b>{point.key}</b></span><br>'
},
plotOptions: {
series: {
pointStart: 2012,
events: {
legendItemClick: function () {
let name = this.name;
Highcharts.each(this.chart.series, function(p, i) {
if (p.name.includes(name) ) {
(!p.visible) ? p.show(): p.hide();
}
})
return false;
}
}
},
area: {
stacking: 'normal',
lineColor: '#666666',
lineWidth: 1,
marker: {
lineWidth: 1,
lineColor: '#666666'
}
}
},
yAxis:...