Menu

[r18]: / class_plot.py  Maximize  Restore  History

Download this file

249 lines (206 with data), 9.4 kB

  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
import re
class plot(object):
from class_copy import data_copy
def __init__(self,x,error,i):
self.x=x
self.e=error
self.i=i
self.parser=re.compile(r'{([^}]+)}')
def create_graph(self,sheet):
if not self.x.sheet_exists(sheet):
self.x.add_sheet(sheet)
print "Creating graph in sheet '%s'"%(sheet)
self.gr=self.x.create_graph(sheet)
def set_chart_type(self,type):
if self.e.debug_mode():
print "\t\tGraph type %s"%(type)
self.x.set_chart_type(self.gr,type)
def set_graph_data(self,data):
"""
data['xrange']
data['yrange'] - list of dicts
'name','data'
"""
if self.e.debug_mode():
print "\t\tSingle x multiple y data"
xrange=self.parse_col_name(data['xrange'])
if self.e.debug_mode():
print "\t\t\txrange %s"%(xrange)
f_data=[]
for ydata in data['yrange']:
yrange=self.parse_col_name(ydata['data'])
if self.e.debug_mode():
print "\t\t\tyrange %s"%(yrange)
f_data.append({'Name':ydata['name'],'Values':yrange,'XValues':xrange,'data_labels':ydata['labels']})
self.x.set_chart_data(self.gr,f_data)
def set_legend_position(self,position):
self.x.set_legend(self.gr,{'position':position})
def set_xy_data(self,data):
if self.e.debug_mode():
print "\t\tXY data"
f_data=[]
for line in data:#Multiple source sheets can de defined in data list
if self.e.debug_mode():
print "\t\tData:X-'%s',Y-'%s'"%(line['x'],line['y'])
xrange=self.parse_col_name(line['x'])
yrange=self.parse_col_name(line['y'])
if self.e.debug_mode():
print "\t\tData:xrange-'%s',yrange-'%s'"%(xrange,yrange)
if self.e.debug_mode():
print "\t\t\txrange %s yrange %s"%(xrange,yrange)
f_data.append({'Name':line['name'],'Values':yrange,'XValues':xrange,'data_labels':False})
self.x.set_chart_data(self.gr,f_data)
def set_compare_autodata_3d(self,data):
if self.e.debug_mode():
print "\t\tAuto data compare 3D, mode"
f_data=[]
if not self.x.sheet_exists(data['source']):
self.e.print_error("Data source sheet not exists '%s'"%(data['source']))
self.source_sheet=data['source']
autocolinfo=self.x.get_generated_col(self.source_sheet)
first_col=autocolinfo['first']
last_col=autocolinfo['last']
header_row=self.x.get_header_row(self.source_sheet)
data_col_iterator=self.x.get_data_col_iterator(self.source_sheet)
data_lines_iterator=self.x.get_data_lines_iterator(self.source_sheet)
#first_col_num=data_col_iterator[0]
first_row_num=data_lines_iterator[0]
last_row_num=data_lines_iterator[len(data_lines_iterator)-1]
xrange=self.rowcol2range(self.source_sheet,first_row_num,first_col-1,last_row_num,first_col-1)
for col_num in range(first_col,last_col+1):#For each data row in source sheet
yrange=self.rowcol2range(self.source_sheet,first_row_num,col_num,last_row_num,col_num)
name=self.x._rowcol_to_cell(self.source_sheet,header_row,col_num)
f_data.append({'Name':name,'Values':yrange,'XValues':xrange,'data_labels':False,'rgb':None})
self.x.set_chart_data(self.gr,f_data)
def set_auto_data(self,data,type):
if self.e.debug_mode():
print "\t\tAuto data, mode '%s'"%(type)
f_data=[]
for plot_section in data:#Multiple source sheets can de defined in data list
if not self.x.sheet_exists(plot_section['source']):
self.e.print_error("Data source sheet not exists '%s'"%(plot_section['source']))
self.source_sheet=plot_section['source']
if 'pycondition' in plot_section:
self.x.load_whole_sheet_data(self.source_sheet)#Loading all data to memory. Nedd to delete after use
if type=='autodata':
first_col=self.x.get_col_num_by_name(self.source_sheet,plot_section['from'],self.i.get_measure_type())
last_col=self.x.get_col_num_by_name(self.source_sheet,plot_section['to'],self.i.get_measure_type())
else:
autocolinfo=self.x.get_generated_col(self.source_sheet)
first_col=autocolinfo['first']
last_col=autocolinfo['last']
xdata_row=self.x.get_header_row(self.source_sheet)
xrange=self.rowcol2range(self.source_sheet,xdata_row,first_col,xdata_row,last_col)
if self.e.debug_mode():
print "\t\t\txrange %s"%(xrange)
for row_num in self.x.get_data_lines_iterator(self.source_sheet):#For each data row in source sheet
self.row_num=row_num
yrange=self.rowcol2range(self.source_sheet,row_num,first_col,row_num,last_col)
name=re.sub(r'{([^}]+)}', self.repl,plot_section['names'])
if 'rgb' in plot_section and plot_section['rgb']!='':
rgb=plot_section['rgb']
else:
rgb=None
if 'pycondition' in plot_section and plot_section['pycondition']!='':#If we need to use only specific rows from source sheet
parsed_confition=self.parser.sub(self.repl_cond, plot_section['pycondition'])#Parse measure formula
if not eval(parsed_confition):
continue
if self.e.debug_mode():
print "\t\t\tyrange %s"%(yrange)
f_data.append({'Name':name,'Values':yrange,'XValues':xrange,'data_labels':False,'rgb':rgb})
if 'pycondition' in plot_section:
self.x.delete_sheet_data(self.source_sheet)#Delete sheet data from memory
self.x.set_chart_data(self.gr,f_data)
def set_3d_rotation(self,rotation_configuration):
self.x._set_3d_rotation(self.gr,rotation_configuration)
def set_graph_title(self,title):
print "\tGraph '%s'"%(title['title'])
self.x.set_title(self.gr,title)
def set_logarithmic(self,alist):
if 'x' in alist:
self.x.set_x_logarithmic(self.gr)
if 'y' in alist:
self.x.set_y_logarithmic(self.gr)
def set_axis_minmax(self,data):
self.x.set_chart_axis_minmax(self.gr,data)
def set_ticklabels_numberformat(self,format):
self.x.set_chart_ticklabels_numberformat(self.gr,format)
def set_gridlines(self,gridlines,type):
formatted_gridlines={}
if 'x' in gridlines:
if 'major' in gridlines['x']:
formatted_gridlines['xlCategory']={'HasMajorGridlines':True}
if gridlines['x']['major']!='auto':
if type in ['xlXYScatterSmooth', 'xlXYScatterSmoothNoMarkers', 'xlXYScatter']:
formatted_gridlines['xlCategory']['MajorUnit']=float(gridlines['x']['major'])
else:
formatted_gridlines['xlCategory']['TickMarkSpacing']=float(gridlines['x']['major'])
if 'y' in gridlines:
if 'major' in gridlines['y']:
formatted_gridlines['xlValue']={'HasMajorGridlines':True}
if gridlines['y']['major']!='auto':
formatted_gridlines['xlValue']['MajorUnit']=float(gridlines['y']['major'])
#if type in ['xlLine','xlXYScatterSmoothNoMarkers','xlXYScatter']:
# formatted_gridlines['xlValue']['MajorUnit']=float(gridlines['y']['major'])
#else:
# formatted_gridlines['xlValue']['TickMarkSpacing']=float(gridlines['y']['major'])
self.x.set_gridlines(self.gr,formatted_gridlines)
def rowcol2range(self,sheet,row1,col1,row2,col2):
first=self.x._rowcol_to_cell(sheet,row1,col1)
second=self.x._rowcol_to_cell(sheet,row2,col2)
return "%s!%s:%s"%(sheet,first,second)
def repl(self,match):
matched_string=match.group(1)#Get matched text
matched_string=matched_string.strip()
match=matched_string.split('!')
#Parse string for replacement
if len(match)==3:#'sheet!type!column_name' format
sheet=match[0]
type_str=match[1]
column_name=match[2]
elif len(match)==2:#'type!column_name' format
sheet=self.source_sheet
type_str=match[0]
column_name=match[1]
elif len(match)==1:#column_name
sheet=self.source_sheet
type_str='m'
column_name=match[0]
else:
self.e.print_error("Unsupported format for column name- '%s'"%(match))
if self.source_sheet!=sheet:
self.e.print_error("Source sheet mismatch %s %s"%(self.source_sheet,sheet))
type=self.i.get_type_from_string(type_str)#Get proper format of type from entered string
col=self.x.get_col_num_by_name(sheet,column_name,type)
return "%s!%s"%(sheet,self.x._rowcol_to_cell(sheet,self.row_num,col))
def parse_col_name(self,name):
fields=name.strip().split('!')
clean_fields=[f.strip() for f in fields]
type=self.i.get_type_from_string(clean_fields[1])#Get proper format of type from entered string
sheet=clean_fields[0]
if not self.x.sheet_exists(sheet):
self.e.print_error("Sheet '%s' not exists"%(sheet))
column_name=clean_fields[2]
col=self.x.get_col_num_by_name(sheet,column_name,type)
first=self.x._rowcol_to_cell(sheet,self.x.get_first_data_row(sheet),col)
second=self.x._rowcol_to_cell(sheet,self.x.get_last_data_row(sheet),col)
return "%s!%s:%s"%(sheet,first,second)
def repl_cond(self,match):
matched_string=match.group(1)#Get matched text
matched_string=matched_string.strip()
fields=matched_string.strip().split('!')
if len(fields)==2:
type_string=fields[0]
col_name=fields[1]
elif len(fields)==1:
type_string=self.i.get_measure_string()
col_name=fields[1]
else:
self.e.print_error("Unexpected string for replacement '%s'"%(matched_string))
type=self.i.get_type_from_string(type_string)#Get proper format of type from entered string
cell_value=self.x.get_cell_value_from_memory(self.source_sheet,col_name,self.row_num,type)
try:
float(cell_value)
except:
cell_value='"%s"'%(cell_value)
return str(cell_value)
MongoDB Logo MongoDB
Gen AI apps are built with MongoDB Atlas
Atlas offers built-in vector search and global availability across 125+ regions. Start building AI apps faster, all in one place.
Try Free →