Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a color factory #123

Merged
merged 1 commit into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions panoramix/static/widgets/viz_world_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function viz_world_map(slice) {
var render = function() {
var container = slice.container;
var div = d3.select(slice.selector);
container.css('height', slice.height());

d3.json(slice.jsonEndpoint(), function(error, json){
var fd = json.form_data;
Expand Down
40 changes: 26 additions & 14 deletions panoramix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from panoramix import db



class memoized(object):
"""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
Expand Down Expand Up @@ -104,22 +105,16 @@ def process_result_value(self, value, dialect):
return value


def color(s):
"""
Get a consistent color from the same string using a hash function

>>> color("foo")
'#FF5A5F'
"""
colors = [
class ColorFactory(object):
BNB_COLORS = [
"#007A87",
"#00D1C1",
"#4EDED2",
"#4FA3AB",
"#565A5C",
"#7B0051",
"#898C8C",
"#8CE071",
"#4EDED2",
"#4FA3AB",
"#9CA299",
"#A14D83",
"#B4A76C",
Expand All @@ -130,10 +125,27 @@ def color(s):
"#FFC4B3",
"#FFCA4F",
]
s = s.encode('utf-8')
h = hashlib.md5(s)
i = int(h.hexdigest(), 16)
return colors[i % len(colors)]

def __init__(self, hash_based=True):
self.d = {}
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():
Expand Down
12 changes: 8 additions & 4 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,11 @@ def get_json_data(self):
for row in df.to_dict(orient='records'):
series[row['group']].append(row)
chart_data = []
cf = utils.ColorFactory()
for k, v in series.items():
chart_data.append({
'key': k,
"color": utils.color(str(k)),
"color": cf.get(str(k)),
'values': v })
return dumps(chart_data)

Expand Down Expand Up @@ -691,6 +692,7 @@ def to_series(self, df, classed='', title_suffix=''):
series = df.to_dict('series')

chart_data = []
cf = utils.ColorFactory()
for name in df.T.index.tolist():
ys = series[name]
if df[name].dtype.kind not in "biufc":
Expand All @@ -704,7 +706,7 @@ def to_series(self, df, classed='', title_suffix=''):
series_title = ", ".join(name)
else:
series_title = ", ".join(name[1:])
color = utils.color(series_title)
color = cf.get(series_title)
if title_suffix:
series_title += title_suffix

Expand Down Expand Up @@ -810,7 +812,8 @@ def get_json_data(self):
df = self.get_df()
df = df.reset_index()
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"))


Expand Down Expand Up @@ -852,9 +855,10 @@ def get_json_data(self):
series_title = ", ".join(name)
else:
series_title = ", ".join(name[1:])
cf = utils.ColorFactory()
d = {
"key": series_title,
"color": utils.color(series_title),
"color": cf.get(series_title),
"values": [
{'x': ds, 'y': ys[i]}
for i, ds in enumerate(df.timestamp)]
Expand Down