问题
I have been trying to implement the pie chart using Highcharts but am encountering an issue with the datalabels on very low resolutions being cropped.
I have attempted adding a windows.resize withing the formatter: function() but that failed.
Here is the code I am currently using:
// Radialize the colors
Highcharts.getOptions().colors = $.map(Highcharts.getOptions().colors, function(color) {
return {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
// Build the chart
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
backgroundColor: {
linearGradient: [0, 0, 500, 500],
stops: [
]
},
},
title: {
text: 'Header Here'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage}%</b>',
percentageDecimals: 0
},
plotOptions: {
pie: {
allowPointSelect: false,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +'%';
}
}
}
},
series: [{
type: 'pie',
name: 'Votes',
data: [
['Vote One', 50],
['Vote Two', 50],
['Vote three', 2]
]
}]
});
Is there anyway other than creating a new chart on resize that the labels can be set to false / hidden? and shown again above a certain resolution?
Many thanks
回答1:
You can set useHTML as true for datalabels and in formatter define your own divs. Then when you catch resize event use show / hide functons.
Simple example which show/hide datalabels after click button is available here:
http://jsfiddle.net/VYGEW/
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line'
},
title: {
text: 'Monthly Average Temperature'
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
useHTML: true,
formatter: function () {
return '<div class="datalabel">' + this.y + '</div>';
}
}
}
},
series: [{
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}]
}, function (chart) {
$('#btn').toggle(function () {
$('.datalabel').hide();
}, function () {
$('.datalabel').show();
});
});
来源:https://stackoverflow.com/questions/14751547/removing-highchart-datalabels-on-windows-resize