Quantcast
Channel: jquery – Crunchify
Viewing all articles
Browse latest Browse all 7

Dynamic Spline HighChart Example with Multiple Y Axis

$
0
0

Dynamic Spline HighChart Example with Multiple Y Axis values - Crunchify Tips

Highcharts is a charting library written in pure HTML5/JavaScript, offering intuitive, interactive charts to your website or web application. Highcharts currently supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types.

Recently I have to add another Y-Axis to Dynamic Spline Chart. With below simple script you can add 2nd y-axis very easily.

Demo

Demo with two diff Y-Axis Values

<script>
$(function () {
    $(document).ready(function() {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        var chart;
        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function() {

                        // set up the updating of the chart each second
                        var series = this.series[0];
                        var series2 = this.series[1];
                        setInterval(function() {
                            var x = (new Date()).getTime(), // current time
                                y = Math.random();
                                z = Math.random();
                            series.addPoint([x, y], false, true);
                            series2.addPoint([x, z], true, true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: [{
                title: {
                    text: 'Value1'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            {
                title: {
                    text: 'Value2'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            }],
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Random data',
                data: (function() {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = -19; i <= 0; i++) {
                        data.push({
                            x: time + i * 1000,
                            y: Math.random()
                        });
                    }
                    return data;
                })()
            },
                    {
                name: 'Random data',
                data: (function() {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = -19; i <= 0; i++) {
                        data.push({
                            x: time + i * 1000,
                            y: Math.random()
                        });
                    }
                    return data;
                })()
            }]
        });
    });

});
</script>

Another must read: JavaScript to Validate Email & Password Fields on Form Submit Event

Below is a sample HTML code:

<HTML>
<HEAD>
<TITLE>Crunchify - Dynamic Spline HighChart Example with
	Multiple Y Axis</TITLE>
<script type="text/javascript"
	src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script>
.....above script goes here....
</script>
</HEAD>
<BODY>
	<div id="container"
		style="min-width: 728px; height: 400px; margin: 0 auto"></div>
</BODY>
</HTML>

Have a suggestion or anything to add to this article? Chime in and share as a comment OR post it in forum.

The post Dynamic Spline HighChart Example with Multiple Y Axis appeared first on Crunchify.
Author: App Shah

Viewing all articles
Browse latest Browse all 7

Trending Articles