Responsive chart

author(s): Torstein Hønsi

by prateekbansal07

HTML

<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/data.js"></script>

<div id="container"></div>

<button id="large">Large</button>
<button id="small">Small</button>
<button id="auto">Auto</button>

CSS

#container {
	min-width: 310px;
	max-width: 800px;
}

JavaScript

Highcharts.getJSON('https://demo-live-data.highcharts.com/aapl-c.json', data => {
    const chart = Highcharts.stockChart('container', {
        chart: {
            height: 400
        },

        title: {
            text: 'Highstock Responsive Chart'
        },

        subtitle: {
            text: 'Click small/large buttons or change window size to test responsiveness'
        },

        rangeSelector: {
            selected: 1
        },

        series: [{
            name: 'AAPL Stock Price',
            data: [[1609459200000,4443.26],[1609459200000,50000.26]],
            type: 'area',
            threshold: null,
            tooltip: {
                valueDecimals: 2
            }
        }],

        responsive: {
            rules: [{
                condition: {
                    maxWidth: 500
                },
                chartOptions: {
                    chart: {
                        height: 300
                    },
                    subtitle: {
                        text: null
                    },
                    navigator: {
                        enabled: false
                    }
                }
            }]
        }
    });

    document.getElementById('small').addEventListener('click', () => {
        chart.setSize(400);
    });

    document.getElementById('large').addEventListener('click', () => {
        chart.setSize(800);
    });

    document.getElementById('auto').addEventListener('click', () => {
        chart.setSize(null);
    });
});