We help IT Professionals succeed at work.

Semi circle donut using Google visualization

Steynsk
Steynsk asked
on
Hi Experts,

I'm building a reservation site for a nonprofit organisation. And I'd like to add a chart in the management section.
I try to create a semi circle donut chart that shows the status of occupation in percentage and numbers of chairs in a theater.
The maximum amount of  chairs that can be occupied is 400. The data we have available is the amount of chairs occupied.

Can someone please help me to adapt the sample below so that it shows number of chairs and percentage of occupied and free chairs in this semi circle donut chart?



<html>
  <head>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
 
google.charts.load('current', {
  callback: function () {
    var data = [
      ['Task', 'Hours'],
      ['Occupied',  19.2],
      ['Free',  30.8],
      [null, 50.0]
    ];

    var total = 0;
    for (var i = 1; i < data.length; i++) {
      if (data[i][0] !== null) {
        total += data[i][1];
      }
    }

    var numberFormat = new google.visualization.NumberFormat({
      pattern: '#,##0.0',
      suffix: '%'
    });

    var dataTable = google.visualization.arrayToDataTable(data);
    for (var i = 0; i < dataTable.getNumberOfRows(); i++) {
      if (dataTable.getValue(i, 0) !== null) {
        dataTable.setFormattedValue(i, 1);
      }
    }

    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

    var options = {
	  title: 'Status of chairs',
      height: 400,
      chartArea: {
        top: 24
      },
      colors: ['#8BC34A', '#64B5F6'],
      pieHole: 0.4,
      pieStartAngle: 270,
      pieSliceText: 'label',
      slices: {
        2: {
          color: 'transparent'
        }
      },
      theme: 'maximized',
      width: 400
    };

    chart.draw(dataTable, options);
  },
  packages: ['corechart']
});

    </script>
  </head>
  <body>

    <div id="chart_div" style="width: 400px; height: 120px;"></div>
  </body>
</html>

Open in new window

Comment
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Which part do you need help with
    var data = [
      ['Task', 'Hours'],
      ['Occupied',  19.2],
      ['Free',  30.8],
      [null, 50.0]
    ];

Open in new window


This is where you set your data as
Occupied: Occupied / 400 * 50 
Free: (400-Occupied) / 400 * 50

Open in new window

Author

Commented:
Thank you Julian  I've changed the code into:
<html>
  <head>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
var Occupied = 225 / 400 * 50
var Free = (400-225) / 400 * 50
google.charts.load('current', {
  callback: function () {
    var data = [
      ['Label', 'Value*2'],
      ['Occupied', Occupied ],
      ['Free',  Free ],
      [null, 50.0]
    ];

    var total = 0;
    for (var i = 1; i < data.length; i++) {
      if (data[i][0] !== null) {
        total += data[i][1];
      }
    }

    var numberFormat = new google.visualization.NumberFormat({
      pattern: '#,##0.0',
      suffix: '%'
    });

    var dataTable = google.visualization.arrayToDataTable(data);
    for (var i = 0; i < dataTable.getNumberOfRows(); i++) {
      if (dataTable.getValue(i, 0) !== null) {
        dataTable.setFormattedValue(i, 1);
      }
    }

    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

    var options = {
	  title: 'Status of chairs',
      height: 400,
      chartArea: {
        top: 24
      },
      colors: ['#8BC34A', '#64B5F6'],
      pieHole: 0.4,
      pieStartAngle: 270,
      pieSliceText: 'label',
      slices: {
        2: {
          color: 'transparent'
        }
      },
      theme: 'maximized',
      width: 400
    };

    chart.draw(dataTable, options);
  },
  packages: ['corechart']
});

    </script>
  </head>
  <body>

    <div id="chart_div" style="width: 400px; height: 120px;"></div>
  </body>
</html>

Open in new window


But when I click one of the two sections I get the wrong values. I'm sorry I don't understand hoe this works.

Kind regards,
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Ok I see what you mean,

It is even easier than that just put the actual values for Occupied and Free and change the null to 400.

How it works.

The chart assumes a full doughnut so all the values in the array, when added up, will represent (not equal) 360 degrees. So if the total is 800 and the null value is 400 this represents half the total so 180 degrees.

Each segment is calculated as the percentage of the total of all the items.

To make a half doughnut you put in the null value equal to the sum of the other two - that part does not get drawn - which is half a circle - so you only see the top half

So, what you need to do is this
    var data = [
      ['Task', 'Hours'],
      ['Occupied',  225],
      ['Free',  175],
      [null, 400]
    ];

Open in new window

Bear in mind though that while you only see half the doughnut - the other half is there - just not visible so the chart actually extends down an equal distance (of the top half). this might create a gap at the bottom that you would need to sort out. You can do this by putting it in a container (<div>) half the height of the chart and setting overflow: hidden.

Author

Commented:
Thank you very much for your help.
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.