Esempio n. 1
0
 def updateColors(layout):
     for k, v in layout.items():
         if isinstance(v, dict):
             updateColors(v)
         else:
             if isinstance(v, list):
                 for _ in v:
                     if isinstance(_, dict):
                         updateColors(_)
             if 'color' in k.lower():
                 if 'rgba' not in v:
                     layout[k] = normalize(v)
     return layout
Esempio n. 2
0
	def updateColors(layout):
		for k,v in layout.items():
			if isinstance(v,dict):
				updateColors(v)
			else:
				if isinstance(v,list):
					for _ in v:
						if isinstance(_,dict):
							updateColors(_)
				if 'color' in k.lower():
					if 'rgba' not in v:
						layout[k]=normalize(v)
		return layout
Esempio n. 3
0
def getLayout(theme=None,
              title='',
              xTitle='',
              yTitle='',
              zTitle='',
              barmode='',
              bargap=None,
              bargroupgap=None,
              gridcolor=None,
              zerolinecolor=None,
              margin=None,
              annotations=False,
              is3d=False,
              **kwargs):
    """
	Generates a plotly Layout

	Parameters:
	-----------
		theme : string
			Layout Theme
				solar
				pearl
				white
		title : string
			Chart Title
		xTitle : string
			X Axis Title
		yTitle : string
			Y Axis Title
		zTitle : string
			Z Axis Title
			Applicable only for 3d charts
		barmode : string 
			Mode when displaying bars
				group
				stack
				overlay
		bargap : float
			Sets the gap between bars
				[0,1)
			Applicabe for bar and histogram plots
		bargroupgap : float
			Set the gap between groups
				[0,1)
			Applicabe for bar and histogram plots			
		gridcolor : string
				grid color
		zerolinecolor : string
				zero line color
		margin : dict or tuple
				Dictionary (l,r,b,t) or
				Tuple containing the left,
				right, bottom and top margins
		annotations : dictionary
			Dictionary of annotations
			{x_point : text}
		is3d : bool
			Indicates if the layout is for a 3D chart
	"""

    for key in kwargs.keys():
        if key not in __LAYOUT_KWARGS:
            raise Exception("Invalid keyword : '{0}'".format(key))

    if not theme:
        theme = auth.get_config_file()['theme']

    size = None
    if annotations:
        if 'font' in annotations:
            if 'size' in annotations['font']:
                size = annotations['font']['size']

    def update_annotations(annotations, font_color, arrow_color):
        if annotations:
            if isinstance(annotations, dict):
                annotations = [annotations]
            for i in annotations:
                i.update(
                    dict(arrowcolor=arrow_color, font={'color': font_color}))

    if theme == 'ggplot':
        layout = Layout(legend=Legend(bgcolor='white',
                                      font={'color': 'grey10'}),
                        paper_bgcolor='white',
                        plot_bgcolor='#E5E5E5',
                        yaxis1=YAxis(tickfont={'color': 'grey10'},
                                     gridcolor='#F6F6F6',
                                     title=yTitle,
                                     titlefont={'color': 'grey10'},
                                     zerolinecolor='#F6F6F6'),
                        xaxis1=XAxis(tickfont={'color': 'grey10'},
                                     gridcolor='#F6F6F6',
                                     title=xTitle,
                                     titlefont={'color': 'grey10'},
                                     zerolinecolor='#F6F6F6',
                                     showgrid=True),
                        titlefont={'color': 'charcoal'})
        update_annotations(annotations, 'grey10', 'grey10')

    if theme == 'solar':
        layout = Layout(legend=Legend(bgcolor='charcoal',
                                      font={'color': 'pearl'}),
                        paper_bgcolor='charcoal',
                        plot_bgcolor='charcoal',
                        yaxis1=YAxis(tickfont={'color': 'grey12'},
                                     gridcolor='grey08',
                                     title=yTitle,
                                     titlefont={'color': 'pearl'},
                                     zerolinecolor='grey09'),
                        xaxis1=XAxis(tickfont={'color': 'grey12'},
                                     gridcolor='grey08',
                                     title=xTitle,
                                     titlefont={'color': 'pearl'},
                                     zerolinecolor='grey09'),
                        titlefont={'color': 'pearl'})
        update_annotations(annotations, 'pearl', 'grey11')

    elif theme == 'space':
        layout = Layout(legend=Legend(bgcolor='grey03',
                                      font={'color': 'pearl'}),
                        paper_bgcolor='grey03',
                        plot_bgcolor='grey03',
                        yaxis1=YAxis(tickfont={'color': 'grey12'},
                                     gridcolor='grey08',
                                     title=yTitle,
                                     titlefont={'color': 'pearl'},
                                     zerolinecolor='grey09'),
                        xaxis1=XAxis(tickfont={'color': 'grey12'},
                                     gridcolor='grey08',
                                     title=xTitle,
                                     titlefont={'color': 'pearl'},
                                     zerolinecolor='grey09'),
                        titlefont={'color': 'pearl'})
        update_annotations(annotations, 'pearl', 'red')

    elif theme == 'pearl':
        layout = Layout(
            legend=Legend(bgcolor='pearl02', font={'color': 'pearl06'}),
            paper_bgcolor='pearl02',
            plot_bgcolor='pearl02',
            yaxis1=YAxis(tickfont={'color': 'pearl06'},
                         gridcolor='pearl04' if is3d else 'pearl03',
                         title=yTitle,
                         titlefont={'color': 'pearl06'},
                         zeroline=False,
                         zerolinecolor='pearl04' if is3d else 'pearl03'),
            xaxis1=XAxis(tickfont={'color': 'pearl06'},
                         gridcolor='pearl04' if is3d else 'pearl03',
                         title=xTitle,
                         titlefont={'color': 'pearl06'},
                         zerolinecolor='pearl04' if is3d else 'pearl03'))
        update_annotations(annotations, 'pearl06', 'pearl04')

    elif theme == 'white':
        layout = Layout(
            legend=Legend(bgcolor='white', font={'color': 'pearl06'}),
            paper_bgcolor='white',
            plot_bgcolor='white',
            yaxis1=YAxis(tickfont={'color': 'pearl06'},
                         gridcolor='pearl04' if is3d else 'pearl03',
                         title=yTitle,
                         titlefont={'color': 'pearl06'},
                         zerolinecolor='pearl04' if is3d else 'pearl03'),
            xaxis1=XAxis(tickfont={'color': 'pearl06'},
                         gridcolor='pearl04' if is3d else 'pearl03',
                         title=xTitle,
                         titlefont={'color': 'pearl06'},
                         zerolinecolor='pearl04' if is3d else 'pearl03'))
        update_annotations(annotations, 'pearl06', 'pearl04')

    if barmode:
        layout.update({'barmode': barmode})
    if bargroupgap:
        layout.update({'bargroupgap': bargroupgap})
    if bargap:
        layout.update(bargap=bargap)
    if title:
        layout.update({'title': title})
    if annotations:
        if size:
            annotations['font']['size'] = size
        layout.update({'annotations': annotations})
    if gridcolor:
        for k in layout:
            if 'axis' in k:
                layout[k].update(gridcolor=normalize(gridcolor))
    if zerolinecolor:
        for k in layout:
            if 'axis' in k:
                layout[k].update(zerolinecolor=normalize(zerolinecolor))
    if margin:
        if isinstance(margin, dict):
            margin = margin
        else:
            margin = dict(zip(('l', 'r', 'b', 't'), margin))
        layout.update(margin=margin)

    if is3d:
        zaxis = layout['xaxis1'].copy()
        zaxis.update(title=zTitle)
        scene = Scene(xaxis1=layout['xaxis1'],
                      yaxis1=layout['yaxis1'],
                      zaxis=zaxis)
        layout.update(scene=scene)
        del layout['xaxis1']
        del layout['yaxis1']

    ## Kwargs

    if 'legend' in kwargs:
        layout['showlegend'] = kwargs['legend']

    # Shapes

    if any(k in kwargs
           for k in ['vline', 'hline', 'shapes', 'hspan', 'vspan']):
        shapes = []

        def get_shapes(xline):
            orientation = xline[0]
            xline = kwargs[xline]
            if isinstance(xline, list):
                for x_i in xline:
                    if isinstance(x_i, dict):
                        x_i['kind'] = 'line'
                        shapes.append(tools.get_shape(**x_i))
                    else:
                        if orientation == 'h':
                            shapes.append(tools.get_shape(kind='line', y=x_i))
                        else:
                            shapes.append(tools.get_shape(kind='line', x=x_i))
            elif isinstance(xline, dict):
                shapes.append(tools.get_shape(**xline))
            else:
                if orientation == 'h':
                    shapes.append(tools.get_shape(kind='line', y=xline))
                else:
                    shapes.append(tools.get_shape(kind='line', x=xline))

        def get_span(xspan):
            orientation = xspan[0]
            xspan = kwargs[xspan]
            if isinstance(xspan, list):
                for x_i in xspan:
                    if isinstance(x_i, dict):
                        x_i['kind'] = 'rect'
                        shapes.append(tools.get_shape(**x_i))
                    else:
                        v0, v1 = x_i
                        if orientation == 'h':
                            shapes.append(
                                tools.get_shape(kind='rect',
                                                y0=v0,
                                                y1=v1,
                                                fill=True,
                                                opacity=.5))
                        else:
                            shapes.append(
                                tools.get_shape(kind='rect',
                                                x0=v0,
                                                x1=v1,
                                                fill=True,
                                                opacity=.5))
            elif isinstance(xspan, dict):
                xspan['kind'] = 'rect'
                shapes.append(tools.get_shape(**xspan))
            elif isinstance(xspan, tuple):
                v0, v1 = xspan
                if orientation == 'h':
                    shapes.append(
                        tools.get_shape(kind='rect',
                                        y0=v0,
                                        y1=v1,
                                        fill=True,
                                        opacity=.5))
                else:
                    shapes.append(
                        tools.get_shape(kind='rect',
                                        x0=v0,
                                        x1=v1,
                                        fill=True,
                                        opacity=.5))
            else:
                raise Exception('Invalid value for {0}span: {1}'.format(
                    orientation, xspan))

        if 'hline' in kwargs:
            get_shapes('hline')
        if 'vline' in kwargs:
            get_shapes('vline')
        if 'hspan' in kwargs:
            get_span('hspan')
        if 'vspan' in kwargs:
            get_span('vspan')
        if 'shapes' in kwargs:
            shapes_ = kwargs['shapes']
            if isinstance(shapes_, list):
                for i in shapes_:
                    shp = i if 'type' in i else tools.get_shape(**i)
                    shapes.append(shp)
            elif isinstance(shapes_, dict):
                shp = shapes_ if 'type' in shapes_ else tools.get_shape(
                    **shapes_)
                shapes.append(shp)
            else:
                raise Exception(
                    "Shapes need to be either a dict or list of dicts")

        layout['shapes'] = shapes

    def updateColors(layout):
        for k, v in layout.items():
            if isinstance(v, dict):
                updateColors(v)
            else:
                if isinstance(v, list):
                    for _ in v:
                        if isinstance(_, dict):
                            updateColors(_)
                if 'color' in k.lower():
                    if 'rgba' not in v:
                        layout[k] = normalize(v)
        return layout

    return updateColors(layout)
Esempio n. 4
0
def get_shape(kind='line',x=None,y=None,x0=None,y0=None,x1=None,y1=None,span=0,color='red',dash='solid',width=1,
				fillcolor=None,fill=False,opacity=1,xref='x',yref='y'):
	"""
	Returns a plotly shape

	Parameters:
	-----------
		kind : string
			Shape kind
				line
				rect
				circle
		x : float 
			x values for the shape. 
			This assumes x0=x1
		x0 : float
			x0 value for the shape
		x1 : float
			x1 value for the shape
		y : float 
			y values for the shape. 
			This assumes y0=y1
		y0 : float
			y0 value for the shape
		y1 : float
			y1 value for the shape
		color : string
			color for shape line
		dash : string
			line style
				solid
				dash
				dashdot
				dot 
		width : int
			line width
		fillcolor : string
			shape fill color
		fill : bool
			If True then fill shape 
			If not fillcolor then the 
			line color will be used
		opacity : float [0,1]
			opacity of the fill 
		xref : string
			Sets the x coordinate system 
			which this object refers to
				'x'
				'paper'
				'x2' etc
		yref : string
			Sets the y coordinate system 
			which this object refers to
				'y'
				'paper'
				'y2' etc
	"""
	if not x1:
		if not x0:
			if not x:
				xref='paper'
				x0=0
				x1=1
			else:
				x0=x1=x
		else:
			x1=x0
	if not y1:
		if not y0:
			if not y:
				yref='paper'
				y0=0
				y1=1
			else:
				y0=y1=y
		else:
			y1=y0

	shape = {	'x0':x0,
				'y0':y0,
				'x1':x1,
				'y1':y1,
				'line' : {
					'color':normalize(color),
					'width':width,
					'dash':dash				
					},
				'xref':xref,
				'yref':yref
				}

	if kind=='line':	
		shape['type']='line'

	elif kind=='circle':
		shape['type']='circle'
		
	elif kind=='rect':
		shape['type']='rect'
	else:
		raise Exception("Invalid or unkown shape type : {0}".format(kind))

	if (fill or fillcolor) and kind!='line':
		fillcolor = color if not fillcolor else fillcolor
		fillcolor=to_rgba(normalize(fillcolor),opacity)
		shape['fillcolor']=fillcolor

	return shape
Esempio n. 5
0
def getLayout(theme=None,title='',xTitle='',yTitle='',zTitle='',barmode='',bargap=None,bargroupgap=None,
				gridcolor=None,zerolinecolor=None,margin=None,annotations=False,is3d=False,**kwargs):
	"""
	Generates a plotly Layout

	Parameters:
	-----------
		theme : string
			Layout Theme
				solar
				pearl
				white
		title : string
			Chart Title
		xTitle : string
			X Axis Title
		yTitle : string
			Y Axis Title
		zTitle : string
			Z Axis Title
			Applicable only for 3d charts
		barmode : string 
			Mode when displaying bars
				group
				stack
				overlay
		bargap : float
			Sets the gap between bars
				[0,1)
			Applicabe for bar and histogram plots
		bargroupgap : float
			Set the gap between groups
				[0,1)
			Applicabe for bar and histogram plots			
		gridcolor : string
				grid color
		zerolinecolor : string
				zero line color
		margin : dict or tuple
				Dictionary (l,r,b,t) or
				Tuple containing the left,
				right, bottom and top margins
		annotations : dictionary
			Dictionary of annotations
			{x_point : text}
		is3d : bool
			Indicates if the layout is for a 3D chart
	"""


	for key in kwargs.keys():
		if key not in __LAYOUT_KWARGS:
			raise Exception("Invalid keyword : '{0}'".format(key))
	
	if not theme:
		theme = auth.get_config_file()['theme']

	size=None
	if annotations:
		if 'font' in annotations:
			if 'size' in annotations['font']:
				size=annotations['font']['size']

	def update_annotations(annotations,font_color,arrow_color):
		if annotations:
			if isinstance(annotations,dict):
				annotations=[annotations]
			for i in annotations:
				i.update(dict(arrowcolor=arrow_color,font={'color':font_color}))

	if theme=='ggplot':
		layout=Layout(legend=Legend(bgcolor='white',font={'color':'grey10'}),
						paper_bgcolor='white',plot_bgcolor='#E5E5E5',
						yaxis1=YAxis(tickfont={'color':'grey10'},gridcolor='#F6F6F6',title=yTitle,
								 titlefont={'color':'grey10'},zerolinecolor='#F6F6F6'),
						xaxis1=XAxis(tickfont={'color':'grey10'},gridcolor='#F6F6F6',title=xTitle,
								titlefont={'color':'grey10'},zerolinecolor='#F6F6F6',showgrid=True),
						titlefont={'color':'charcoal'})
		update_annotations(annotations,'grey10','grey10')

	if theme=='solar':
		layout=Layout(legend=Legend(bgcolor='charcoal',font={'color':'pearl'}),
						paper_bgcolor='charcoal',plot_bgcolor='charcoal',
						yaxis1=YAxis(tickfont={'color':'grey12'},gridcolor='grey08',title=yTitle,
								 titlefont={'color':'pearl'},zerolinecolor='grey09'),
						xaxis1=XAxis(tickfont={'color':'grey12'},gridcolor='grey08',title=xTitle,
								titlefont={'color':'pearl'},zerolinecolor='grey09'),
						titlefont={'color':'pearl'})
		update_annotations(annotations,'pearl','grey11')

	elif theme=='space':
		layout=Layout(legend=Legend(bgcolor='grey03',font={'color':'pearl'}),
						paper_bgcolor='grey03',plot_bgcolor='grey03',
						yaxis1=YAxis(tickfont={'color':'grey12'},gridcolor='grey08',title=yTitle,
								 titlefont={'color':'pearl'},zerolinecolor='grey09'),
						xaxis1=XAxis(tickfont={'color':'grey12'},gridcolor='grey08',title=xTitle,
								titlefont={'color':'pearl'},zerolinecolor='grey09'),
						titlefont={'color':'pearl'})
		update_annotations(annotations,'pearl','red')

	elif theme=='pearl':
		layout=Layout(legend=Legend(bgcolor='pearl02',font={'color':'pearl06'}),
						paper_bgcolor='pearl02',plot_bgcolor='pearl02',
						yaxis1=YAxis(tickfont={'color':'pearl06'},gridcolor='pearl04' if is3d else 'pearl03',title=yTitle,
								  titlefont={'color':'pearl06'},zeroline=False,zerolinecolor='pearl04' if is3d else 'pearl03'),
						xaxis1=XAxis(tickfont={'color':'pearl06'},gridcolor='pearl04' if is3d else 'pearl03',title=xTitle,
								  titlefont={'color':'pearl06'},zerolinecolor='pearl04' if is3d else 'pearl03'))
		update_annotations(annotations,'pearl06','pearl04')

	elif theme=='white':
		layout=Layout(legend=Legend(bgcolor='white',font={'color':'pearl06'}),
						paper_bgcolor='white',plot_bgcolor='white',
						yaxis1=YAxis(tickfont={'color':'pearl06'},gridcolor='pearl04' if is3d else 'pearl03',title=yTitle,
								  titlefont={'color':'pearl06'},zerolinecolor='pearl04' if is3d else 'pearl03'),
						xaxis1=XAxis(tickfont={'color':'pearl06'},gridcolor='pearl04' if is3d else 'pearl03',title=xTitle,
								  titlefont={'color':'pearl06'},zerolinecolor='pearl04' if is3d else 'pearl03'))
		update_annotations(annotations,'pearl06','pearl04')
	
	if barmode:
		layout.update({'barmode':barmode})
	if bargroupgap:
		layout.update({'bargroupgap':bargroupgap})
	if bargap:
		layout.update(bargap=bargap)
	if title:
		layout.update({'title':title})
	if annotations:
		if size:
			annotations['font']['size']=size
		layout.update({'annotations':annotations})
	if gridcolor:
		for k in layout:
			if 'axis' in k:
				layout[k].update(gridcolor=normalize(gridcolor))
	if zerolinecolor:
		for k in layout:
			if 'axis' in k:
				layout[k].update(zerolinecolor=normalize(zerolinecolor))
	if margin:
		if isinstance(margin,dict):
			margin=margin
		else:
			margin=dict(zip(('l','r','b','t'),margin))
		layout.update(margin=margin)

	if is3d:
		zaxis=layout['xaxis1'].copy()
		zaxis.update(title=zTitle)
		scene=Scene(xaxis1=layout['xaxis1'],yaxis1=layout['yaxis1'],zaxis=zaxis)
		layout.update(scene=scene)
		del layout['xaxis1']
		del layout['yaxis1']


	## Kwargs

	if 'legend' in kwargs:
		layout['showlegend']=kwargs['legend']


	# Shapes 

	if any(k in kwargs for k in ['vline','hline','shapes','hspan','vspan']):
		shapes=[]

		def get_shapes(xline):
			orientation=xline[0]
			xline=kwargs[xline]
			if isinstance(xline,list):
				for x_i in xline:
					if isinstance(x_i,dict):
						x_i['kind']='line'
						shapes.append(tools.get_shape(**x_i))
					else:						
						if orientation=='h':
							shapes.append(tools.get_shape(kind='line',y=x_i))
						else:
							shapes.append(tools.get_shape(kind='line',x=x_i))
			elif isinstance(xline,dict):
				shapes.append(tools.get_shape(**xline))
			else:
				if orientation=='h':
					shapes.append(tools.get_shape(kind='line',y=xline))			
				else:
					shapes.append(tools.get_shape(kind='line',x=xline))			

		def get_span(xspan):
			orientation=xspan[0]
			xspan=kwargs[xspan]
			if isinstance(xspan,list):
				for x_i in xspan:
					if isinstance(x_i,dict):
						x_i['kind']='rect'
						shapes.append(tools.get_shape(**x_i))
					else:
						v0,v1=x_i
						if orientation=='h':
							shapes.append(tools.get_shape(kind='rect',y0=v0,y1=v1,fill=True,opacity=.5))
						else:
							shapes.append(tools.get_shape(kind='rect',x0=v0,x1=v1,fill=True,opacity=.5))
			elif isinstance(xspan,dict):
				xspan['kind']='rect'
				shapes.append(tools.get_shape(**xspan))
			elif isinstance(xspan,tuple):
				v0,v1=xspan
				if orientation=='h':
					shapes.append(tools.get_shape(kind='rect',y0=v0,y1=v1,fill=True,opacity=.5))
				else:
					shapes.append(tools.get_shape(kind='rect',x0=v0,x1=v1,fill=True,opacity=.5))
			else:
				raise Exception('Invalid value for {0}span: {1}'.format(orientation,xspan))

		if 'hline' in kwargs:
			get_shapes('hline')
		if 'vline' in kwargs:
			get_shapes('vline')
		if 'hspan' in kwargs:
			get_span('hspan')
		if 'vspan' in kwargs:
			get_span('vspan')
		if 'shapes' in kwargs:
			shapes_=kwargs['shapes']
			if isinstance(shapes_,list):
				for i in shapes_:
					shp=i if 'type' in i else tools.get_shape(**i)
					shapes.append(shp)
			elif isinstance(shapes_,dict):
					shp=shapes_ if 'type' in shapes_ else tools.get_shape(**shapes_)
					shapes.append(shp)
			else:
				raise Exception("Shapes need to be either a dict or list of dicts")


		layout['shapes']=shapes

	def updateColors(layout):
		for k,v in layout.items():
			if isinstance(v,dict):
				updateColors(v)
			else:
				if isinstance(v,list):
					for _ in v:
						if isinstance(_,dict):
							updateColors(_)
				if 'color' in k.lower():
					if 'rgba' not in v:
						layout[k]=normalize(v)
		return layout
	
	return updateColors(layout)