score:37
You need to take into account the location of the chart, so if you use the 'left' and 'top' attributes, you can add on half the width of the plot and subtract half the width of your text bounding box. This would yield the exact center:
text = chart.renderer.text("My Text").add();
textBBox = text.getBBox();
x = chart.plotLeft + (chart.plotWidth * 0.5) - (textBBox.width * 0.5);
y = chart.plotTop + (chart.plotHeight * 0.5) - (textBBox.height * 0.5);
text.attr({x: x, y: y});
Apparently the bounding box will be 0s unless you add it first.
Correction:
y = chart.plotTop + (chart.plotHeight * 0.5) + (textBBox.height * 0.25);
So, my original thought, and this seemed fine, was that the text would be aligned by the upper left, however it is done by the bottom left instead. Thus, instead of subtracting half the height, we actually need to add it. What confuses me, and something I don't understand yet, is that to get the center you add only 25% of the height rather then 50%. Note: This does not appear to work on IE 7 or 8.
MAJOR UPDATE
<- Centered text that works in all browers
What this new update does is it adds a new element using jquery after the chart is done. This works in all browers that I have tested (including IE7, 8, 9, Firefox and Chrome).
var textX = chart.plotLeft + (chart.plotWidth * 0.5);
var textY = chart.plotTop + (chart.plotHeight * 0.5);
// The width and height need to be calculated properly, so I made it a span.
var span = '<span id="pieChartInfoText" style="position:absolute; text-align:center;">';
span += '<span style="font-size: 32px">Upper</span><br>';
span += '<span style="font-size: 16px">Lower</span>';
span += '</span>';
$("#addText").append(span);
span = $('#pieChartInfoText');
span.css('left', textX + (span.width() * -0.5));
span.css('top', textY + (span.height() * -0.5));
score:0
var newChart = new Highcharts.Chart(options);
//Set No data text
var textX = newChart.plotLeft + (newChart.plotWidth * 0.5);
var textY = newChart.plotTop + (newChart.plotHeight * 0.3);
var textWidth = 500;
textX = textX - (textWidth / 2);
newChart.renderer.label('<div style="width: ' + textWidth + 'px; text-align: center"><span>Dati non disponibili per questa selezione</span></div>', textX, textY, null, null, null, true)
.css({
fontSize: '16px',
}).add();
score:0
I found this works when there is a legend and you set the text you want in the centre of the chart as the title:
events: {
load: function() {
const titleHeight = this.title.getBBox().height;
this.title.attr({
y: this.plotHeight / 2 + titleHeight / 2,
});
},
redraw: function() {
const titleHeight = this.title.getBBox().height;
this.title.attr({
y: this.plotHeight / 2 + titleHeight / 2,
});
},
},
score:1
First, you may want to check the html in your fiddle.
You had a width on your container of 500
instead of 500px
.
<div id="container" style="height: 400px; width: 500px"></div>
Second, this fiddle is a bit quick and dirty, but I think it accomplishes what you wanted?
score:2
Just add the following.
title: {
text : 'title to display in the middle'
verticalAlign: 'middle',
y: 5
},
score:9
The best solution is to rely on series center, instead of plotHeight
or plotWidth
.
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'pie'
},
plotOptions: {
pie: {
//innerSize: '60%'
}
},
title: {
text: ''
},
series: [{
data: [
['Firefox', 2262],
['IE7', 3800],
['IE6', 1000],
['Chrome', 1986]
]}]
},
function(chart) { // on complete
var textX = chart.plotLeft + (chart.series[0].center[0]);
var textY = chart.plotTop + (chart.series[0].center[1]);
var span = '<span id="pieChartInfoText" style="position:absolute; text-align:center;">';
span += '<span style="font-size: 32px">Upper</span><br>';
span += '<span style="font-size: 16px">Lower</span>';
span += '</span>';
$("#addText").append(span);
span = $('#pieChartInfoText');
span.css('left', textX + (span.width() * -0.5));
span.css('top', textY + (span.height() * -0.5));
});
Working demo: http://jsfiddle.net/4dL9S/
score:54
Use this code to center chart title vertically:
title: {
verticalAlign: 'middle',
floating: true
}
Edit
Since version 2.1 The floating property can be omitted as the documentation states it is implied by the presence of the verticalAlign property
When a value is given, the title behaves as floating.
Source: stackoverflow.com
Related Query
- Place text in center of pie chart - Highcharts
- HighCharts Pie Chart - Add text inside each slice
- Highcharts Donut Chart text in center change on hover
- How to center chart title position dynamically inside pie chart in highcharts
- How to center highcharts pie chart and legend on a page?
- set Pie chart labels exact in center of Slices of PIE highcharts
- highcharts donut chart center text overlaps with tooltip
- Highcharts - where to place style and changing style for one wedge in a pie chart
- Highcharts: How to align text label middle center of pie chart
- Highcharts - pie chart with html div at center
- Highcharts display label for pie chart using html table as data source
- Highcharts - Make text inside pie chart responsive
- Adding Text into Center of Pie Graph highcharts
- Pie Chart Center Text gets overlap with tooltip
- Add text inside a circular progress pie chart bar using HIGHCHARTS
- Highcharts Pie chart longer text
- How to center the title text in pie chart highchart?
- Adding text to the center of pie chart: Highcharts
- How to display the value instead of percentage in a pie chart using jquery Highcharts
- HighCharts - Make the pie chart 100% of the div
- Highcharts - labels inside and outside a pie chart
- Highcharts Pie Chart ignores percentageDecimals tooltip setting and has floating point inaccuracy issue
- Highcharts pie chart dynamically changes size
- Highcharts not displaying data labels for Pie chart in arabic
- Total of values in HighCharts Pie Chart
- highcharts - chart names for multiple pie chart series in one chart
- How do you add text to the bottom center of legend and bottom center of chart under legend?
- Highcharts Pie Chart Drilldown not showing on third drill
- how to make highcharts pie datalabels always in center of each slice?
- Highcharts Pie Chart Label Threshold
More Query from same tag
- Highcharts charts on a page not rendering correctly when output to PDF using wkhtmltopdf
- Equaling max value in two side of graph in highcharts.js
- Using a flask variable as data source for highcharts
- How can highcharts linear scale or log scale button enabel and zoom in or out full page
- Highcharts: how to separate stacked columns on datetime axis
- Manually load and unload external javascript in AngularJS
- Rounded corners in highcharts
- Plotting data using higcharts in flask web application
- Javascript event conflict, but not sure how to identify the first event assignment
- Highcharts is not responsive in ajax call
- how to call ajax function to update the Highcharts graph dynamically?
- IE 6 Export Menu size issue
- ASP.NET WebRequest to Highcharts export server
- highcharts 5.0.14 phantomjs issue with missing axis titles in exported image
- Highcharts error #13 unless I run under IE compatibility view?
- change the color of Highmap
- How do I create data binding between highcharts and Polymer 1.0
- Highchart line graph - Parse json
- Using Highcharts.js with odoo
- React: How to show correct date and plot data as Months in Highcharts
- HighCharts: render the xAise line as the yAise line
- How do I fix highchart to display series correctly with a special marker
- Highcharts Export to CSV No Data Grouping
- Highcharts version 7.1.1 how to offline-exporting PDF with amd modules
- Is there a way of dynamically toggling the Highstock navigator to regain vertical space for the chart?
- Label Y-Axis stops in Highcharts solid gauges
- Creating a Simple Line Chart with Highcharts
- How to refresh highcharts (pie chart) data via ajax
- Highcharts with multipe Series
- Add html buttons and selects to Highcharts