Book Image

Instant Highcharts

By : Cyril Grandjean
Book Image

Instant Highcharts

By: Cyril Grandjean

Overview of this book

Nowadays, a lot of systems and tools are developed using web technologies. Customers need to analyze their data using charts on their computer or their mobile device. With the Highcharts library, it is possible to create highly customizable and interactive charts on any computer or a mobile device without installing any plugins. Instant Highcharts is a practical, hands-on guide that provides you with a step-by-step approach, which will help you to take advantage of the powerful features of Highcharts. With Instant Highcharts, you will learn everything you need to know to create your own dynamic charts with your own data inside your web application. Instant Highcharts will present you with different features available with Highcharts, and helps you to create your own chart. You will start with Highcharts installation and then learn to create your own customized column, bar, area, and pie charts with your data. You will learn to add some interactive functionality such as the zoom feature or to export your chart into a printable format. We will also give you tips that will help you to improve your skills with Highcharts.
Table of Contents (7 chapters)

Quick start – setting up the main sections of Highcharts


A Highcharts' chart is composed of several parts, which could be mandatory or optional parts depending on the chart that you will create. In this section, we will show you the main elements that are composing a Highcharts' chart, and we will reuse the chart, previously created in order to illustrate these different core concepts.

Step 1 – setting the title and subtitle

For each Highcharts' chart, you can set a title and a subtitle. In the previous section, we have already declared the chart with the title Sales by city. We will now add a subtitle section with the text Statistics of 2012.

  1. Add the subtitle section and set the text to Statistics of 2012:

    subtitle: {
       text: 'Statistics of 2012'
    }
  2. You should have the expected result as shown in the following chart:

Step 2 – setting the credits

In the credits parts, you will be able to set the source or the copyright of your chart. By default, it is set to Highcharts.com. We will change this default value to Packt Publishing. The URL of the credit by default is http://www.highcharts.com, but you can change it by setting the href parameter with your own URL. We will change this value with http://www.packtpub.com.

  1. Add the credit section and set the text to Packt Publishing and set the href section with http://www.packtpub.com:

    credits: {
       text: 'Packt Publishing',
       href: 'http://www.packtpub.com'
    }
  2. You should have the expected result as shown in the following chart:

Step 3 – setting the series

The series section is a mandatory one of Highcharts, which you will have to include for every chart, that you will create. A series is simply a set of data that will be defined with a name and an array of data. We will now modify the previous series section by adding another section that will represent the sales of two different years for the three cities already defined:

  1. Rename the previous series with the name 2011 and add another series with the name 2012 and a new set of data:

    series: [{
           name: '2011',
           data: [1000, 2500, 1500]
       },
       {
           name: '2012',
           data: [1200, 2200, 1700]
    }]
  2. You should have the expected result as shown in the following chart:

Step 4 – setting the axes, plot lines, and plot bands

The axes sections will be set for some charts, such as line and column charts, but will not be included for charts, such as pie charts or donut charts. Axes are divided into two types: Y Axes and X Axes. Then, each axis is divided into four categories:

  • Linear Axis: This category of axis will use a linear scale like our Y Axis.

  • Logarithmic Axis: This category of axis will follow a logarithmic scale.

  • Category Axis: This axis will display different categories of data. Our X-Axis is a category axis.

  • Time Axis: You will have to use this category of axis when you want to represent your data along a time axis.

By default, the axis is calculated automatically, but you can set a minimum or maximum value if you set the 'min' and 'max' parameters of your axis. Then, you can add plot lines and plot bands for each of your axes. Plot lines and plot bands can be used when you want to display a limit or when you want to point out some parts of your axis.

For our chart, we will add a plot line, which will represent the best sales of the company and a plot band, which will represent the expected sales:

  1. For the plot line, we add our element inside the plotLines section of our Y Axis with a red color set in hexadecimal format with a width of 2 pixels and a value set to 2500.

    plotLines: [{
       color: '#FF0000',
       width: 2,
       value: 2500
    }]
  2. Then, for the plot band, we add our element inside the plotBands section of our Y Axis with a green color set in RGBA format from the values 1000 to 1500. The fourth parameter of our rgba function will allow us to have a bit of transparency. This parameter has to be set between zero (Transparent) and one (Opaque). We will also add a label with a text set to Expected Sales.

    plotBands: [{
       color: 'rgba(124,252,0, 0.3)',
       from: 1000,
       to: 1500,
       label: {
          text: 'Expected Sales'
       }
    }]
  3. For your yAxis, you should have the following code:

    yAxis: {
       title: {
          text: 'Sales'
       },
       plotLines: [{
          color: '#FF0000',
          width: 2,
          value: 2500
       }],
       plotBands: [{
           color: 'rgba(124,252,0, 0.3)',
           from: 1000,
           to: 1500,
           label: {
              text: 'Expected Sales'
           }
       }]
    }
  4. You should have the expected result as shown in the following chart:

Step 5 – setting the tool tip

The tool tip element appears when you are pointing your mouse over an element. Highcharts sets a default tool tip, but you can override it by setting a new template. In our example, we will modify the tool tip by adding a measurement unit, which will simply be Units in our case.

  1. Add the tooltip section and set the parameter valueSuffix with the value Units. This parameter defines the text that will be added at the end of the number.

    tooltip: {
       valueSuffix: 'Units'
    }
  2. You should have the expected result as shown in the following chart:

Step 6 – setting the legend

The chart legend can be displayed by default on some Highcharts' charts. Nevertheless, you can hide or customize the legend. In order to retrieve some space in our chart, we will change the legend position to the top right side of the chart.

  1. Add the legend section. Set the align parameter to the right and verticalAlign to the top, in order to move the legend to the top right side. Then, set the floating parameter to true. This parameter will be used in order to have the legend over our chart.

    legend: {
       align: 'right',
       verticalAlign: 'top',
       floating: true
    }
  2. You should have the expected result as shown in the following chart:

Step 7 – final code

The final code looks like:

$(function () { 
    var chart = $('#myFirstChartContainer').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Sales by city'
        },
        subtitle: {
            text: 'Statistics of 2012'
        },
        credits: {
            text: 'Packt Publishing',
            href: 'http://www.packtpub.com'
        },
        xAxis: {
            categories: ['London', 'Paris', 'Madrid']
        },
        yAxis: {
            title: {
                text: 'Sales'
            },
            plotLines: [{
                color: '#FF0000',
                width: 2,
                value: 2500
            }],
            plotBands: [{
                color: 'rgba(124,252,0, 0.3)',
                from: 1000,
                to: 1500,
                label: {
                    text: 'Expected Sales'
                }
            }]
        },
        tooltip: {
            valueSuffix: ' Units'
        },
        legend: {
            align: 'right',
            verticalAlign: 'top',
            floating: true
        },
        series: [{
            name: '2011',
            data: [1000, 2500, 1500]
        },
        {
            name: '2012',
            data: [1200, 2200, 1700]
        }]
    });
});

At the end of this section, you have created the main elements that comprise a chart, and you will now be able to reuse it to any charts that you would want to create.