score:159
The Cause
This is not a Highcharts issue, at least not by itself, the problem is caused by the fact that Bootstrap uses display: none
to hide inactive tabs:
.tab-content > .tab-pane, .pill-content > .pill-pane {
display: none; /* this is the problem */
}
This causes Highcharts not able to get the expected width to initialize the chart, thus defaults to 600px
. This would happen to other tools using the same approach to hide content.
A Pure CSS Solution
Instead of working around the issue with an extra redraw or delayed initialization, we can achieve the hidden effect using height: 0; overflow-y: hidden
, this way the hidden tab panes are still in place and having valid width
:
/* bootstrap hack: fix content width inside hidden tabs */
.tab-content > .tab-pane:not(.active),
.pill-content > .pill-pane:not(.active) {
display: block;
height: 0;
overflow-y: hidden;
}
/* bootstrap hack end */
Update: We now target inactive tabs directly, via :not(.active)
, to avoid possible side effect if any.
This solution is seamless, so you no longer have to worry whether the chart is inside a tab-pane or not. And it's efficient as it fixes the width issue in the first place, thus there's no need to workaround the width issue afterward via resize/redraw/reflow using javascript.
Note about cascading order
This patch needs to be loaded after bootstrap css, because of CSS cascading order rules, in short, the latter rule wins.
Demo
Tip: switch to the Profile tab to see the difference.
score:-1
The in-line style "width" is different for the charts. I'm not sure when and how are those calculated, but you can override those in css for example.
score:2
I found an easier solution in another post. Basically, the problem comes because Highcharts cannot render on a div with 'display: none' set. The solution is just write a fix in your CSS stylesheet.
.tab-content > .tab-pane,
.pill-content > .pill-pane {
display: inline;
visibility: hidden;
position: absolute;
width: 930px;
}
.tab-content > .active,
.pill-content > .active {
display: inline;
visibility: visible;
position: absolute;
width: 930px;
}
This is going to make the tabs to work with visibility
attribute instead of display
. Charts will render well.
score:5
Based on the Pawel's answer I render the chart at beginning and then resize the chart.
The code used (with bootstrap 3.0.3 and HighCharts 3.0.7)
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
$('#container').highcharts().setSize($('#container').width(),$('#container').height());
})
score:5
Based on Paweł's answer I've made a working example:
http://codepen.io/colinsteinmann/pen/BlGfE
Basically the .reflow() function is called on each chart when a tab is clicked. That way the chart gets redrawn based on the new dimensions which are applied to the container when it becomes visible.
This is the most important snippet of code:
// fix dimensions of chart that was in a hidden element
jQuery(document).on( 'shown.bs.tab', 'a[data-toggle="tab"]', function (e) { // on tab selection event
jQuery( ".contains-chart" ).each(function() { // target each element with the .contains-chart class
var chart = jQuery(this).highcharts(); // target the chart itself
chart.reflow() // reflow that chart
});
})
score:30
The problem is that Highcharts can not calculate width of the container which has CSS: display:none
, so applied is default width (600px). In fact, browser is not able to calculate width for such elements.
For example when you will use separator to resize Result window, chart will resize and will work. So you have three options:
- render chart after first click on that tab
- render chart at the beginning, but after each click and window resize use
chart.setSize(w,h)
with new width and height - render chart at the beginning, but after tab click call
chart.reflow()
Source: stackoverflow.com
Related Query
- Why are Bootstrap tabs displaying tab-pane divs with incorrect widths when using highcharts?
- highcharts graphs in bootstrap tabs are not displaying properly
- Highcharts Boxplot - box with lower, upper quartile and median is not displaying when min and max are null of a category
- Using the source version instead of mimified when import lib with reactjs and npm
- Why .html() doesn't work with SVG selectors using jquery ?
- bootstrap data-toggle="tab" - how to make a tab active with a JS call
- Using Highcharts and displaying a message over or on the chart when there is no data
- Highcharts - Issue with negative values when displaying multiple axes
- Using highcharts with rails not displaying plotLines
- when printing page with highcharts using javascript this.print() width values in css in @media print seem to be ignored
- Receiving incorrect datetimes when using milliseconds for x-axis?
- Why do I have the issue 'property assigment expected' when I want to display a chart on my web page using highcharts
- Using Bootstrap Datepicker with Highcharts
- Jquery Highcharts is not loading when using with common function?
- How can I hide non-selected series and axes when using XY zoom with highcharts?
- Highcharts graph width is incorrect when scrollbar is present, in bootstrap fluid span
- Strange behavior with highcharts when using "column" type and multiple datetime series
- How to specify a range of data when using HighCharts with <table>?
- Why Markers points are not aligned with yAxis in Highcharts?
- Have an issue with JavaScript, AJAX code displaying data
- Highchart not displaying when used with drop down
- Why do I get 'Cannot read property 'isValid' of undefined' when using 400 data points?
- Why doesn't HighCharts render when I pull it in with Ajax?
- Displaying date on the x axis with Highcharts using ASP.NET MVC
- Why does my Highchart look different when I transfer my code from Jsfiddle to my server?
- Highcharts: Column and Bar Chart labels are incorrect when I drilldown
- highchart's line chart with constant series not plotting when using linear gradient colour
- Highcharts - Column labels with different widths are not aligned
- Using Highmaps mapbbuble and datalabels. Datalabels are not going out of screen when zoomed
- Highcharts column chart returning incorrect series index on click when using shared tooltip
More Query from same tag
- Adding vertical line on xAxis in HighStock
- convert string into array for highcharts js
- How to make color fill area between 2 points at HighCharts
- highchart display lines with the same color
- Customize Highcharts desc tag
- Hover on areas not on point in Highchart-polygon
- Highcharts: change graph when radio button selected
- HighCharts: Custom button next to legend inside chart area
- Stacked bar chart from JSON file - seems to be ignoring a value
- Highcharts tooltips with embedded sparklines
- Snap to month in highchart stock navigator
- Highcharts drilldown datalabels different from parent
- Group specific points to categories in Highchart
- How to show javascript id data into piechart (Highchart.js)?
- How to show bar of graph in different colour when it reaches threshold level in high charts in JS?
- Switching between many Highcharts using buttons or link text
- Highstock columns overlapping one another even if there is space
- Serializing a function as a parameter in json using C#
- How to set 'lang' property in Highcharts using Angular JS?
- Highcharts Activity Gauge - how to get different behaviors while hover over
- Spline rendered behind column chart
- Center a chart inside of bootstrap's carousel
- react-highcharts-official lazy loading data
- highcharter xaxis crosshair tooltip labeling all series datapoints
- How can I export my chart output to JPEG or png format?
- Remove label from chart when exporting
- Add Source to Highcharts Export CSV
- Highchart duplicates data on chart instead of refreshing it
- How to embed text on the bar of the highchart
- refactor javascript to an external file