Python Google Spreadsheet API 气泡图未正确显示

问题描述 投票:0回答:2

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

数据存储在 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
2个回答
1
投票

在这种情况下,我建议在请求正文中包含

bubbleMaxRadiusSize
bubbleMinRadiusSize
,如下所示。当你的请求体被修改后,就变成了如下。

来自:

'bubbleOpacity': 1.0}},

致:

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

结果:

整个脚本:

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

参考资料:


0
投票

您是否注意到所需输出中的相对大小与解决方案中给出的相对大小完全不同?例如,比较以色列和伊拉克。我正在寻找这个问题的答案:为什么 Google 电子表格中的气泡大小与数据源不正确成比例以及如何修复它?

令人惊讶的是,看起来没有人注意到这一点,或者少数注意到这一点的人被埋在谷歌搜索结果的一页又一页之下,这些人谈论这个主题,也没有意识到他们的图表不反映他们的数据。

© www.soinside.com 2019 - 2024. All rights reserved.