3

I'm doing a responsive pie chart which holds title in centered position inside it.I've used,

title: {
            text: "",
            margin: 0,
            y:0,
            x:0,
            align: 'center',
            verticalAlign: 'middle',
        },

but it's not perfectly centered inside the chart.Any suggestions would be appreciated.Thank you in advance.

Here's the Link : http://jsfiddle.net/LHSey/128/

2 Answers 2

6

Better is remove title and use renderer which allows to add custom text, which can be repositioned each time (when you redraw chart). Only what you need is catch this event.

function addTitle() {

        if (this.title) {
            this.title.destroy();
        }

        var r = this.renderer,
            x = this.series[0].center[0] + this.plotLeft,
            y = this.series[0].center[1] + this.plotTop;
        this.title = r.text('Series 1', 0, 0)
            .css({
            color: '#4572A7',
            fontSize: '16px'
        }).hide()
            .add();

        var bbox = this.title.getBBox();
        this.title.attr({
            x: x - (bbox.width / 2),
            y: y
        }).show();
    }

chart:{
    events: {
                    load: addTitle,
                    redraw: addTitle,
            },
} 

Example: http://jsfiddle.net/LHSey/129/

5
  • 1
    Expert solution.This works really fine for me.Thanks @Sabastian.
    – Arun
    Jan 8, 2015 at 14:20
  • 1
    If the title is completely text based it's working really fine.But if the title consists of image it doesn't consider the image and there's no error persists.In the case of using useHTML :true also it doesn't workout.Is there any way to achieve it?There're also constraints,if the title is more than single line.
    – Arun
    Jan 8, 2015 at 18:29
  • You can use next renderer for image and use the same option. Because HTML object cannot be "renderered", becuase highcharts uses SVG not HTML. Jan 9, 2015 at 10:21
  • why doesnt it align veritcally if I change font size to large like 60px?
    – Toniq
    Mar 10, 2018 at 17:07
  • @SebastianBochan can you please help me how can I implement the addTitle() in HighchartsReact?
    – newbie
    Jan 8, 2021 at 14:40
0

We can customize any properties of title using chart.setTitle() as shown below.I've added a title and set useHTML property to true.

chart: {
            events: {
                    load: function () {
                        this.setTitle({text: 'Title',useHTML:true});
                    } 
            }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.