/
chart.py
366 lines (328 loc) · 13.6 KB
/
chart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from pygsheets.utils import format_addr
from pygsheets.cell import Cell
from pygsheets.custom_types import ChartType
from pygsheets.exceptions import InvalidArgumentValue
class Chart(object):
"""
Represents a chart in a sheet.
:param worksheet: Worksheet object in which the chart resides
:param domain: Cell range of the desired chart domain in the form of tuple of tuples
:param ranges: Cell ranges of the desired ranges in the form of list of tuple of tuples
:param chart_type: An instance of :class:`ChartType` Enum.
:param title: Title of the chart
:param anchor_cell: Position of the left corner of the chart in the form of cell address or cell object
:param json_obj: Represents a json structure of the chart as given in `api <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#BasicChartSpec>`__.
"""
def __init__(self, worksheet, domain=None, ranges=None, chart_type=None, title='', anchor_cell=None, json_obj=None):
self._title = title
self._chart_type = chart_type
self._domain = ()
if domain:
self._domain = (format_addr(domain[0], 'tuple'), format_addr(domain[1], 'tuple'))
self._ranges = []
if ranges:
for i in range(len(ranges)):
self._ranges.append((format_addr(ranges[i][0], 'tuple'), format_addr(ranges[i][1], 'tuple')))
self._worksheet = worksheet
self._title_font_family = 'Roboto'
self._font_name = 'Roboto'
self._legend_position = 'RIGHT_LEGEND'
self._chart_id = None
self._anchor_cell = anchor_cell
if json_obj is None:
self._create_chart()
else:
self.set_json(json_obj)
@property
def title(self):
"""Title of the chart"""
return self._title
@title.setter
def title(self, new_title):
temp = self._title
self._title = new_title
try:
self.update_chart()
except:
self._title = temp
@property
def domain(self):
"""
Domain of the chart.
The domain takes the cell range in the form of tuple of cell adresses. Where first adress is the
top cell of the column and 2nd element the last adress of the column.
Example: ((1,1),(6,1)) or ('A1','A6')
"""
return self._domain
@domain.setter
def domain(self, new_domain):
new_domain = (format_addr(new_domain[0], 'tuple'), format_addr(new_domain[1], 'tuple'))
temp = self._domain
self._domain = new_domain
try:
self.update_chart()
except:
self._domain = temp
@property
def chart_type(self):
"""Type of the chart
The specificed as enum of type :class:'ChartType'
The available chart types are given in the `api docs <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#BasicChartType>`__ .
"""
return self._chart_type
@chart_type.setter
def chart_type(self, new_chart_type):
if not isinstance(new_chart_type, ChartType):
raise InvalidArgumentValue
temp = self._chart_type
self._chart_type = new_chart_type
try:
self.update_chart()
except:
self._chart_type = temp
@property
def ranges(self):
"""
Ranges of the chart (y values)
A chart can have multiple columns as range. So you can provide them as a list. The ranges are
taken in the form of list of tuple of cell adresses. where each tuple inside the list represents
a column as staring and ending cell.
Example:
[((1,2),(6,2)), ((1,3),(6,3))] or [('B1','B6'), ('C1','C6')]
"""
return self._ranges
@ranges.setter
def ranges(self, new_ranges):
if type(new_ranges) is tuple:
new_ranges = [new_ranges]
for i in range(len(new_ranges)):
new_ranges[i] = (format_addr(new_ranges[i][0], 'tuple'), format_addr(new_ranges[i][1], 'tuple'))
temp = self._ranges
self._ranges = new_ranges
try:
self.update_chart()
except:
self._ranges = temp
@property
def title_font_family(self):
"""
Font family of the title. (Default: 'Roboto')
"""
return self._title_font_family
@title_font_family.setter
def title_font_family(self, new_title_font_family):
temp = self._title_font_family
self._title_font_family = new_title_font_family
try:
self.update_chart()
except:
self._title_font_family = temp
@property
def font_name(self):
"""
Font name for the chart. (Default: 'Roboto')
"""
return self._font_name
@font_name.setter
def font_name(self, new_font_name):
temp = self._font_name
self._font_name = new_font_name
try:
self.update_chart()
except:
self._font_name = temp
@property
def legend_position(self):
"""
Legend postion of the chart. (Default: 'RIGHT_LEGEND')
The available options are given in the `api docs <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#BasicChartLegendPosition>`__.
"""
return self._legend_position
@legend_position.setter
def legend_position(self, new_legend_position):
temp = self._legend_position
self._legend_position = new_legend_position
try:
self.update_chart()
except:
self._legend_position = temp
@property
def id(self):
"""Id of the this chart."""
return self._chart_id
@property
def anchor_cell(self):
"""Position of the left corner of the chart in the form of cell address or cell object,
Changing this will move the chart.
"""
return self._anchor_cell
@anchor_cell.setter
def anchor_cell(self, new_anchor_cell):
temp = self._anchor_cell
try:
if type(new_anchor_cell) is Cell:
self._anchor_cell = (new_anchor_cell.row, new_anchor_cell.col)
self._update_position()
else:
self._anchor_cell = format_addr(new_anchor_cell, 'tuple')
self._update_position()
except:
self._anchor_cell = temp
def delete(self):
"""
Deletes the chart.
.. warning::
Once the chart is deleted the objects of that chart still exist and should not be used.
"""
request = {
"deleteEmbeddedObject": {
"objectId": self._chart_id
}
}
self._worksheet.client.sheet.batch_update(self._worksheet.spreadsheet.id, request)
def refresh(self):
"""Refreshes the object to incorporate the changes made in the chart through other objects or Google sheet"""
chart_data = self._worksheet.client.sheet.get(self._worksheet.spreadsheet.id, fields='sheets(charts,properties)')
sheet_list = chart_data.get('sheets')
for sheet in sheet_list:
if sheet.get('properties', {}).get('sheetId', None) is self._worksheet.id:
chart_list = sheet.get('charts')
if chart_list:
for chart in chart_list:
if chart.get('chartId') == self._chart_id:
self.set_json(chart)
def _get_anchor_cell(self):
if self._anchor_cell is None:
return {
"columnIndex": self._domain[1][1]-1,
"rowIndex": self._domain[1][0], "sheetId": self._worksheet.id}
else:
if type(self._anchor_cell) is Cell:
return {
"columnIndex": self._anchor_cell.col-1,
"rowIndex": self._anchor_cell.row-1, "sheetId": self._worksheet.id}
else:
cell = format_addr(self._anchor_cell, 'tuple')
return {
"columnIndex": cell[1]-1,
"rowIndex": cell[0]-1, "sheetId": self._worksheet.id}
def _get_ranges_request(self):
ranges_request_list = []
for i in range(len(self._ranges)):
req = {
'series': {
'sourceRange': {
'sources': [self._worksheet.get_gridrange(self._ranges[i][0], self._ranges[i][1])]
}
},
}
ranges_request_list.append(req)
return ranges_request_list
def _create_chart(self):
request = {
"addChart": {
"chart": {
"spec": {
"title": self._title,
"basicChart": {
"chartType": self._chart_type.value,
"domains": [
{
"domain": {
"sourceRange": {
"sources": [
self._worksheet.get_gridrange(self._domain[0], self._domain[1])
]
}
}
}
],
"series": self._get_ranges_request()
}
},
"position": {
"overlayPosition": {
"anchorCell": self._get_anchor_cell()
}
}
}
}
}
response = self._worksheet.client.sheet.batch_update(self._worksheet.spreadsheet.id, request)
chart_data_list = response.get('replies')
chart_json = chart_data_list[0].get('addChart',{}).get('chart')
self.set_json(chart_json)
def _update_position(self):
request = {
"updateEmbeddedObjectPosition": {
"objectId": self._chart_id,
"newPosition": {
"overlayPosition": {
"anchorCell": {
"sheetId": self._worksheet.id,
"rowIndex": self._anchor_cell[0]-1,
"columnIndex": self._anchor_cell[1]-1
}
}
},
"fields": "*"
}}
self._worksheet.client.sheet.batch_update(self._worksheet.spreadsheet.id, request)
def update_chart(self):
"""updates the applied changes to the sheet."""
request = {
'updateChartSpec':{
'chartId': self._chart_id, "spec": self.get_json()}
}
self._worksheet.client.sheet.batch_update(self._worksheet.spreadsheet.id, request)
def get_json(self):
"""Returns the chart as a dictionary structured like the Google Sheets API v4."""
domains = [{'domain': {'sourceRange': {'sources': [
self._worksheet.get_gridrange(self._domain[0], self._domain[1])]}}}]
ranges = self._get_ranges_request()
spec = dict()
spec['title'] = self._title
spec['basicChart'] = dict()
spec['titleTextFormat'] = dict()
spec['basicChart']['chartType'] = self._chart_type.value
spec['basicChart']['legendPosition'] = self._legend_position
spec['titleTextFormat']['fontFamily'] = self._title_font_family
spec['fontName'] = self._font_name
spec['basicChart']['domains'] = domains
spec['basicChart']['series'] = ranges
return spec
def set_json(self, chart_data):
"""
Reads a json-dictionary returned by the Google Sheets API v4 and initialize all the properties from it.
:param chart_data: The chart data as json specified in sheets api.
"""
anchor_cell_data = chart_data.get('position',{}).get('overlayPosition',{}).get('anchorCell')
self._anchor_cell = (anchor_cell_data.get('rowIndex',0)+1, anchor_cell_data.get('columnIndex',0)+1)
self._title = chart_data.get('spec',{}).get('title',None)
self._chart_id = chart_data.get('chartId',None)
self._title_font_family = chart_data.get('spec',{}).get('titleTextFormat',{}).get('fontFamily',None)
self._font_name = chart_data.get('spec',{}).get('titleTextFormat',{}).get('fontFamily',None)
basic_chart = chart_data.get('spec',{}).get('basicChart', None)
self._chart_type = ChartType(basic_chart.get('chartType', None))
self._legend_position = basic_chart.get('legendPosition', None)
domain_list = basic_chart.get('domains')
for d in domain_list:
source_list = d.get('domain',{}).get('sourceRange',{}).get('sources',None)
for source in source_list:
start_row = source.get('startRowIndex',0)
end_row = source.get('endRowIndex',0)
start_column = source.get('startColumnIndex',0)
end_column = source.get('endColumnIndex',0)
self._domain = [(start_row+1, start_column+1),(end_row, end_column)]
range_list = basic_chart.get('series', [])
self._ranges = []
for r in range_list:
source_list = r.get('series',{}).get('sourceRange',{}).get('sources',None)
for source in source_list:
start_row = source.get('startRowIndex',0)
end_row = source.get('endRowIndex',0)
start_column = source.get('startColumnIndex',0)
end_column = source.get('endColumnIndex',0)
self._ranges.append([(start_row+1, start_column+1), (end_row, end_column)])
def __repr__(self):
return '<%s %s %s>' % (self.__class__.__name__, self.chart_type.value, repr(self.title))