Skip to content

Instantly share code, notes, and snippets.

@sdaityari
Forked from kawadhiya21/chart.md
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdaityari/6eb38a0f96be9541794e to your computer and use it in GitHub Desktop.
Save sdaityari/6eb38a0f96be9541794e to your computer and use it in GitHub Desktop.

Using Highcharts to make charts

Charts are used to represent mass data with the help of visual aids. Initially, they were created manually, using calculators to do the math. Then came softwares like MS Excel and charts became an inseparable part of Powerpoint presentations during 1990s.

In this decade, with the internet boom, browsers have taken over everything. The strengthening CSS3 boosted the math-loving-techies to develop libraries for charts so that one need not switch to heavy software by automating the system of creation of charts using dynamic data from database and visualize it on the browser. The biggest advantage of this was to bring down the requirements to a bare minimum- one just needed a computer connected to the internet and a web browser to view your creations.

Long story short, we have tons of libraries now. Some very advanced libraries like D3.js and some very simple libraries like Chart.js. However, Highcharts is a combination of both. It is extremely easy to use, thanks to the documentation and fulfills modern requirements.

Requirements

Let us start the tutorial by downloading the required files of Highcharts (currently v4.0.1) from its download page. Extract it, and move the js/highcharts.js file to the root folder (which would be assumed in the code used in the tutorial). Its only dependency is jQuery. But, it also comes with a standalone version, which I prefer to include locally. A CDN link for highcharts may also be used.

Getting Started

We will include 3 JavaScript files which are jQuery, highcharts and the script to use highcharts framework.

As usual we start with index.html and include the 3 javascript files (in the same sequence).

<html>
	<head>
		<title>High Charts Examples</title>
	</head>
	<body>
		<script type="text/javascript" src="jquery.js"></script>
		<script type="text/javascript" src="highcharts.js"></script>
		<script type="text/javascript" src="script.js"></script>
	</body>
</html>

Introduction to Highcharts and some of its basic features.

Before we proceed, let us understand some basic definitions as given in the documentation.

  1. Titles and Subtitle - The name of the chart is the title and label names to X and Y axis are subtitles.
  2. Axes - These are X and Y axis except they are absent in pie charts.
  3. Series - It is the data supplied to the charts.
  4. Tooltip - The ability to mark certain points and naming them is called tooltips. The concept is similar to a jQuery UI tooltip.

There are few other terms like Navigators, Scroll Bars and Zooming, which are optional and are used if requirement arises. All our divisions are of 400px in width, hence, we define that in our CSS.

<style type="text/css">
	div{
		width:400px;
	}
</style>

Line Chart

We start with a simple line chart. We will use real data. Its the rainfall pattern across various states of India sorted monthwise. The link of data is here. We create a div in the index.html with id='line_chart'. We will reference this in script.js and initiate the charts.

<h3>Line Chart</h3>
<div id='line_chart'></div>
$(function () {
    $('#line_chart').highcharts({
        title: {
        	text: 'Rainfall Pattern of Andaman Nicobar in 2010'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            title:{
                text:'Months'
            }
        },
        yAxis:{
            title:{
                text:'Rainfall in mm'
            }
        },
        series: [{
            data: [79.8,26,1.7,28.3,233,502,443,319,234.8,249.1,583.2,355.7],
            name: 'Rainfall Data'
        }]

    });
});

Line Chart

Let us include the data of another state as well just to compare between the two.

$(function () {
    $('#line_chart').highcharts({
        title: {
        	text: 'Rainfall Pattern in States (2010)'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            title:{
                text:'Months'
            }
        },
        yAxis:{
            title:{
                text:'Rainfall in mm'
            }
        },
        series: [{
            data: [79.8,26,1.7,28.3,233,502,443,319,234.8,249.1,583.2,355.7],
            name: 'Andaman and Nicobar'
        },
        {
            data: [72.8,66,4.5,28.3,123,502,43,59,234.8,29.1,583.2,35.7],
            name: 'Karnataka'
        }]

    });
});

combining Charts

Notice the tooltip in the following screenshot. It denotes the X and Y coordinate data at the data point.

tooltip

Spline Chart

A spline chart shows the data in a continuous manner with curves rather than sharp peaks. A spline works with an existing list of points, and draws a smooth curve through them. It is declared exactly like line chart but the chart.type variable is changed to spline.

// in index.html
<div id='spline_chart'></div>

Javscript portion is as follows. Notice the change.

    $('#spline_chart').highcharts({
        chart:{
            type: 'spline'
        },
        title: {
        	text: 'Rainfall Pattern in States (2010)'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            title:{
                text:'Months'
            }
        },
        yAxis:{
            title:{
                text:'Rainfall in mm'
            }
        },
        series: [{
            data: [79.8,26,1.7,28.3,233,502,443,319,234.8,249.1,583.2,355.7],
            name: 'Andaman and Nicobar'
        },
        {
            data: [72.8,66,4.5,28.3,123,502,43,59,234.8,29.1,583.2,35.7],
            name: 'Karnataka'
        }]

    });

Spline Chart

Bar and Pie Chart

Creating the bar or pie charts are easy. Just change the type to bar and pie respectively.

Bar Chart

Pie Chart

Getting data by AJAX

The number of use cases of generating static charts are not so much. We can therefore give the user the feature to build his own charts. Here's how to create charts using data received from an AJAX call.

    function create(){
        var options = {
            chart: {
                renderTo: 'report',
                type: 'column'
            },
            title: {
                text: 'Day Wise Rainfall Report'
            },
            xAxis: {
                title : {
                    text: 'Date'
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Rainfall'
                }
            },

            series: [{
            }]
        };
        
        $.ajax({
            
            url: '/controller/action',
            
            success: function(chartdata){
                
                options.series[0].name = "Rainfall";
                options.series[0].data = JSON.parse(chartdata)['rainfall'];
                options.xAxis.categories = JSON.parse(chartdata)['date'];
                var chart = new Highcharts.Chart(options);
                
            }
        });
    }

With this, we come to the end of this tutorial. I hope it was not too difficult to learn this library. Some more customization options can be seen in the docs. I bet one of the easiest and ready to learn docs with most gradual learning curve.

Why not fall in love with graphs?

The Github Repo contains the basic code used in this tutorial and here is a Demo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment