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)