Adding a color factory

This commit is contained in:
Maxime Beauchemin 2016-01-22 15:25:56 -08:00
parent 2f6e971463
commit 2df3cfd774
3 changed files with 35 additions and 18 deletions

View File

@ -6,6 +6,7 @@ function viz_world_map(slice) {
var render = function() { var render = function() {
var container = slice.container; var container = slice.container;
var div = d3.select(slice.selector); var div = d3.select(slice.selector);
container.css('height', slice.height());
d3.json(slice.jsonEndpoint(), function(error, json){ d3.json(slice.jsonEndpoint(), function(error, json){
var fd = json.form_data; var fd = json.form_data;

View File

@ -12,6 +12,7 @@ import parsedatetime
from panoramix import db from panoramix import db
class memoized(object): class memoized(object):
"""Decorator that caches a function's return value each time it is called. """Decorator that caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned, and If called later with the same arguments, the cached value is returned, and
@ -104,22 +105,16 @@ class JSONEncodedDict(TypeDecorator):
return value return value
def color(s): class ColorFactory(object):
""" BNB_COLORS = [
Get a consistent color from the same string using a hash function
>>> color("foo")
'#FF5A5F'
"""
colors = [
"#007A87", "#007A87",
"#00D1C1", "#00D1C1",
"#4EDED2",
"#4FA3AB",
"#565A5C", "#565A5C",
"#7B0051", "#7B0051",
"#898C8C", "#898C8C",
"#8CE071", "#8CE071",
"#4EDED2",
"#4FA3AB",
"#9CA299", "#9CA299",
"#A14D83", "#A14D83",
"#B4A76C", "#B4A76C",
@ -130,10 +125,27 @@ def color(s):
"#FFC4B3", "#FFC4B3",
"#FFCA4F", "#FFCA4F",
] ]
s = s.encode('utf-8')
h = hashlib.md5(s) def __init__(self, hash_based=True):
i = int(h.hexdigest(), 16) self.d = {}
return colors[i % len(colors)] self.hash_based = hash_based
def get(self, s):
"""
Get a consistent color from the same string using a hash function
>>> color("foo")
'#FF5A5F'
"""
if self.hash_based:
s = s.encode('utf-8')
h = hashlib.md5(s)
i = int(h.hexdigest(), 16)
else:
if s not in self.d:
self.d[s] = len(self.d)
i = self.d[s]
return self.BNB_COLORS[i % len(self.BNB_COLORS)]
def init(): def init():

View File

@ -532,10 +532,11 @@ class BubbleViz(NVD3Viz):
for row in df.to_dict(orient='records'): for row in df.to_dict(orient='records'):
series[row['group']].append(row) series[row['group']].append(row)
chart_data = [] chart_data = []
cf = utils.ColorFactory()
for k, v in series.items(): for k, v in series.items():
chart_data.append({ chart_data.append({
'key': k, 'key': k,
"color": utils.color(str(k)), "color": cf.get(str(k)),
'values': v }) 'values': v })
return dumps(chart_data) return dumps(chart_data)
@ -691,6 +692,7 @@ class NVD3TimeSeriesViz(NVD3Viz):
series = df.to_dict('series') series = df.to_dict('series')
chart_data = [] chart_data = []
cf = utils.ColorFactory()
for name in df.T.index.tolist(): for name in df.T.index.tolist():
ys = series[name] ys = series[name]
if df[name].dtype.kind not in "biufc": if df[name].dtype.kind not in "biufc":
@ -704,7 +706,7 @@ class NVD3TimeSeriesViz(NVD3Viz):
series_title = ", ".join(name) series_title = ", ".join(name)
else: else:
series_title = ", ".join(name[1:]) series_title = ", ".join(name[1:])
color = utils.color(series_title) color = cf.get(series_title)
if title_suffix: if title_suffix:
series_title += title_suffix series_title += title_suffix
@ -810,7 +812,8 @@ class DistributionPieViz(NVD3Viz):
df = self.get_df() df = self.get_df()
df = df.reset_index() df = df.reset_index()
df.columns = ['x', 'y'] df.columns = ['x', 'y']
df['color'] = map(utils.color, df.x) cf = utils.ColorFactory()
df['color'] = map(cf.get, df.x)
return dumps(df.to_dict(orient="records")) return dumps(df.to_dict(orient="records"))
@ -852,9 +855,10 @@ class DistributionBarViz(DistributionPieViz):
series_title = ", ".join(name) series_title = ", ".join(name)
else: else:
series_title = ", ".join(name[1:]) series_title = ", ".join(name[1:])
cf = utils.ColorFactory()
d = { d = {
"key": series_title, "key": series_title,
"color": utils.color(series_title), "color": cf.get(series_title),
"values": [ "values": [
{'x': ds, 'y': ys[i]} {'x': ds, 'y': ys[i]}
for i, ds in enumerate(df.timestamp)] for i, ds in enumerate(df.timestamp)]