Skip to content

Commit 6ac0812

Browse files
author
AdrianoDee
committed
Add Chart.js gauge plots
1 parent 21cfe01 commit 6ac0812

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

Utilities/RelMon/python/definitions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,8 @@
197197
("!(mu20+|wzMu20+|jet20+)","Btag@1"))
198198
data_pattern_blist_pairs=()
199199

200+
## colors for gauge
201+
202+
from matplotlib.colors import LinearSegmentedColormap
203+
gauge_cmap=LinearSegmentedColormap.from_list('rg',["r", "orange","y","lime"], N=256)
204+

Utilities/RelMon/python/directories2html.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import os
2626
import hashlib
27+
import random
2728

2829
if "RELMON_SA" in os.environ:
2930
from .dirstructure import Comparison,Directory
@@ -492,27 +493,44 @@ def directory2html(directory, hashing, standalone, depth=0):
492493

493494
#chdir(old_cwd)
494495

495-
#-------------------------------------------------------------------------------
496+
def to_rgba(t):
497+
''' Convert a RGBA tuple to string'''
498+
return "rgba(%f,%f,%f,%f)"%(t)
499+
500+
def build_gauge_js(rate,w=100,minrate=.80):
501+
502+
color = to_rgba(gauge_cmap(rate-minrate))
503+
font_size = int(w/5)
504+
text_offs = int(w/8)
505+
gauge_max = 1. - rate
506+
507+
name = random.getrandbits(64) # just a random has for the canvas
508+
html = ""
509+
html += '<div style="width:100%%;max-width:%d; position: relative;">'%(w)
510+
html += '<div style="width: 100%%; position: absolute; top: 75%%; margin-top: -%dpx; text-align: center;">'%(text_offs)
511+
html += '<span style="color: %s; font-family: courier; font-size: %dpx;">%.2f%%</span>'%(color,font_size,rate*100.)
512+
html += '<canvas id="%s"></canvas>'%(name)
513+
514+
# "gauge" chart
515+
html += '<script> new Chart("%s",'%(name)
516+
html += '{ type: "doughnut",'
496517

497-
def build_gauge(total_success_rate,minrate=.80,small=False,escaped=False):
498-
total_success_rate_scaled=(total_success_rate-minrate)
499-
total_success_rate_scaled_repr=total_success_rate_scaled/(1-minrate)
500-
if total_success_rate_scaled_repr<0:
501-
total_success_rate_scaled_repr=0
502-
size_s="200x100"
503-
if small:
504-
size_s="40x30"
505-
#print "Total success rate %2.2f and scaled %2.2f "%(total_success_rate,total_success_rate_scaled)
506-
gauge_link ="https://chart.googleapis.com/chart?chs=%s&cht=gom"%size_s
507-
gauge_link+="&chd=t:%2.1f"%(total_success_rate_scaled_repr*100.)
508-
if not small:
509-
gauge_link+="&chxt=x,y&chxl=0:|%2.1f%%|1:|%i%%|%i%%|100%%"%(total_success_rate*100,minrate*100.,(1+minrate)*50)
510-
gauge_link+="&chma=10,10,10,0"
511-
img_tag= '<img src="%s">'%gauge_link
512-
if escaped:
513-
img_tag=html.escape(img_tag)
514-
return img_tag
518+
# data
519+
html += 'data: {'
520+
html += 'datasets: [{ backgroundColor: ["%s", "rgba(0, 0, 0, 0.1)"],'%(color)
521+
html += 'data: [%f,%f]}] },'%(rate,gauge_max)
515522

523+
#options
524+
html += 'options: { responsive: true, rotation: -90, circumference: 180 },'
525+
526+
html += '}); </script> </div> </div>'
527+
528+
print("here")
529+
530+
img_link="https://upload.wikimedia.org/wikipedia/commons/a/a8/Circle_Davys-Grey_Solid.svg"
531+
html='<img src="%s" title="%s" height=50 width=50>' %(img_link,"test")
532+
533+
return html
516534
#-------------------------------------------------------------------------------
517535

518536
def get_aggr_pairs_info(dir_dict,the_aggr_pairs=[]):
@@ -615,7 +633,7 @@ def make_categories_summary(dir_dict,aggregation_rules):
615633
html+='</div>'
616634

617635
html+='<div class="span-6 last">'
618-
html+=build_gauge(average_success_rate)
636+
html+=build_gauge_js(average_success_rate)
619637
html+='</div>'
620638

621639
html+='<hr>'
@@ -649,7 +667,7 @@ def make_twiki_table(dir_dict,aggregation_rules):
649667

650668
for cat_name,present_subdirs,total_weight,average_success_rate in aggr_pairs_info:
651669
#print cat_name,present_subdirs,total_weight,average_success_rate
652-
html+=build_gauge(average_success_rate,small=True,escaped=True)
670+
html+=build_gauge_js(average_success_rate,w=40)
653671
html+=" | "
654672

655673
html+='</div> <a href="#top">Top...</a>'

Utilities/RelMon/python/dirstructure.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,14 @@ def get_subdirs_names(self):
168168
subdirnames.append(subdir.name)
169169
return subdirnames
170170

171-
def get_summary_chart_ajax(self,w=400,h=300):
172-
"""Emit the ajax to build a pie chart using google apis...
173-
"""
174-
url = "https://chart.googleapis.com/chart?"
175-
url+= "cht=p3" # Select the 3d chart
176-
#url+= "&chl=Success|Null|Fail" # give labels
177-
url+= "&chco=00FF00|FFFF00|FF0000|7A7A7A" # give colours to labels
178-
url+= "&chs=%sx%s" %(w,h)
179-
#url+= "&chtt=%s" %self.name
180-
url+= "&chd=t:%.2f,%.2f,%.2f,%.2f"%(self.get_success_rate(),self.get_null_rate(),self.get_fail_rate(),self.get_skiped_rate())
181-
182-
return url
183-
184171
def get_piechart_js(self,w=400,link=None,title=None):
185172
"""
186173
Build the HTML snippet to render a piechart with chart.js
187174
"""
188175

189176
name = random.getrandbits(64) # just a random has for the canvas
190177
html = ""
191-
html += '<canvas id="%s" style="max-width:%d"></canvas>'%(name,w)
178+
html += '<canvas id="%s" style="width:100%%;max-width:%d"></canvas>'%(name,w)
192179
# piechart
193180
html += '<script> new Chart("%s",'%(name)
194181
html += '{ type: "pie",'

0 commit comments

Comments
 (0)