Search code examples
pythongoogle-sheetsgoogle-sheets-apibubble-chart

Python Google Spreadsheet API Bubble Chart not properly displayed


I use the Google example https://developers.google.com/chart/interactive/docs/gallery/bubblechart to test the Google Spreadshhet API v4 with Python v3.7.8 to create a bubble chart.

The data are stored in a Spreadsheet in a Google Drive where I create the bubble chart.

enter image description here

The bubble chart is created but the bubbles are not visible/displayed. Hereafter the code:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

...

body = {'requests': 
        [{'addChart': 
          {'chart': 
           {'spec': 
            {'title': 'Correlation between life expectancy, fertility rate and population of some world countries (2010)', 
             'titleTextPosition': 
             {'horizontalAlignment': 'CENTER'}, 
             'bubbleChart': 
             {'legendPosition': 'RIGHT_LEGEND',
              'domain': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 1, 
                  'endColumnIndex': 2}]}}, 
              'series': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 2, 
                  'endColumnIndex': 3}]}}, 
              'groupIds': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                 'startRowIndex': 17, 
                 'endRowIndex': 27, 
                 'startColumnIndex': 3, 
                 'endColumnIndex': 4}]}}, 
              'bubbleLabels': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 0, 
                  'endColumnIndex': 1}]}}, 
              'bubbleSizes': 
              {'sourceRange': 
               {'sources': 
                [{'sheetId': 909072886, 
                  'startRowIndex': 17, 
                  'endRowIndex': 27, 
                  'startColumnIndex': 4, 
                  'endColumnIndex': 5}]}}, 
              'bubbleOpacity': 1.0}}, 
            'position': 
            {'overlayPosition': 
             {'anchorCell': 
              {'sheetId': 909072886, 
               'rowIndex': 61, 
               'columnIndex': 6}, 
               'offsetXPixels': 0, 
               'offsetYPixels': 0, 
               'widthPixels': 600, 
               'heightPixels': 371
             }
            }
           }
          }
         }
        ]
       }

response = service.spreadsheets().batchUpdate(spreadsheetId=file_id, body=body).execute()

I should get the following:

enter image description here

But I get this:

no bubbles displayed. Remark: with mouse over the (non visible) bubble, it displays all the right data (life expectancy, fertility rate, poupulation, region with the right color) of the country!

enter image description here

Don't hesitate to support me!


Solution

  • In this case, I would like to propose to include bubbleMaxRadiusSize and bubbleMinRadiusSize in the request body as follows. When your request body is modified, it becomes as follows.

    From:

    'bubbleOpacity': 1.0}},
    

    To:

    'bubbleOpacity': 1.0,
    'bubbleMaxRadiusSize': 50,
    'bubbleMinRadiusSize': 5
    }},
    
    • In this modification, 50 and 5 are used as the sample values for bubbleMaxRadiusSize and bubbleMinRadiusSize, respectively. So please modify these values for your actual situation.

    Result:

    enter image description here

    Whole script:

    body = {'requests': [{'addChart': {'chart': {'spec': {'title': 'Correlation between life expectancy, fertility rate and population of some world countries (2010)', 'titleTextPosition': {'horizontalAlignment': 'CENTER'},
         'bubbleChart': {
        'legendPosition': 'RIGHT_LEGEND',
        'domain': {'sourceRange': {'sources': [{
            'sheetId': 909072886,
            'startRowIndex': 17,
            'endRowIndex': 27,
            'startColumnIndex': 1,
            'endColumnIndex': 2,
            }]}},
        'series': {'sourceRange': {'sources': [{
            'sheetId': 909072886,
            'startRowIndex': 17,
            'endRowIndex': 27,
            'startColumnIndex': 2,
            'endColumnIndex': 3,
            }]}},
        'groupIds': {'sourceRange': {'sources': [{
            'sheetId': 909072886,
            'startRowIndex': 17,
            'endRowIndex': 27,
            'startColumnIndex': 3,
            'endColumnIndex': 4,
            }]}},
        'bubbleLabels': {'sourceRange': {'sources': [{
            'sheetId': 909072886,
            'startRowIndex': 17,
            'endRowIndex': 27,
            'startColumnIndex': 0,
            'endColumnIndex': 1,
            }]}},
        'bubbleSizes': {'sourceRange': {'sources': [{
            'sheetId': 909072886,
            'startRowIndex': 17,
            'endRowIndex': 27,
            'startColumnIndex': 4,
            'endColumnIndex': 5,
            }]}},
        'bubbleOpacity': 1.0,
        'bubbleMaxRadiusSize': 50,  # Added
        'bubbleMinRadiusSize': 5,  # Added
        }}, 'position': {'overlayPosition': {
        'anchorCell': {'sheetId': 909072886, 'rowIndex': 61, 'columnIndex': 6},
        'offsetXPixels': 0,
        'offsetYPixels': 0,
        'widthPixels': 600,
        'heightPixels': 371,
        }}}}}]}
    
    response = service.spreadsheets().batchUpdate(spreadsheetId=file_id, body=body).execute()

    References: