5

New to highcharts, but a simple question:

I need to mix several (say 6 or 8) small Highcharts gauges and charts on a single browser window. Is the only reliable way of doing this to create a separate div for each one and to target a separate Highcharts instance at each div?

And as a supplementary: Even if there is an alternative approach, is this multi-div approach as good as any, or are there pros and cons?

johnd
  • 87
  • 2
  • 2
  • 3

4 Answers4

4

Here is how I do multiple Highcharts on the same page.

http://jsfiddle.net/no1uknow/Jb2cb/2/

Keep in mind that you'll want to use jQuery 1.9.1 and Highcharts JS 3.0.x+, if you want to support most all browsers including IE8. This jsfiddle demo will work cross browser.

There are many examples using "var chart" singular for every chart, but I like making a var container_chartname for each div container. This technique gives me a lot more control to be interactive with each chart on the fly. Like resize, update data, etc.

If you plan on using forms to build charts dynamically then you can also use "just" the javascript code in the above jsfiddle example as the json callback. This will load all the divs each time you hit submit on a form dynamically.

Stackoverflow requires me to post code here, but see jsfiddle for the full piece of code...

And of course this is just my technique, there are many others...

        var container_chartCorrectiveAction = new Highcharts.Chart({
        chart: {
                renderTo: 'container_chartCorrectiveAction',

                        type: 'bar',
                        height: 195

                    },
                    title: {
                        text: 'Corrective Action'
                    },
                    subtitle: {
                        text: 'Sub-ATA () / ATA (20)'
                    },
                    xAxis: {
                        categories: ['No Defects Found-Fastener-Loose / Displaced'],
                        title: {
                            text: 'Action'
                        },
                        labels: {
                            style: {
                                width: '12000px'
                            }
                        }
                    },
                    yAxis: {
                        min: 0,
                        title: {
                            text: 'Count',
                            align: 'high'
                        },
                        labels: {
                            overflow: 'justify'
                        }
                    },
                    tooltip: {
                        formatter: function() {
                            return ''+ this.series.name +': '+ this.y +' Count';
                        }
                    },
                    plotOptions: {
                        bar: {
                            dataLabels: {
                                enabled: true
                            }
                        },
                        series: {
                            pointWidth:10,
                            groupPadding: .05,
                            shadow: true
                        }
                    },
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom',
                        floating: false,
                        borderWidth: 1,
                        backgroundColor: '#FFFFFF',
                        shadow: true,
                        labelFormatter: function() {
                            return '<div class="' + this.name + '-arrow"></div><span style="font-family: \'Advent Pro\', sans-serif; font-size:12px">' + this.name +'</span><br/><span style="font-size:10px; color:#ababaa">   Total: ' + this.options.total + '</span>';
                        }
                    },
                    credits: {
                        enabled: false
                    },
                    exporting: { 
                        enabled: true 
                    },
                    series: [{
                name: 'Heavy',
                total: '0',
                data: [null]
                },{
                name: 'Intermediate',
                total: '0',
                data: [null]
                },{
                name: 'Line',
                total: '0',
                data: [null]
                },{
                name: 'Lite',
                total: '1',
                data: [1]
                }]
                });


        var container_chartAtaFleetAvg = new Highcharts.Chart({
        chart: {
                renderTo: 'container_chartAtaFleetAvg',

                        type: 'bar',
                        height: 185

                    },
                    title: {
                        text: 'Fleet Average'
                    },
                    subtitle: {
                        text: 'ATA (20)'
                    },
                    xAxis: {
                        categories: ['Fleet Average'],
                        title: {
                            text: ''
                        },
                        labels: {
                            style: {
                                width: '12000px'
                            }
                        }
                    },
                    yAxis: {
                        min: 0,
                        title: {
                            text: 'Average',
                            align: 'high'
                        },
                        labels: {
                            overflow: 'justify'
                        }
                    },
                    tooltip: {
                        formatter: function() {
                            return ''+ this.series.name +': '+ this.y +' Average';
                        }
                    },
                    plotOptions: {
                        bar: {
                            dataLabels: {
                                enabled: true
                            }
                        },
                        series: {
                            pointWidth:10,
                            groupPadding: .05,
                            shadow: true
                        }
                    },
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom',
                        floating: false,
                        borderWidth: 1,
                        backgroundColor: '#FFFFFF',
                        shadow: true,
                        labelFormatter: function() {
                            return '<div class="' + this.name + '-arrow"></div><span style="font-family: \'Advent Pro\', sans-serif; font-size:12px">' + this.name +'</span><br/><span style="font-size:10px; color:#ababaa">   Total: ' + this.options.total + '</span>';
                        }
                    },
                    credits: {
                        enabled: false
                    },
                    exporting: { 
                        enabled: true 
                    },
                    series: [{
                name: 'Intermediate',
                total: '0.35',
                data: [0.35]
                },{
                name: 'Lite',
                total: '0.3',
                data: [0.30]
                },{
                name: 'Heavy',
                total: '0.1',
                data: [0.10]
                }]
                });
no1uknow
  • 549
  • 1
  • 7
  • 18
2

You can also use many series in one div. http://jsfiddle.net/sbochan/wzprF/

Sebastian Bochan
  • 37,348
  • 3
  • 49
  • 75
  • 1
    Of course. It depends on what the series represent. For totally different datas, I would suggest the multi-div based method, who is more semantically correct. But, for linked datas, the multi-series method would be better. – Pierre Fourgeaud Jul 15 '13 at 11:52
  • Excatly, but it is the additional solution for problem, so maybe will be helpful for someone. – Sebastian Bochan Jul 15 '13 at 11:53
  • Sure ! I was just pointing the different use for both methods ;) – Pierre Fourgeaud Jul 15 '13 at 11:54
  • 1
    Thanks again, Sebastien & Pierre. But if I've understood correctly, the multiple gauges might need to be reasonably similar to one another to make it worth doing as multiple series? Otherwise, I can imagine that the code gets quite confusing as you try to define two or more fairly different gauges within the one overall chart definition? (And perhaps doesn't end up saving much duplicated coding?) Perhaps keeping the individual gauge code separate helps maintenance too? Would you agree? – johnd Jul 15 '13 at 13:08
  • @johnd It depends on datas. Semantically datas who represents similar things should be in the same graph with multiple series but if you have *the evolution of the CAC 40* on one side and *the number of users on your website* on the other side (for example), you should use the multi-divs method who seem more appropriate for this case :) – Pierre Fourgeaud Jul 15 '13 at 13:18
0

It seems that the only way to do this is to have a container by chart.

In my own opinion, I don't think a multi-div approach is a bad idea. Just be careful about the id you will give to the div.

But you can create a clean code, on the javascript side. When you create many charts (6 or 8 is a lot...), you have to avoid code duplication... (At least to limit duplication)

There is a good way to do this with jQuery.extend() and Highcharts.setOptions : Manage multiple highchart charts in a single webpage

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • Many thanks. To clarify a little: I agree that 6 or 8 large, fully detailed charts would be too much for a single window. However, HC can obviously do gauges too and a 300px diameter gauge is a relatively large one - you can do a decent gauge in a 300 x 400px div. So mixing say 4 or 5 such gauges and maybe a couple of medium-sized, well-designed charts ought to be perfectly feasible for a dashboard-type project – johnd Jul 15 '13 at 06:38
  • @user2581569 For a dashboard-type project, the multi-div approach is not bad. And I already saw admin dashboards with 6-to-9 charts ;) Just like I said, I suggest you to limit duplicates. – Pierre Fourgeaud Jul 15 '13 at 08:11
-3

Make sure your div containers are formatted as this

<div ... ></div>

and not

<div ... />

as it may not render properly.

Agustín
  • 1,569
  • 1
  • 14
  • 9