问题
I am trying since some days to make a pie chart from highcharts responsive. I am working on a medium size project and sometimes it is easy to lose the overview.
I already checked this : http://www.angulartutorial.net/2014/03/responsive-highchart.html but no success.
Problem: The highchart looks good when the width is 1920px. When it is 900px then the description of the pie (series -> data) is outside the browser and one can not read it, moreover, the pie is for me to small.
Question: How can I avoid this behaviour? I would like a bigger pie and be able to read the (series-> data).
I provide the following code:
My HTML code is:
<div id="container-independency" >
<div id="independency" >
<div>Title plot</div>
<div style="margin-left: 2.8%; margin-top:1%; font-size: 24px;">Bla blablabla blab bl<span class="autarkie" > </span> % blabla = <strong> <span class="autarkie" >
</span> % blablabla blablabla</strong></div>
<div id="highcharts_container"></div>
</div>
</div>
The CSS code:
#container-independency{
width: 90%;
max-width: 1620px;
background-color: #b8860b;
clear: both;
padding: 1%;
display: none;
box-sizing: border-box;
}
#independency{
width: 80%;
margin-left: auto;
margin-right: auto;
padding: 1%;
background-color: #ffb6c1;
box-sizing: border-box;
}
#highcharts_container{
width: 100%;
margin-left: auto;
margin-right: auto;
box-sizing: border-box;
}
Highcharts:
('#highcharts_container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title:{
text:''
},
credits: {
enabled: false
},
navigation: {
buttonOptions: {
enabled: false
}
},
tooltip: {
pointFormat: '<b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.2f} %',
style: {
color: '#58585a',
fontFamily: 'klavika-web, sans-serif', fontSize: '12px'
}
}
}
},
series: [{
name: '',
data: [
['Property1aaa/Property2aaa/Property3aaaaaa', independency],
['More blablabla blablabla', 100-independency],
]
}]
});//highcharts_container
Update:
回答1:
Each time a chart changes size redraw event of a chart is triggered. You can check width of a chart in that event and call additional update for series, because if you change your labels' text to one with <br>
tags, then pie seems to be fitting well. In case your issue is more complex solution will still be similar - check size and update chart.
Example with changed point names: http://jsfiddle.net/j86jkfvj/114/
Example with series update when width is < 900px: http://jsfiddle.net/dhwzw8qg/
回答2:
Here's an example I found of redrawing the pie based on a resize event of the page. I've used it and works well:
jsfiddle.net/4mo2qf79/12
HTML:
<div class="wrapper">
<div id="container" style="width:100%;"></div>
</div>
JS:
$(function () {
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Responsive Resize'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Firefox', 45.0],
['IE', 26.8],
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
}]
});
function redrawchart(){
var chart = $('#container').highcharts();
console.log('redraw');
var w = $('#container').closest(".wrapper").width()
// setsize will trigger the graph redraw
chart.setSize(
w,w * (3/4),false
);
}
$(window).resize(redrawchart);
redrawchart();
});
来源:https://stackoverflow.com/questions/32118711/how-to-make-a-pie-chart-from-highcharts-responsive