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

Including multiple series in one chart


While it is useful to display one data series, we may want to add more data to a chart. For example, we may want to compare two different sets of data over the same period of time.

In Highcharts, we can display additional data in a separate series array. The series arrays are just lists of data with a name. In Highcharts, this list is represented by a JavaScript array.

How to do it...

To get started, follow the ensuing instructions:

  1. Define options for our chart as in the previous recipe, as follows:

    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Including multiple series in one chart'
        },
        series: [{
            name: 'Bar #1',
            data: [1, 2, 3, 4]
        }]
    };
  2. Add a second series object as shown in the following code:

    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Including multiple series in one chart'
        },
        series: [{
            name: 'Bar #1',
            data: [1, 2, 3, 4]
        },  // Add a new series
        {
            name: 'Bar #2',
            data: [4, 3, 2, 1]
        }]
    };
  3. Finally, render the chart using the highcharts function $('#container').highcharts(options)". This output is shown in the following screenshot:

There's more...

If we want to add another series to the chart after it has been rendered, you can use the addSeries method and pass it in the series object. We can get a reference to the chart in one of the following two ways:

  • Create the chart, then call .highcharts() with the appropriate jQuery selector, as shown in the following code:

    $('#container').highcharts(options);
    var chart = $('#container').highcharts();
  • When creating the chart, chain together a call to .highcharts()as follows:

    var chart = $('#container').highcharts(options).highcharts();

    Using the chaining method, we can add a series as follows:

        var chart = $('#container').highcharts(options).highcharts();
        chart.addSeries({
            name: 'Series 2',
            data: [4,3,2,1]
        });

The addSeries method also has a few other arguments that can be passed. The addSeries method also has optional second and third arguments that determine whether the chart should be redrawn (defaults to true) and how the new series should be animated (defaults to true, but we could supply an animation object that is best described in the documentation).