Book Image

Highcharts Cookbook

By : Nicholas Terwoord
Book Image

Highcharts Cookbook

By: Nicholas Terwoord

Overview of this book

<p>Highcharts is a JavaScript library that enables web developers to create a wide range of different, highly customized charts. Highcharts easily integrates with existing JavaScript frameworks and is simple enough to make a column chart in a few lines of code, but flexible enough to handle more complex charting scenarios such as viewing multiple chart types with different data sources on a multitude of devices and form-factors.</p> <p>"Highcharts Cookbook" is a practical guide that provides you with clear, step-by-step recipes to create dynamic, functional charts in your web applications using Highcharts. With "Highcharts Cookbook", you will create and design dynamic and versatile charts in different scenarios.</p> <p>"Highcharts Cookbook" through its wide array of recipes will walk you through everything you need to know about Highcharts and will enable you to unleash its full potential in your web applications quickly and easily.</p> <p>You will learn how to integrate Highcharts with different frontend and backend libraries such as ExtJS, jQuery, and the Yii framework with some examples in Python, PHP, and NodeJS. You will also cover how to handle user interactions like form input and mouse events, how to fetch remote data in CSV, XML, and JSON format, and how to render charts for offline usage. If you want to learn the different ways you can leverage the power of Highcharts to create, integrate and extend its features in your application, then this book is for you.</p>
Table of Contents (19 chapters)
Highcharts Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating your first chart


To create and render a chart, we'll need to create a Highcharts.Chart instance and provide it with some options.

Getting ready

There are a few things that we need to do before we get started:

  1. Install bower (http://bower.io), a package manager for JavaScript.

  2. Create a bower.json file that lists information about our project, most importantly, its dependencies, as shown in the following code:

    {
      "name": "my-project",
      "dependencies": {
        "highcharts": "~3.0",
        "jquery": "^1.9"
      }
    }

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  3. From the same folder, run bower install to install our dependencies.

  4. Create a simple HTML page where we will create our chart, as shown in the following code:

    <html>
        <head>
            <style type='text/css'>
                #container {
                    width: 300px;
                    height: 300px;
                    border: 1px solid #000;
                    padding: 20px;
                    margin: 10px;
                }
            </style>
        </head>
        <body>
            <div id='container'></div>
    
    
            <script src='./bower_components/jquery/jquery.js'></script>
            <script src='./bower_components/highcharts/highcharts-all.js'></script>
    
            <script type='text/javascript'>
                $(document).ready(function() {
                    // our code will go here
                });
            </script>
        </body>
    </html>

    Note

    In our examples, we will be using jQuery, but there are plugins and adapters for many different toolkits and frameworks.

How to do it...

To get started, follow the ensuing instructions:

  1. First, we create an options object that will define what our chart looks like, as shown in the following code:

    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Creating your first chart'
        },
        series: [{
            name: 'Bar #1'
            data: [1, 2, 3, 4]
        }]
    }

    Note

    It is possible to create a chart with an empty set of options (that is, options = {}) but this generates a very bland chart.

  2. Next, we render our new chart by calling the .highcharts jQuery function on some element on the page. In this case, we select an element on the page with an id value equal to container, as shown in the following code:

        var options = {
            chart: {
                type: 'bar'
            },
            title: {
                text: 'Creating your first chart'
            },
            series: [{
                name: 'Bar #1',
                data: [1,2,3,4]
            }]
        };
        
        $('#container').highcharts(options);
    

    The following is the rendered chart:

How it works...

The .highcharts function is actually a part of a jQuery plugin used to create the Highcharts.Chart objects. It uses jQuery's element selector (for example, $('#container')) to find the element we want to render the chart to and renders the chart inside that element. Even if we supply a more general selector (for example, $('div')), it will only render the first element.

There's more...

As previously mentioned, it is not necessary to use jQuery to render a chart. We can create a chart instance manually using the chart.renderTo option and the Highcharts.Chart constructor. Using this method, we can either pass in the ID of an element or a reference to an element, as shown in the following code:

    // Using an element id
    var options = {
        chart: {
            renderTo: 'container'
        },
         // ...
    }

    var chart = new Highcharts.Chart(options);
    
    // Using an element reference
    var otherOptions = {
        chart: {
            renderTo: document.getElementById('container');
        },
         // ...
    }

    var otherChart = new Highcharts.Chart(options);