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

Adding extra content to tooltips


We've already seen that tooltips can add useful behavior to our charts; however, we are not merely limited to changing colors or text in the tooltip. It is possible to access more data than just what Highcharts provides.

How to do it...

To get started, follow the ensuing instructions:

  1. Create or make available a set of additional data that we would like to use in our tooltips as follows:

    var altName = ["apple", "banana", "pear"];
  2. Define options for our chart, including our series and its additional data, as shown in the following code:

    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Adding extra content to tooltips'
        },
        series: [{
            data: [{
                'x': 1,
                'y': 4,
                'category': 'Apple'
            }, {
                'x': 2,
                'y': 3,
                'category': 'Pear'
            }, {
                'x': 3,
                'y': 2,
                'category': 'Banana'
            }]
        }]
    };
  3. Access our desired fields in the formatter function via this.point.options as shown in the following code:

    var options = {
        // ...
        tooltip: {
            formatter: function() {
                return 'We have ' + this.y + ' ' + this.point.options.category + 's'
            }
        }
    }

    The following is the output chart:

How it works...

Highcharts supports multiple data formats. Earlier, we were using its most basic format—an array of numeric data. However, as in this example, we have seen that the data array can be more complex. A data series can support two other formats—an array of objects with named values (as we used previously) and an array of the [x,y] coordinates, as you can see in the following code:

var options = {
    // ...
    series: {
        data: [
            [1,1],
            [2,2],
            [3,3]
        ]
    }
}

When we specify data as an array of objects, we can access the information about the individual points via this.point.options. Since the formatter function is just a JavaScript function, we can do whatever we might normally do inside a JavaScript function, such as displaying our additional information.