score:37

Accepted answer

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

http://jsfiddle.net/nvx3s/2/

<- 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?

http://jsfiddle.net/e2qpa/3/

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
}

example

donut chart with title in the center

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.


Related Query

More Query from same tag