center chart title inside pie chart - Javascript highcharts

Javascript examples for highcharts:Pie Chart

Description

center chart title inside pie chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript">
$(function () {/*from   www.  j av  a2  s  .co m*/
    var chart;
    $(document).ready(function () {
        chart = new Highcharts.Chart({
            chart: {
                borderWidth: 1,
                renderTo: 'ChartContainer',
                type: "pie",
                events: {
                    load: addTitle,
                    redraw: addTitle,
                },
            },
            plotOptions: {
                pie: {
                    animation: false,
                    borderWidth: 0,
                    innerSize: '90%',
                    dataLabels: {
                        enabled: false
                    }
                },
            },
            title: {
                text: "",
                margin: 0,
                y: 0,
                x: 0,
                align: 'center',
                verticalAlign: 'middle',
            },
            yAxis: {
                title: {
                    text: null
                },
            },
            series: [{
                data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
            }]
        });
    });
    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();
        }
});

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <script src="https://code.highcharts.com/modules/exporting.js"></script> 
      <div id="ChartContainer" style="width:100%; height: 400px; margin: 0 auto"></div>  
   </body>
</html>

Related Tutorials