问题
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?
回答1:
You could also trigger the window.resize Event
$(window).trigger('resize');
And Highcharts should update the Chart size
回答2:
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
Hope this helps.
回答3:
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]
}]
});
});
回答4:
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>
Hope this helps.
回答5:
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
来源:https://stackoverflow.com/questions/8383350/highstocks-graph-width-not-correctly-rendered