Using the slices setoption in Google Apps Script

2019-07-23 06:14发布

问题:

I'm doing some testing with Google Charts service, and I'm trying to explode certain slices of my piechart. However, this isn't working as I'd hoped.

My data source is a range A1:B30, and when I try to explode a range within it, for example the first 4 slices, I'd have thought this would do the trick:

setOption('slices',['0',{offset: 0.75},'1',{offset: 0.75},'2',{offset: 0.75},
                        '3',{offset: 0.75},'4',{offset: 0.75}])

However, that doesn't seem to work - I end up with this:

It's weird - Has it got something to do with there being two elements (the A and B column values in the range) ?? That would explain if the pattern was it always skipped one slice, but at one point it skips two - strange! Any advice?

best wishes

Dave

回答1:

there are a couple options for defining slices

you can use an object ({}) with named keys for each slice

pieChart.setOption('slices', {
  0: {offset: 0.05},
  1: {offset: 0.15},
  2: {offset: 0.25},
  3: {offset: 0.35}
});

or an array ([]) of objects for each slice

pieChart.setOption('slices', [
  {offset: 0.05},
  {offset: 0.15},
  {offset: 0.25},
  {offset: 0.35}
]);

each a little different from the code in question, see following working snippet...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([
      ['Language', 'Speakers (in millions)'],
      ['Assamese', 13], ['Bengali', 83], ['Bodo', 14],
      ['Dogri', 23], ['Gujarati', 46], ['Hindi', 300],
      ['Kannada', 38], ['Kashmiri', 5.5], ['Konkani', 5],
      ['Maithili', 20], ['Malayalam', 33], ['Manipuri', 1.5],
      ['Marathi', 72], ['Nepali', 2.9], ['Oriya', 33],
      ['Punjabi', 29], ['Sanskrit', 0.01], ['Santhali', 6.5],
      ['Sindhi', 2.5], ['Tamil', 61], ['Telugu', 74], ['Urdu', 52]
    ]);

    var pieChart = new google.visualization.ChartWrapper({
      chartType: 'PieChart',
      containerId: 'chart_div',
      dataTable: data,
      options: {
        height: 400,
        width: 400
      }
    });

    pieChart.setOption('slices', {
      0: {offset: 0.05},
      1: {offset: 0.15},
      2: {offset: 0.25},
      3: {offset: 0.35}
    });

    pieChart.draw();
  },
  packages:['controls', 'corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>