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

Displaying multiple charts in one graph


We are not limited to displaying a single series in a chart, and likewise, we are not limited to displaying a single type of chart within the same chart. In some circumstances, we may want to display the same data using different types of charts.

Earlier, we saw that a series object can have data associated with it, such as name. Similarly, a series object can also have a type, which changes how the data is displayed in the rendered chart.

How to do it...

To get started, follow the ensuing instructions:

  1. Define our chart options as we did in the previous recipe, as shown in the following code:

    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Displaying multiple charts in one graph'
        },
        series: [{
            name: 'Bar #1',
            data: [1, 2, 3, 4]
        }, {
            name: 'Bar #2',
            data: [4, 3, 2, 1]
        }]
  2. Add a new series to our chart with the type pie using the following code:

    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Displaying multiple charts in one graph'
        },
        series: [{
            name: 'Bar #1',
            data: [1, 2, 3, 4]
        }, {
            name: 'Bar #2',
            data: [4, 3, 2, 1]
        }, { // add new series
            type: 'pie',
            data: [1,2,3,4],
            center: [0,0]
        }]
    }

    The following is the rendered chart:

How it works...

By default, Highcharts will use the chart.type string to determine how the different series should be displayed. However, if a series has its own type provided, it will use that type when it is rendered in the chart.

There's more...

Just changing the type string of the series will probably result in something ugly or otherwise undesired, especially in the case of a pie chart where it will render on top of the existing chart. Fortunately, it is possible to adjust the positioning and style of a pie series by providing a center option.

If we wanted to enable the labels on the pie chart, we could set dataLabels.enabled to true, as shown in the following code:

    var options = {
        // ...
        series: [{
            type: 'pie',
            name: 'Bar #1',
            data: [1,2,3,4],
            dataLabels: {
                enabled: true
            }
        }]
    };

In fact, a series object can have any options that you would normally set inside plotOptions.<chartType>. For more details, visit http://api.highcharts.com/highcharts#plotOptions.