Skip to content

Commit

Permalink
Allowing for normalizing across x or y
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jan 28, 2016
1 parent 0714dc6 commit 4e6e20d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
8 changes: 8 additions & 0 deletions panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def __init__(self, viz):
'black_white']),
default='fire',
description=""),
'normalize_across': SelectField(
'Normalize Across', choices=self.choicify([
'heatmap', 'x', 'y']),
default='heatmap',
description=(
"Color will be rendered based on a ratio "
"of the cell against the sum of across this "
"criteria")),
'canvas_image_rendering': SelectField(
'Rendering', choices=(
('pixelated', 'pixelated (Sharp)'),
Expand Down
6 changes: 5 additions & 1 deletion panoramix/static/panoramix.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ var color = function(){
// Returns a linear scaler our of an array of color
if(!Array.isArray(colors))
colors = spectrums[colors];
var ext = d3.extent(data, accessor);
if(data !== undefined)
var ext = d3.extent(data, accessor);
else
var ext = [0,1];

var points = [];
var chunkSize = (ext[1] - ext[0]) / colors.length;
$.each(colors, function(i, c){
Expand Down
8 changes: 4 additions & 4 deletions panoramix/static/widgets/viz_heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ px.registerViz('heatmap', function(slice) {
var height = slice.height();
var hmWidth = width - (margins.l + margins.r)
var hmHeight = height - (margins.b + margins.t)
var fp = d3.format('.3p');
d3.json(slice.jsonEndpoint(), function(error, payload) {
var matrix = {};
if (error){
Expand Down Expand Up @@ -39,9 +40,7 @@ px.registerViz('heatmap', function(slice) {
var X = 0, Y = 1;
var heatmapDim = [xRbScale.domain().length, yRbScale.domain().length];

var color = px.color.colorScalerFactory(
fd.linear_color_scheme, data, function(d){return d.v});

var color = px.color.colorScalerFactory(fd.linear_color_scheme);

var scale = [
d3.scale.linear()
Expand Down Expand Up @@ -99,6 +98,7 @@ px.registerViz('heatmap', function(slice) {
s += "<div><b>" + fd.all_columns_x + ": </b>" + obj.x + "<div>"
s += "<div><b>" + fd.all_columns_y +": </b>" + obj.y + "<div>"
s += "<div><b>" + fd.metric + ": </b>" + obj.v + "<div>"
s += "<div><b>%: </b>" + fp(obj.perc) + "<div>"
return s;
}
})
Expand Down Expand Up @@ -146,7 +146,7 @@ px.registerViz('heatmap', function(slice) {
image = context.createImageData(heatmapDim[0], heatmapDim[1]);
var pixs = {};
$.each(data, function(i, d) {
var c = d3.rgb(color(d.v));
var c = d3.rgb(color(d.perc));
var x = xScale(d.x);
var y = yScale(d.y);
pixs[x + (y*xScale.domain().length)] = c;
Expand Down
18 changes: 18 additions & 0 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ class HeatmapViz(BaseViz):
'linear_color_scheme',
('xscale_interval', 'yscale_interval'),
'canvas_image_rendering',
'normalize_across',
)
},)
def query_obj(self):
Expand All @@ -1233,6 +1234,23 @@ def get_json_data(self):
else:
df = df[[x, y, v]]
df.columns = ['x', 'y', 'v']
norm = fd.get('normalize_across')
overall = False
if norm == 'heatmap':
overall = True
else:
gb = df.groupby(norm, group_keys=False)
if len(gb) <= 1:
overall = True
else:
df['perc'] = (
gb.apply(
lambda x: (x.v - x.v.min()) / (x.v.max() - x.v.min()))
)
if overall:
v = df.v
min_ = v.min()
df['perc'] = (v - min_) / (v.max() - min_)
return df.to_json(orient="records")


Expand Down

0 comments on commit 4e6e20d

Please sign in to comment.