【问题标题】:Python Google Spreadsheet API Bubble Chart not properly displayedPython Google 电子表格 API 气泡图未正确显示
【发布时间】:2021-01-11 19:29:33
【问题描述】:

我使用 google 示例 https://developers.google.com/chart/interactive/docs/gallery/bubblechart 来测试 Google Spreadshhet API v4 和 Python v3.7.8 以创建气泡图。

数据存储在我创建气泡图的 Google Drive 中的电子表格中。

气泡图已创建,但气泡不可见/不显示。以下代码:

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()

我应该得到以下信息:

但我明白了:没有显示气泡。备注:鼠标悬停在(不可见)气泡上,显示国家所有正确数据(预期寿命、生育率、人口、颜色正确的地区)!

不要犹豫,支持我!提前谢谢你。

【问题讨论】:

    标签: python api google-sheets google-sheets-api bubble-chart


    【解决方案1】:

    在这种情况下,我建议在请求正文中包含bubbleMaxRadiusSizebubbleMinRadiusSize,如下所示。当你的请求体被修改后,变成如下。

    发件人:

    'bubbleOpacity': 1.0}},
    

    收件人:

    'bubbleOpacity': 1.0,
    'bubbleMaxRadiusSize': 50,
    'bubbleMinRadiusSize': 5
    }},
    
    • 在此修改中,505 分别用作bubbleMaxRadiusSizebubbleMinRadiusSize 的样本值。所以请根据您的实际情况修改这些值。

    结果:

    整个脚本:

    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()

    参考资料:

    【讨论】:

    • 很好用!我很惊讶我认为这 2 个字段有默认值。
    • @hganger 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    相关资源
    最近更新 更多