Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center highcharts pie chart and legend on a page?

I'm loading my highchart data on a node/react single page application and I cant figure out how to center it in the middle of the page. Currently it looks like this:

enter image description here

It's floating to the left and I want to center everything in the orange box in the middle. I've looked throughout the highcharts api doc and can't seem to figure it out. The jsfiddle is here: http://jsfiddle.net/tobitobetoby/1fqvzpdn/36/ (even though it's centered in jsfiddle, it floats to the left on a web/html page)

chart = new Highcharts.Chart({
  chart: {
      renderTo: 'container',
      type: 'pie',
      events: {
        load: function(event) {
          var chart = this,
            points = chart.series[0].points,
            len = points.length,
            total = 0,
            i = 0;
          for (i = 0; i < len; i++) {
            total += points[i].y;
          }
          chart.setTitle({
            text: '<br>€' + total,
            verticalAlign: 'middle',
            style: {
              fontFamily: 'Arial,Roboto,Helvetica,sans-serif',
              fontWeight: 'bold',
              fontSize: 34
            },
          });
          // Adding 'transaction' label - labels below don't support images/icons
          this.renderer.label("<div class='transactions' style='fontSize:20px !important;'><img style='width:25px; height:25px; position:relative; top:7px;' src='https://github.com/tobi-ajala/shell-exercise/blob/master/icons/card.png?raw=true'/> &nbsp Transactions</div>", 120, 130, null, null, null, true).add();
          // Adding date label
          this.renderer.label("<div class='transactions'>11 Sept 2017 - 11 Oct 2017</div>", 95, 225, null, null, null, true).add();
    }
  } 
}
like image 389
T.Doe Avatar asked Oct 31 '25 15:10

T.Doe


1 Answers

Looking at the complete git repo, the index.css file was not being imported in the index.js file, hence not being able to reference the css commands. The solution is, in the index.js file, add:

import './index.css';

To the top of the code. Then in the index.css file, add:

.highcharts-container {
  margin: 0 auto;
}

And this will centre the enter Highcharts div.

like image 66
T.Doe Avatar answered Nov 02 '25 05:11

T.Doe