Skip to content

Commit

Permalink
sorting added
Browse files Browse the repository at this point in the history
  • Loading branch information
stevewirts committed Feb 5, 2015
1 parent 89c5932 commit 793960e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 58 deletions.
108 changes: 78 additions & 30 deletions behaviors/fin-hypergrid-behavior-qtree.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
//* v sort descending
//* |^| sort absolute value ascending
//* |v| sort absolute value descending
var sortMap = {
a: '^',
d: 'v',
A: '|^|',
D: '|v|'
};

var sortStates = {
n: 'a',
a: 'd',
d: 'A',
A: 'D',
};

Polymer({ /* jslint ignore:line */
ready: function() {
Expand All @@ -57,7 +70,6 @@
};
this.readyInit();
this.sorted = {};
this.sortStates = ['', ' ^', ' v', ' |^|', ' |v|'];
this.ws = null;
this.reconnect();
},
Expand Down Expand Up @@ -164,16 +176,19 @@

//return the column names, they are available to us as meta data in the most recent page Q sent us.
getFixedRowValue: function(x) {
var headers = this.block.H;
x = this.translateColumnIndex(x);
var col = headers[x];
if (!this.sorted[x + 1]) {
this.sorted[x + 1] = 0;
}
var sortIndicator = this.sortStates[this.sorted[x + 1]];
return col + ' ' + sortIndicator;
var colId = this.getColumnId(x);
var sortIndicator = this.getSortIndicator(colId);
return colId + ' ' + sortIndicator;
},

getSortIndicator: function(colId) {
var sortIndex = this.block.S.cols.indexOf(colId);
if (sortIndex < 0) return '';
var sortState = this.block.S.sorts[sortIndex];
var symbol = sortMap[sortState];
var state = symbol + ' ' + (sortIndex + 1);
return state;
},
// h_ is the text of the hierarchy column.
// l_ is the level of the row of the table.
// e_ tells you whether the row is a leaf or a node.
Expand Down Expand Up @@ -203,9 +218,7 @@

//let Q decide if this instance is sortable or not
getCanSort: function() {
return false; // for now.....
var canSort = this.block.features.sorting === true;
return canSort;
return true
},

//on a header click do a sort!
Expand All @@ -215,28 +228,63 @@

//first ask q if this is a sortable instance, then send a message to Q to sort our data set
toggleSort: function(columnIndex) {
var columnIndex = this.translateColumnIndex(columnIndex);
var colId = this.getColumnId(columnIndex);
if (!this.getCanSort()) {
return;
}
columnIndex++;
var current = this.sorted[columnIndex];
var stateCount = this.sortStates.length;
this.sorted = {}; //clear out other sorted for now, well add multicolumn sort later
this.sorted[columnIndex] = (current + 1) % stateCount;
var state = this.sortStates[this.sorted[columnIndex]];
var message = {
cmd: 'sortTable',
data: {
table: 'trade',
sort: current === (stateCount - 1) ? '' : this.block.headers[columnIndex][0],
asc: state.indexOf('^') > 0,
abs: state.indexOf('|') > 0,
start: this.scrollPositionY,
num: 60
}
var sortBlob = this.block.S;
var sortIndex = sortBlob.cols.indexOf(colId);

//lets get the current state or 'n' if it doesn't exist yet
var currentState = sortBlob.sorts[sortIndex] || 'n';

//lets set to the next state or undefined
var newState = sortStates[currentState];

//remove this column from it's current order position
if (sortIndex > -1) {
sortBlob.cols.splice(sortIndex, 1);
sortBlob.sorts.splice(sortIndex, 1);
}

//push to the front the new state
if (newState) {
sortBlob.cols.unshift(colId);
sortBlob.sorts.unshift(newState);
}

//ony 3 nested sorts allowed for now
sortBlob.cols.length = sortBlob.sorts.length = Math.min(3, sortBlob.cols.length);

//lets tell Q now
var msg = {
id: "abc",
fn: "sorts",
cols: sortBlob.cols,
sorts: sortBlob.sorts
};
this.ws.send(JSON.stringify(message));

msg.sorts[colId] = 'a';
this.ws.send(JSON.stringify(msg));

// columnIndex++;
// var current = this.sorted[columnIndex];
// var stateCount = this.sortStates.length;
// this.sorted = {}; //clear out other sorted for now, well add multicolumn sort later
// this.sorted[columnIndex] = (current + 1) % stateCount;
// var state = this.sortStates[this.sorted[columnIndex]];
// var message = {
// cmd: 'sortTable',
// data: {
// table: 'trade',
// sort: current === (stateCount - 1) ? '' : this.block.headers[columnIndex][0],
// asc: state.indexOf('^') > 0,
// abs: state.indexOf('|') > 0,
// start: this.scrollPositionY,
// num: 60
// }
// };
// this.ws.send(JSON.stringify(message));
},

//delegate column alignment through the map at the top based on the column type
Expand Down
4 changes: 2 additions & 2 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</paper-dropdown>
</paper-menu-button>

<paper-tabs selected="0" flex>
<paper-tabs selected="4" flex>
<paper-tab>In Memory</paper-tab>
<paper-tab>Default</paper-tab>
<paper-tab>JSON</paper-tab>
Expand All @@ -130,7 +130,7 @@

</core-toolbar>

<core-pages selected="0">
<core-pages selected="4">

<div vertical layout>
<div class="description">
Expand Down
14 changes: 7 additions & 7 deletions fin-hypergrid-cell-provider.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@
valignOffset = valignOffset + Math.ceil(height / 2);

//fill background only if our bgColor is populated or we are a selected cell
if (this.config.bgColor || this.config.isSelected) {
gc.fillStyle = this.config.isSelected ? this.config.bgSelColor : this.config.bgColor;
gc.fillRect(x, y, width, height);
}
if (this.config.isSelected) {
gc.fillStyle = this.config.isSelected ? this.config.bgSelColor : this.config.bgColor;
gc.fillRect(x, y, width, height);
}

//draw text
gc.fillStyle = this.config.isSelected ? this.config.fgSelColor : this.config.fgColor;
Expand Down Expand Up @@ -221,9 +221,9 @@
var valignOffset = Math.ceil(height / 2);

//fill background only if our bgColor is populated or we are a selected cell
if (this.config.bgColor || this.config.isSelected) {
gc.fillStyle = this.config.isSelected ? this.config.bgSelColor : this.config.bgColor;
gc.fillRect(x, y, width, height);
if (this.config.isSelected) {
gc.fillStyle = this.config.isSelected ? this.config.bgSelColor : this.config.bgColor;
gc.fillRect(x, y, width, height);
}

gc.fillStyle = this.config.isSelected ? this.config.fgSelColor : this.config.fgColor;
Expand Down
2 changes: 1 addition & 1 deletion fin-hypergrid-renderer.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
config.y = y;
config.value = value;
config.fgColor = fgColor;
config.bgColor = constants.backgroundColor === bgColor ? null : bgColor;
config.bgColor = bgColor; //
config.fgSelColor = fgSelColor;
config.bgSelColor = bgSelColor;
config.font = font;
Expand Down
2 changes: 1 addition & 1 deletion fin-hypergrid.min.html

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions q/t.q
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ at:{[b;p;g;n]p,([n:enlist(count[n]#g)!n,()]v:enlist b)}
tsort:{[t;o]
n:exec n_ from t;
i:children[parent n]except enlist();
j:msort[0!t;key o;(`a`d!(iasc;idesc))get o]each i;
f:{x((abs;lower)type[y]in 10 11h)y};
j:msort[0!t;key o;(`a`d`A`D!(iasc;idesc;f iasc;f idesc))get o]each i;
n?pmesh over n j}

/ parent-vector -> child-list
Expand Down Expand Up @@ -162,7 +163,7 @@ $[.z.K<3.3;
.js.upd:{.js.snd .js.set()!()}
.js.set:{`Z`Z_ set'.tt.cons[T;G;P;A;S]H;.js.ret x}
.js.sub:{[z]flip each(1#;1_)@\:.tt.rows[R]z}
.js.obj:{`Z`Z_`G`G_`H`H_`Q`S`R`N!(.js.sub Z;.js.sub Z_;G;where["S"=q]except G;H;cols[T]except H;q:.tt.qtype T;S;R;count Z)}
.js.obj:{`Z`Z_`G`G_`H`H_`Q`S`R`N!(.js.sub Z;.js.sub Z_;G;where["S"=q]except G;H;cols[T]except H;q:.tt.qtype T;`cols`sorts!(key S;get S);R;count Z)}
.js.ret:{x,.js.obj[]}

// globals
Expand Down Expand Up @@ -196,29 +197,20 @@ trader:`chico`harpo`groucho`zeppo`moe`larry`curly`shemp`abbott`costello
sector:`energy`materials`industrials`financials`healthcare`utilities`infotech
strategy:`statarb`pairs`mergerarb`house`chart`indexarb

n:100000
n:1000000
T:([tradeId:til n]
holdingId:n?holdingId;
symbol:n?symbol;
sector:n?sector;
trader:n?trader;
strategy:n?strategy;
time:09:30:00.0+n?23000000;
price:50+.23*n?400;
quantity:(100*10+n?20)-2000;
date:2000.01.01+asc n?365;
price1:50+.23*n?400;
amount1:n?1.0;
price2:50+.23*n?400;
amount2:(20*til n)_(n*20)?100;
date2:2000.01.01+asc n?365;
price3:50+.23*n?400;
amount3:(20*til n)_(n*20)?100;
date3:2000.01.01+asc n?365;
price4:50+.23*n?400;
amount4:100*10+n?20;
date4:2000.01.01+asc n?365;
amount5:100*10+n?20)
time:09:30:00.0+n?23000000)

G:`sector`trader`strategy
H:`symbol`price`quantity
H:cols[T]except G,keys T
A[`price]:(avg;`price)

/ show .js.set()!();

0 comments on commit 793960e

Please sign in to comment.