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,
        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: {
          min: -4400,
          max: 27000,
          startOnTick: false,
          endOnTick: false
        },
    series: [{
        name: 'eólica',
        color: '#66ffaa',
        data: [5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000]

    }, {
        name: 'fotovo',
        color:'orange',
        data: [4752, 4820, 4877, 4925, 10006, 4976, 4946, 4911, 4913]
    }, {
        name: 'NUCLEAR',
        color: '#ff66aa',
        data: [2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000]

    }, {
    	color:'red',
        name: 'POS1',
        data: [-2000, -2000, 2000, 2000, -2000, -2000, 2000, 2000, 2000]
    }, {
   ...