Highcharts Example With Spring Boot

Highcharts is one of the popular javascript library used to produce user-friendly charts for a given set of data. We can generate different types of graphical representations of the data with the help of the Highcharts library.

In this article, we will learn how to integrate the Highcharts javascript library into spring boot web application. We will create a spring boot web application, which will read the JSON data through AJAX call and display the bar chart on the HTML page.

Let us begin! 🙂

Table of Contents

Create a Spring boot application with required dependencies

Create a new Spring boot application with required dependencies. For this example, we need to add spring-boot-starter-web and spring-boot-starter-thymeleaf dependencies.

The pom.xml file with the required dependencies is given below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>

Configure the web application

To redirect the Spring web application to a default landing page make the following changes to the Spring boot application class.

In the below code, we are implementing the WebMvcConfigurer interface and overriding the addViewControllers method to set the default landing view to landig-page.

package com.asb.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class SpringBootHighchartsExampleApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootHighchartsExampleApplication.class, args);

    public void addViewControllers(ViewControllerRegistry registry) {

Create the REST endpoint

We need to create a RESTful endpoint to return the required data. For this, create a REST controller called HighChartController.java as shown below.

package com.asb.example.controller;

import java.util.Map;
import java.util.TreeMap;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class HighChartController {

    public ResponseEntity<Map<String, Integer>> getPieChart() {
        Map<String, Integer> graphData = new TreeMap<>();
        graphData.put("2016", 147);
        graphData.put("2017", 1256);
        graphData.put("2018", 3856);
        graphData.put("2019", 19807);
        return new ResponseEntity<>(graphData, HttpStatus.OK);

Here, we are returning a map containing blog statistics. The map keys are having calendar years and map values are having page view count. The RESTful endpoint is exposed on the path /get-data.

In this example, we are using static data by returning hard coded values. We can also return the data from a database by using spring data jpa and other ways(retrieving from other services, etc).

Create the HTML page

We have mapped the landing page by setting the view name to landing-page in the previous step. Create a HTML page called landing-page.html under /src/main/resources/templates/ directory. Add the following content.

<!DOCTYPE html>
<html xmlns="https://www.thymeleafe.org">
<meta charset="ISO-8859-1">
<title>High Chart Example - Spring Boot</title>
<link rel="stylesheet"
<link rel="stylesheet"
    href="https://code.highcharts.com/css/highcharts.css" />
<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>
    <div align="center">
        <h2>Spring Boot Highcharts Example</h2>
    <figure class="highcharts-figure">
        <div id="container-bar"></div>
    <script type="text/javascript">
                function() {
                        url : "/get-data",
                        success : function(result) {
                            var yearDtls = [];
                            var countDtls = [];
                                    function(key) {
                            drawChart(yearDtls, countDtls);
        function drawChart(year, viewCounts) {
            Highcharts.chart('container-bar', {
                chart : {
                    type : 'column',
                    styledMode : true
                title : {
                    text : 'Blog Page view count'
                xAxis : [ {
                    title : {
                        text : 'Year'
                    categories : year
                } ],
                yAxis : [ {
                    className : 'highcharts-color-0',
                    title : {
                        text : 'Page View Count'
                } ],
                series : [ {
                    data : viewCounts
                } ]

Here, we are adding the required javascript libraries and CSS files. We have added Bootstrap and Highchart CSS and js files. We have also added the JQuery javascript library to the header part of the HTML page.

The application loads the JSON data from the backend to show the bar graph through the JQuery AJAX call to the /get-data endpoint.

Once the application loads the data, we are plotting the chart by using Highcahrts.chart() method of the Highcharts library.

This method takes the HTML element id and chart configuration as the method parameter. We have passed container-bar as the HTML element id(Id of the <div> element used to plot the Highchart)

A few of the Highchart configurations used in this example are given below. We are plotting the blog statistics using a bar chart.

  • chart: Setting related to the chart.
  • title: To set the Highchart title.
  • xAxis: To set up the configuration related to the X-axis of the graph.
  • yAxis: To set up the configuration related to the Y-axis of the graph.
  • series: The list of values to use to plot the chart.

Running the application

Start the spring boot application. We should be able to get the bar chart as shown below.

Highchart example with spring boot


In this article, we learned how to integrate the Highcharts javascript library into the Spring boot web application. We have learned how to load the data into an HTML page using AJAX call and display the graphical representation of the data.

The sample code is available on GitHub.


  1. Hello , thank you very much for the post, but , I thing you’ve a mistkae in the path directory ” /src/main/resources/template/” template should be named “templates”

Comments are closed.