diff --git a/panoramix/static/widgets/viz_world_map.js b/panoramix/static/widgets/viz_world_map.js index 0ae8799d95a2..998ae4742866 100644 --- a/panoramix/static/widgets/viz_world_map.js +++ b/panoramix/static/widgets/viz_world_map.js @@ -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; diff --git a/panoramix/utils.py b/panoramix/utils.py index 0334d91aee64..70c8146a1c5e 100644 --- a/panoramix/utils.py +++ b/panoramix/utils.py @@ -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 @@ -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", @@ -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(): diff --git a/panoramix/viz.py b/panoramix/viz.py index e2cfb7214560..1ffffa33bc5b 100644 --- a/panoramix/viz.py +++ b/panoramix/viz.py @@ -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) @@ -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": @@ -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 @@ -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")) @@ -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)]