Commit 57df52e3 authored by Sanjay Yadav's avatar Sanjay Yadav

BUG: Fixed heatgrid color/na-color/na-text and cell width and height from YAML

parent 12c17ec8
Pipeline #31917 failed with stage
in 52 seconds
......@@ -1080,7 +1080,6 @@ def heatgrid(shape, spec, data):
width = shape.width
pixel_inch = 10000
default_height = 20
fifteen_percent = 0.15
height = spec.get('cell-height', default_height) * pixel_inch
parent = shape._parent
shape.element.delete()
......@@ -1090,20 +1089,16 @@ def heatgrid(shape, spec, data):
for key in ['row', 'column', 'value']:
if isinstance(spec[key], (dict,)) and 'function' in spec[key]:
spec[key] = compile_function(spec, key, data, handler)
if isinstance(spec.get('width'), (dict,)) and 'function' in spec['width']:
spec['width'] = compile_function(spec, 'width', data, handler)
# Loading data
data = compile_function(spec, 'data', data, handler)
data = data.sort_values(by=[spec['column']])
rows = data[spec['row']].unique().tolist()
columns = sorted(data[spec['column']].unique().tolist())
twenty_percent = (width * fifteen_percent)
left_margin = (width * spec.get('left-margin', 0.15))
padding = spec.get('style', {}).get('padding', 10)
padding = padding * pixel_inch
gradient = spec.get('style', {}).get('gradient', 'RdYlGn')
styles = copy.deepcopy(spec.get('style', {}))
if styles.get('gradient'):
......@@ -1113,30 +1108,39 @@ def heatgrid(shape, spec, data):
if isinstance(styles.get(key), (dict,)) and 'function' in styles[key]:
styles[key] = compile_function(styles, key, data, handler)
# Calculating cell's width based on config
_width = (width - left_margin) / float(len(columns)) / pixel_inch
_width = spec.get('cell-width', _width) * pixel_inch
# Adding Columns to the HeatGrid.
for idx, column in enumerate(columns):
txt = parent.add_textbox(
left + ((width - twenty_percent) / len(columns)) * idx + twenty_percent,
top - height - padding, ((width - twenty_percent) / len(columns)), height)
left + _width * idx + left_margin, top - height - padding, _width, height)
add_text_to_shape(txt, '{}'.format(column), **styles)
# Cell width
_width = (width - twenty_percent) / float(len(columns)) / pixel_inch
_width = spec.get('cell-width', _width) * pixel_inch
for index, row in enumerate(rows):
_data = data[data[spec['row']] == row].reset_index(drop=True)
_data = data[data[spec['row']] == row].dropna()
if len(_data) < len(columns):
_data = pd.merge(
_data, pd.DataFrame({spec['column']: list(columns)}),
left_on=spec['column'], right_on=spec['column'], how='outer')
_data = _data.sort_values(by=[spec['column']]).reset_index(drop=True)
for _idx, _row in _data.iterrows():
style = copy.deepcopy(styles)
# Adding cells
xaxis = left + (_width * _idx) + twenty_percent
xaxis = left + (_width * _idx) + left_margin
yaxis = top + (height * index) + padding * index
_rect = rect(parent, xaxis, yaxis, _width - padding, height)
# Adding color gradient to cell if gradient is True
if style.get('gradient'):
grad_txt = scale_data(_row['value'], _min, _max)
grad_txt = scale_data(_row[spec['value']], _min, _max)
gradient = matplotlib.cm.get_cmap(spec['style']['gradient'])
style['fill'] = matplotlib.colors.to_hex(gradient(grad_txt))
style['color'] = _color.contrast(style['fill'])
if np.isnan(_row[spec['value']]) and spec.get('na-color'):
style['fill'] = spec.get('na-color')
style['color'] = _color.contrast(style['fill'])
rect_css(_rect, **style)
# Adding text to cells if required.
if spec.get('text'):
......@@ -1145,11 +1149,13 @@ def heatgrid(shape, spec, data):
cell_txt = '{}'.format(_row[spec['value']])
if callable(spec['text']):
cell_txt = spec['text'](_row)
if np.isnan(_row[spec['value']]) and spec.get('na-text'):
cell_txt = spec.get('na-text')
add_text_to_shape(_txt, cell_txt, **style)
# Adding row's text in left side
txt = parent.add_textbox(
left, top + (height * index) + padding * index,
_width + twenty_percent, height)
_width + left_margin, height)
add_text_to_shape(txt, row, **styles)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment