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 a new theme


When working on charts, we may find that there is a set of colors that works well or that there are settings that we may want to use in other charts. This is where themes are helpful. Themes are just a collection of common options that we can apply to all charts.

How to do it...

To get started, follow the ensuing instructions:

  1. Create an empty options object using the following code:

    var myTheme = {};
  2. Assign the properties we want in the theme, such as colors or a background color as follows:

    myTheme.colors = ["#000000", "#ff0000", "#00ff00", "#0000ff"]
    myTheme.chart = {
        backgroundColor: '#cccccc'
    };
    myTheme.title = {
        style: {
            fontSize: '20px',
            fontFamily: '"Georgia", "Verdana", sans-serif',
            fontWeight: 'bold',
            color: '#000000'
        }
    }

    Note

    When all colors have been used, Highcharts will pull new colors from the beginning of the array.

  3. Call Highcharts.setOptions to apply our theme to all charts using the following code:

    Highcharts.setOptions(myTheme);

    The following is the output chart:

How it works...

The Highcharts.setOptions function, as previously discussed, sets options globally; a theme is just a set of options that we want applied to all charts.

If we want to store the theme in a separate file, we only need to make a few small changes. First, we will create our theme in a new file. In this file, we will create our theme in the Highcharts namespace, include our theme file after highcharts.js on our main page, and call Highcharts.setOptions, as shown in the following code:

    // myTheme.js
    Highcharts.myTheme = {
        // Our theme goes here
    };
    
    // main page
    <script type='text/javascript' src='highcharts.js'></script>
    <script type='text/javascript' src='myTheme.js'></script>
    
    <script type='text/javascript'>
        // ... chart creation ...
        
        Highcharts.setOptions(Highcharts.myTheme);
        
        // ... chart rendering …
    </script>

There's more...

Highcharts provides a few basic themes out of the box. This includes grid, skies, gray, dark blue, and dark green, in addition to the default colors. They can be found in the themes folder and are included just as you would include any other theme. More details on theming can be found at http://highcharts.com/docs/chart-design-and-style/themes.