Highcharts Annotations Chart

In the previous chapters, we learned how to setup highcharts library and how to create a chart with required configurations using highcharts library in our webpage. Now, we will learn how to create a chart with annotations using highcharts library with examples.

Highcharts Annotations Chart Example

Following is the example of creating a chart with annotations by setting the annotations properties using highcharts library.

 

<html>

<head>

    <title>Highcharts Annotations Chart</title>

    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <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/annotations.js"></script>

    <script type="text/javascript">

        $(function() {

            var elevationData = [

              [0.0, 225], [0.1, 226], [0.2, 228], [0.3, 228], [0.4, 229], [0.5, 229],

              [0.6, 230], [0.7, 234], [0.8, 235], [0.9, 236], [1.0, 235], [1.1, 232],

              [1.2, 228], [1.3, 223], [1.4, 218], [1.5, 214], [1.6, 207], [1.7, 202],

              [1.8, 198], [1.9, 196], [2.0, 197], [2.1, 200], [2.2, 205], [2.3, 206],

              [2.4, 210], [2.5, 210], [2.6, 210], [2.7, 209], [2.8, 208], [2.9, 207],

              [3.0, 209], [3.1, 208], [3.2, 207], [3.3, 207], [3.4, 206], [3.5, 206],

              [3.6, 205], [3.7, 201], [3.8, 195], [3.9, 191], [4.0, 191], [4.1, 195],

              [4.2, 199], [4.3, 203], [4.4, 208], [4.5, 208], [4.6, 208], [4.7, 208],

              [4.8, 209], [4.9, 207], [5.0, 207], [5.1, 208], [5.2, 209], [5.3, 208],

              [5.4, 210], [5.5, 209], [5.6, 209], [5.7, 206], [5.8, 207], [5.9, 209],

              [6.0, 211], [6.1, 206], [6.2, 201], [6.3, 199], [6.4, 200], [6.5, 202],

              [6.6, 203], [6.7, 202], [6.8, 204], [6.9, 206], [7.0, 208], [7.1, 205],

              [7.2, 202], [7.3, 198], [7.4, 198], [7.5, 198], [7.6, 198], [7.7, 198],

              [7.8, 199], [7.9, 197], [8.0, 194], [8.1, 194], [8.2, 195], [8.3, 195],

              [8.4, 196], [8.5, 192], [8.6, 200], [8.7, 197], [8.8, 194], [8.9, 194],

              [9.0, 193], [9.1, 192], [9.2, 192], [9.3, 193], [9.4, 191], [9.5, 191],

            [9.6, 193], [9.7, 193], [9.8, 194], [9.9, 192], [10.0, 192], [10.1, 192],

            [10.2, 192], [10.3, 192], [10.4, 193], [10.5, 193], [10.6, 193], [10.7, 193],

            [10.8, 194], [10.9, 194], [11.0, 194], [11.1, 195], [11.2, 194], [11.3, 194],

            [11.4, 194], [11.5, 194], [11.6, 193], [11.7, 194], [11.8, 194], [11.9, 194],

            [12.0, 195], [12.1, 195], [12.2, 195], [12.3, 197], [12.4, 197], [12.5, 197],

            [12.6, 198], [12.7, 201], [12.8, 202], [12.9, 203], [13.0, 205], [13.1, 205],

            [13.2, 204], [13.3, 210], [13.4, 213], [13.5, 212], [13.6, 213]

             ];

            Highcharts.chart('container', {

                chart: {

                    type: 'area',

                    zoomType: 'x',

                    panning: true,

                    panKey: 'shift',

                    scrollablePlotArea: {

                        minWidth: 600

                    }

                },

                title: {

                    text: '2017 Tour de France Stage 8'

                },

                subtitle: {

                    text: 'Annotated chart in Highcharts'

                },

                annotations: [{

                    labelOptions: {

                        backgroundColor: 'rgba(255,255,255,0.5)',

                        verticalAlign: 'top',

                        y: 15

                    },

                    labels: [{

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 1.9,

                            y: 196

                        },

                        text: 'Arbois'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 4.8,

                            y: 209

                        },

                        text: 'Montrond'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 6.3,

                            y: 199

                        },

                        text: 'Mont-sur-Monnet'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 8.6,

                            y: 200

                        },

                        x: -10,

                        text: 'Bonlieu'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 9.4,

                            y: 191

                        },

                        text: 'Chassal'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 10.5,

                            y: 193

                        },

                        text: 'Saint-Claude'

                    }]

                }, {

                    labels: [{

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 11.3,

                            y: 194

                        },

                        x: -30,

                        text: 'Col de la Joux'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 12.2,

                            y: 195

                        },

                        text: 'Côte de Viry'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 13.3,

                            y: 210

                        },

                        text: 'Montée de la Combe<br>de Laisia Les Molunes'

                    }]

                }, {

                    labelOptions: {

                        shape: 'connector',

                        align: 'right',

                        justify: false,

                        crop: true,

                        style: {

                            fontSize: '0.8em',

                            textOutline: '1px white'

                        }

                    },

                    labels: [{

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 5.2,

                            y: 209

                        },

                        text: '6.1 km climb<br>4.6% on avg.'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 6.0,

                            y: 212

                        },

                        text: '7.6 km climb<br>5.2% on avg.'

                    }, {

                        point: {

                            xAxis: 0,

                            yAxis: 0,

                            x: 7.0,

                            y: 208

                        },

                        text: '11.7 km climb<br>6.4% on avg.'

                    }]

                }],

                xAxis: {

                    labels: {

                        format: '{value} km'

                    },

                    minRange: 5,

                    title: {

                        text: 'Distance'

                    }

                },

                yAxis: {

                    startOnTick: true,

                    endOnTick: false,

                    maxPadding: 0.35,

                    title: {

                        text: null

                    },

                    labels: {

                        format: '{value} m'

                    }

                },

                tooltip: {

                    headerFormat: 'Distance: {point.x:.1f} km<br>',

                    pointFormat: '{point.y} m a. s. l.',

                    shared: true

                },

                legend: {

                    enabled: false

                },

                series: [{

                    data: elevationData,

                    lineColor: Highcharts.getOptions().colors[1],

                    color: Highcharts.getOptions().colors[2],

                    fillOpacity: 0.5,

                    name: 'Elevation',

                    marker: {

                        enabled: false

                    },

                    threshold: null

                }]

            });

        });

    </script>

</head>

<body>

    <div id="container" style="width: 100%; height: 400px;">

    </div>

</body>

</html>

If you observe the above example, we added annotations properties to create a chart with annotations using highcharts library with required properties.

 

When we execute the above highcharts example, we will get the result like as shown below.

 

Highcharts Annotations Chart Example Result

 

This is how we can create the chart with annotations using highcharts library with required annotation properties.

Topics Covered