mirror of https://github.com/apache/superset.git
Merge pull request #123 from mistercrunch/color_factory
Adding a color factory
This commit is contained in:
commit
b472cad5d1
|
@ -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;
|
||||
|
|
|
@ -12,6 +12,7 @@ import parsedatetime
|
|||
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 @@ class JSONEncodedDict(TypeDecorator):
|
|||
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():
|
||||
|
|
|
@ -532,10 +532,11 @@ class BubbleViz(NVD3Viz):
|
|||
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 @@ class NVD3TimeSeriesViz(NVD3Viz):
|
|||
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 @@ class NVD3TimeSeriesViz(NVD3Viz):
|
|||
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 @@ class DistributionPieViz(NVD3Viz):
|
|||
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 @@ class DistributionBarViz(DistributionPieViz):
|
|||
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)]
|
||||
|
|
Loading…
Reference in New Issue