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 reusable graphs


So far we have experimented with a lot of different graph options and configurations where themes defined a common set of styles; we may find a time where we have a very common type of graph that we want to create and we do not want to define the same options over and over again. We can avoid this tedium by creating reusable charts.

How to do it...

To get started, follow the ensuing instructions:

  1. Determine what type of a chart you want to make reusable. Suppose that we want to take our existing spiderweb chart.

  2. Create a new function spiderWebChart. This chart will take an options argument to let us configure the chart and return a Highcharts.Chart instance, as shown in the following code:

        var SpiderWebChart = function (options) {
            return Highcharts.Chart(options);
        };
  3. Define default values for the chart that will give it the correct appearance, as we did in the recipe Creating spiderweb graphs for comparison, using the following code:

        var SpiderWebChart = function (options) {
            // create options if they don't exist
            var modifiedOptions = options || {};
            
            // create a chart option if it does not exist
            modifiedOptions.chart = modifiedOptions.chart || {};
            modifiedOptions.chart.polar = true;
            
            // create an xAxis option if it does not exist
            modifiedOptions.xAxis = modifiedOptions.xAxis || {};
            modifiedOptions.xAxis.tickmarkPlacement = 'on';
            modifiedOptions.xAxis.lineWidth = 0;
    
            // create a yAxis option if it does not exist
            modifiedOptions.yAxis = modifiedOptions.xAxis || {};
            modifiedOptions.yAxis.gridLineInterpolation = 'polygon';
            modifiedOptions.yAxis.lineWidth = 0;
            
            return new Highcharts.Chart(modifiedOptions);
        };
  4. Create a spiderweb graph using the options from the previously mentioned recipe, using the following code:

        var chart = SpiderWebChart({
            chart: {
                renderTo: 'container'
            },
            title: {
                text: 'Creating spiderweb graphs for comparison'
            },
            series: [{
                name: 'Fighter',
                data: [10, 1, 5],
                pointPlacement: 'on'
            }, {
                name: 'Rogue',
                data: [5, 10, 1],
                pointPlacement: 'on'
            }]
        });

How it works...

We have created a wrapper function for common options. Instead of using jQuery, we use the renderTo option to find an element with container as its ID and render the chart within that element. As we only overwrite certain properties in our SpiderWebChart function, we can pass in as many other options as we like and only the ones relevant to the SpiderWebChart function will be affected.