python - Python Google Spreadsheet API 气泡图未正确显示
问题描述
我使用谷歌示例https://developers.google.com/chart/interactive/docs/gallery/bubblechart使用 Python v3.7.8 测试 Google Spreadshhet API v4 以创建气泡图。
数据存储在我创建气泡图的谷歌驱动器中的电子表格中。
气泡图已创建,但气泡不可见/不显示。此后的代码:
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()
我应该得到以下信息:
但我明白了:没有显示气泡。备注:将鼠标悬停在(不可见)气泡上,它会显示该国家所有正确的数据(预期寿命、生育率、人口、颜色正确的地区)!
不要犹豫支持我!先感谢您。
解决方案
在这种情况下,我建议在请求正文中包含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()
参考:
推荐阅读
- javascript - Nodejs,试图将var导出到另一个文件
- python - 迭代数据帧并使用基于条件的替换方法
- statistics - 如何将估计每华氏度增加的效应大小转换为摄氏度?
- android - 根据 oAuth2.0 身份验证应该在设备本机浏览器中进行,那么为什么所有已知的应用程序都具有本机登录屏幕
- html - 即使在标记它们之后,边框也不会在 CSS 中显示!重要
- arraylist - 爪哇。将 ListArray 转换为 List
- terraform - 如何在 terraform 中对来自多个来源的资源组 ID 运行 for_each?
- go - 如何使用 GO SDK 在 Couchbase 文档中添加新字段
- google-apps-script - 清除一张工作表上某一列中每一行的内容,具体取决于另一张工作表上同一行的某一列的变化
- php - 如果按名称请求,如何让 Nginx 找到现有的 index.php?