Highstocks graph width not correctly rendered
Asked Answered
P

5

8

Hello I have a problem with highstocks when using jquery tabs.

this is the code for the constructor.

Chart = new Highcharts.StockChart({  
        Chart = new Highcharts.StockChart({
        chart: {
            renderTo: 
                'Container',
                 alignticks:false
             },
        xAxis: { .......... },
        yAxis: [{ ....... }],
        series : [{ .......... }]
    });

The Container has only half the width of the whole page.

When the page is loaded to the tab containing the graph, then its width is rendered correctly. But when the page is loaded first to another tab, then its width spans the whole width of the page, overlapping with other things on the page. So the page has to be refreshed in order to fix this.

Does anyone have a solution to this?

Pily answered 5/12, 2011 at 9:40 Comment(2)
Please use jsfiddle.net to demonstrate your problem. You explanation is alright but you are missing the relevant code.Bandurria
There is chart.reflow() method.Audraaudras
D
21

You could also trigger the window.resize Event

$(window).trigger('resize');

And Highcharts should update the Chart size

Derouen answered 4/11, 2013 at 13:20 Comment(2)
Added this to the end of the tab .click function and it worked like a charm.Ere
Despite newer versions having the .reflow() function, this works for older versionsMayne
D
10

Highcharts sets the width of the chart to the width of the containing element. So if the containing element is hidden, it can't get a width.

To fix this you can set the width option when initializing your chart:

Chart = new Highcharts.StockChart({  
    Chart = new Highcharts.StockChart({
    chart: {
          renderTo: ...,
          width: <SOME WIDTH>
        },
    xAxis: { .......... },
    yAxis: [{ ....... }],
    series : [{ .......... }]
});

If you aren't able to set the chart to a proper width I've used this trick to be able to get the width of a hidden element:

jQuery: Get height of hidden element in jQuery

Defense answered 6/12, 2011 at 16:58 Comment(0)
H
6

I know this is a long while after you asked your question but I had a similar issue in that my charts were rendering on the client with zero width whenever the chart was on a tab that was not activated by default. This is because, as @brent indicated, highcharts uses the parent's width and, as of jQuery UI 1.9, the tab widget uses an inline style of display: none to hide the inactive tabs.

Instead of using the document ready event of jQuery to initialize your highchart, you should instead use the activate event of your the tab your chart is on. This has the added benefit of doing the snazzy chart animation each time the user clicks the tab.

Instead of:

$(document).ready(function() { 
    var chart1 = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [{
            name: 'Jane',
            data: [1, 0, 4]
        }, {
            name: 'John',
            data: [5, 7, 3]
        }]
    });
});​

Use:

$('#tabcontainer').on('tabsactivate', function (event, ui) { 
    var chart1 = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [{
            name: 'Jane',
            data: [1, 0, 4]
        }, {
            name: 'John',
            data: [5, 7, 3]
        }]
    });
});​
Houseclean answered 6/2, 2013 at 4:8 Comment(0)
L
3

You can set the width of the graph's container div with a percentage.

Something like this should work.

<div id = "tab1"> 
  <div id="graphContainer1" style= "width: 90%"></div>
</div>
<div id = "tab2" style = "display:none"> 
  <div id="graphContainer2" style= "width: 60%"></div>
</div>
Lasser answered 29/10, 2012 at 8:25 Comment(0)
S
1

In your style sheet replace the rule for the class selector .ui-tabs .ui-tabs-hide with

.ui-tabs .ui-tabs-hide {
    position: absolute;
    left: -10000px;
}

Solution taken from: jQuery UI - Tabs

Shortstop answered 26/12, 2011 at 17:31 Comment(2)
Thanks! Although I use Bootstrap's tabs, a modified version of this trick worked perfectly. This is what I came up with (I'm sorry for the lack of formatting): .tab-content > .tab-pane, .pill-content > .pill-pane { display: block; position: absolute; left: -10000px; } .tab-content > .active, .pill-content > .active { position: static; left: auto; }Augend
@piotr.d, I just tried but preferred to set height to 0 for inactive tabs, see https://mcmap.net/q/328824/-why-are-bootstrap-tabs-displaying-tab-pane-divs-with-incorrect-widths-when-using-highchartsDodgem

© 2022 - 2024 — McMap. All rights reserved.