Skip to content

Commit 882d71f

Browse files
author
minjk-bl
committed
Apps > Groupby - enable Grouper only with datetime column
1 parent 6d01555 commit 882d71f

File tree

2 files changed

+81
-33
lines changed

2 files changed

+81
-33
lines changed

src/common/component/vpColumnSelector.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ define([
9797
var colList = [];
9898
if (colTags.length > 0) {
9999
for (var i = 0; i < colTags.length; i++) {
100-
var colValue = $(colTags[i]).data('code');
101-
if (colValue) {
102-
colList.push(colValue);
100+
var colName = $(colTags[i]).data('colname');
101+
var colDtype = $(colTags[i]).data('dtype');
102+
var colCode = $(colTags[i]).data('code');
103+
if (colCode) {
104+
colList.push({ name: colName, dtype: colDtype, code: colCode});
103105
}
104106
}
105107
}

src/common/vpGroupby.js

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ define([
139139
} = state;
140140

141141
$(this._wrapSelector('#vp_gbVariable')).val(variable);
142-
$(this._wrapSelector('#vp_gbBy')).val(groupby.join(','));
142+
$(this._wrapSelector('#vp_gbBy')).val(groupby.map(col=>col.code).join(','));
143143
$(this._wrapSelector('#vp_gbBy')).data('list', groupby);
144-
$(this._wrapSelector('#vp_gbByGrouper')).prop('checked', useGrouper);
145-
$(this._wrapSelector('#vp_gbByGrouperNumber')).val(grouperNumber);
146-
$(this._wrapSelector('#vp_gbByGrouperPeriod')).val(grouperPeriod);
147-
$(this._wrapSelector('#vp_gbDisplay')).val(display.join(','));
144+
if (useGrouper) {
145+
$(this._wrapSelector('#vp_gbByGrouper')).removeAttr('disabled');
146+
$(this._wrapSelector('#vp_gbByGrouper')).prop('checked', useGrouper);
147+
$(this._wrapSelector('#vp_gbByGrouperNumber')).val(grouperNumber);
148+
$(this._wrapSelector('#vp_gbByGrouperPeriod')).val(grouperPeriod);
149+
$(this._wrapSelector('.vp-gb-by-grouper-box')).show();
150+
}
151+
$(this._wrapSelector('#vp_gbDisplay')).val(display.map(col=>col.code).join(','));
148152
$(this._wrapSelector('#vp_gbDisplay')).data('list', display);
149153
$(this._wrapSelector('#vp_gbMethod')).val(method);
150154
$(this._wrapSelector('#vp_gbMethodSelector')).val(method);
@@ -303,7 +307,7 @@ define([
303307
page.appendFormatLine('<label for="{0}" class="{1}">{2}</label>', 'vp_gbBy', 'vp-orange-text wp80', 'Groupby');
304308
page.appendFormatLine('<input type="text" id="{0}" placeholder="{1}" disabled/>', 'vp_gbBy', 'Groupby coluns');
305309
page.appendFormatLine('<button id="{0}" class="{1}">{2}</button>', 'vp_gbBySelect', 'vp-button wp50', 'Edit');
306-
page.appendFormatLine('<label style="display: none;"><input type="checkbox" id="{0}"/><span>{1}</span></label>', 'vp_gbByGrouper', 'Grouper');
310+
page.appendFormatLine('<label><input type="checkbox" id="{0}" disabled/><span>{1}</span></label>', 'vp_gbByGrouper', 'Grouper');
307311
page.appendFormatLine('<div class="{0}" style="display:none;">', 'vp-gb-by-grouper-box');
308312
page.appendFormatLine('<input type="number" id="{0}" class="{1}"/>', 'vp_gbByGrouperNumber', 'vp-gb-by-number');
309313
page.appendFormatLine('<select id="{0}">', 'vp_gbByGrouperPeriod');
@@ -531,7 +535,11 @@ define([
531535
openInnerPopup(targetSelector, title='Select columns', includeList=[]) {
532536
this.popup.type = 'column';
533537
this.popup.targetSelector = targetSelector;
534-
this.renderColumnSelector(this.popup.targetSelector.data('list'), includeList);
538+
var previousList = this.popup.targetSelector.data('list');
539+
if (previousList) {
540+
previousList = previousList.map(col => col.code)
541+
}
542+
this.renderColumnSelector(previousList, includeList);
535543

536544
// set title
537545
$(this._wrapSelector('.' + APP_POPUP_BOX + ' .' + APP_TITLE)).text(title);
@@ -664,11 +672,12 @@ define([
664672
$(document).on('change', this._wrapSelector('#vp_gbBy'), function(event) {
665673
var colList = event.colList;
666674
that.state.groupby = colList;
667-
668-
if (colList && colList.length == 1) {
669-
$(that._wrapSelector('#vp_gbByGrouper')).parent().show();
675+
676+
if (colList && colList.length == 1
677+
&& colList[0].dtype.includes('datetime')) {
678+
$(that._wrapSelector('#vp_gbByGrouper')).removeAttr('disabled');
670679
} else {
671-
$(that._wrapSelector('#vp_gbByGrouper')).parent().hide();
680+
$(that._wrapSelector('#vp_gbByGrouper')).attr('disabled', true);
672681
}
673682
});
674683

@@ -763,20 +772,38 @@ define([
763772
var idx = $(that._wrapSelector('.vp-gb-adv-col')).index(this);
764773

765774
// if there's change, reset display namings
766-
var previousList = that.state.advColList[idx];
767-
if (!previousList || colList.length !== previousList.length
768-
|| !colList.slice().sort().every((val, idx) => { return val === previousList.slice().sort()[idx]})) {
769-
that.state.advNamingList = []
770-
$(that._wrapSelector('.vp-gb-adv-naming')).val('');
771-
$(that._wrapSelector('.vp-gb-adv-naming')).data('dict', {});
775+
// var previousList = that.state.advColList[idx];
776+
// if (!previousList || colList.length !== previousList.length
777+
// || !colList.map(col=>col.code).slice().sort().every((val, idx) => {
778+
// return val === previousList.map(col=>col.code).slice().sort()[idx]
779+
// })) {
780+
// that.state.advNamingList = []
781+
// $(this).parent().find('.vp-gb-adv-naming').val('');
782+
// $(this).parent().find('.vp-gb-adv-naming').data('dict', {});
783+
// }
784+
var namingDict = that.state.advNamingList[idx];
785+
if (namingDict) {
786+
// namingDict = namingDict.filter(key => colList.map(col=>col.code).includes(key));
787+
Object.keys(namingDict).forEach(key => {
788+
if (!colList.map(col=>col.code).includes(key)) {
789+
delete namingDict[key];
790+
}
791+
});
792+
that.state.advNamingList[idx] = namingDict;
793+
$(this).parent().find('.vp-gb-adv-naming').val(Object.values(namingDict).map(val => "'" + val +"'").join(','));
794+
$(this).parent().find('.vp-gb-adv-naming').data('dict', namingDict);
772795
}
773796

774797
that.state.advColList[idx] = colList;
775798
});
776799

777800
// edit target columns
778801
$(document).on('click', this._wrapSelector('.vp-gb-adv-col-selector'), function() {
779-
that.openInnerPopup($(this).parent().find('.vp-gb-adv-col'), 'Select columns', that.state.display);
802+
var includeList = that.state.display;
803+
if (includeList && includeList.length > 0) {
804+
includeList = includeList.map(col => col.code);
805+
}
806+
that.openInnerPopup($(this).parent().find('.vp-gb-adv-col'), 'Select columns', includeList);
780807
});
781808

782809
// select method
@@ -815,6 +842,9 @@ define([
815842
$(document).on('click', this._wrapSelector('.vp-gb-adv-naming-selector'), function() {
816843
var parentDiv = $(this).parent();
817844
var columns = $(parentDiv).find('.vp-gb-adv-col').data('list');
845+
if (columns && columns.length > 0) {
846+
columns = columns.map(col => col.code);
847+
}
818848
var method = $(parentDiv).find('.vp-gb-adv-method').val();
819849
if (!method || method == '' || method == "''") {
820850
// set focus on selecting method tag
@@ -899,7 +929,7 @@ define([
899929
if (that.popup.type == 'column') {
900930
var colList = that.popup.ColSelector.getColumnList();
901931

902-
$(that.popup.targetSelector).val(colList.join(','));
932+
$(that.popup.targetSelector).val(colList.map(col => { return col.code }).join(','));
903933
$(that.popup.targetSelector).data('list', colList);
904934
$(that.popup.targetSelector).trigger({ type: 'change', colList: colList });
905935
that.closeInnerPopup();
@@ -911,13 +941,11 @@ define([
911941
var key = $(tags[i]).data('code');
912942
var val = $(tags[i]).val();
913943
if (val && val != '') {
914-
dict[key] = "'" + val + "'";
944+
dict[key] = val;
915945
}
916946
}
917947

918-
console.log(dict);
919-
920-
$(that.popup.targetSelector).val(Object.values(dict).join(','));
948+
$(that.popup.targetSelector).val(Object.values(dict).map(val => "'" + val +"'").join(','));
921949
$(that.popup.targetSelector).data('dict', dict);
922950
$(that.popup.targetSelector).trigger({ type: 'change', namingDict: dict });
923951
that.closeInnerPopup();
@@ -980,6 +1008,10 @@ define([
9801008
display, method, advanced, allocateTo, resetIndex
9811009
} = this.state;
9821010

1011+
// mapping colList states
1012+
groupby = groupby.map(col => col.code);
1013+
display = display.map(col => col.code);
1014+
9831015
//====================================================================
9841016
// Allocation
9851017
//====================================================================
@@ -1000,9 +1032,8 @@ define([
10001032
// Grouper
10011033
if (useGrouper) {
10021034
byStr = vpCommon.formatString("pd.Grouper(key={0}, freq='{1}')", byStr, grouperNumber + grouperPeriod);
1003-
}
1004-
1005-
if (resetIndex == true) {
1035+
} else if (resetIndex == true) {
1036+
// as_index option cannot use with Grouper -> use .reset_index() at the end
10061037
byStr += ', as_index=False';
10071038
}
10081039
// variable & groupby columns & option
@@ -1039,6 +1070,9 @@ define([
10391070
}
10401071
for (var i = 0; i < advItemTags.length; i++) {
10411072
var advColumns = $(advItemTags[i]).find('.vp-gb-adv-col').data('list');
1073+
if (advColumns && advColumns.length > 0) {
1074+
advColumns = advColumns.map(col => col.code);
1075+
}
10421076
var advMethod = $(advItemTags[i]).find('.vp-gb-adv-method').val();
10431077
var advNaming = $(advItemTags[i]).find('.vp-gb-adv-naming').data('dict');
10441078
if (!advMethod || advMethod == '' || advMethod == "''") {
@@ -1047,6 +1081,9 @@ define([
10471081
if (advColumns && advColumns.length > 0) {
10481082
advColumns.forEach(col => {
10491083
var naming = advNaming[col];
1084+
if (naming && naming != '') {
1085+
naming = "'" + naming + "'";
1086+
}
10501087
if (Object.keys(advColumnDict).includes(col)) {
10511088
advColumnDict[col].push({ method: advMethod, naming: naming})
10521089
} else {
@@ -1055,7 +1092,11 @@ define([
10551092
});
10561093

10571094
} else {
1058-
advColumnDict['nothing'].push({ method: advMethod, naming: advNaming[advMethod]});
1095+
var naming = advNaming[advMethod];
1096+
if (naming && naming != '') {
1097+
naming = "'" + naming + "'";
1098+
}
1099+
advColumnDict['nothing'].push({ method: advMethod, naming: naming});
10591100
}
10601101
}
10611102

@@ -1065,7 +1106,7 @@ define([
10651106
var noColList = advColumnDict['nothing'];
10661107
if (noColList.length == 1) {
10671108
// 1 method
1068-
if (noColList[0].naming && noColList[0].naming != undefined) {
1109+
if (noColList[0].naming && noColList[0].naming != '') {
10691110
methodStr.appendFormat("[({0}, {1})]", noColList[0].naming, noColList[0].method);
10701111
} else {
10711112
methodStr.appendFormat("{0}", noColList[0].method);
@@ -1074,7 +1115,7 @@ define([
10741115
// more than 1 method
10751116
var tmpList = [];
10761117
noColList.forEach(obj => {
1077-
if (obj.naming && obj.naming != undefined) {
1118+
if (obj.naming && obj.naming != '') {
10781119
tmpList.push(vpCommon.formatString("({0}, {1})", obj.naming, obj.method));
10791120
} else {
10801121
tmpList.push(obj.method);
@@ -1106,7 +1147,7 @@ define([
11061147
var tmpList2 = [];
11071148
var useTuple = false;
11081149
colList.forEach(obj => {
1109-
if (obj.naming && obj.naming != undefined) {
1150+
if (obj.naming && obj.naming != '') {
11101151
tmpList2.push(vpCommon.formatString("({0}, {1})", obj.naming, obj.method));
11111152
useTuple = true;
11121153
} else {
@@ -1129,6 +1170,11 @@ define([
11291170
//================================================================
11301171
methodStr.appendFormat('{0}()', method);
11311172
}
1173+
1174+
// when using as_index option with Grouper, use .reset_index()
1175+
if (useGrouper && resetIndex) {
1176+
methodStr.append('.reset_index()');
1177+
}
11321178
// display columns
11331179
code.appendFormat('{0}.{1}', colStr, methodStr.toString());
11341180

0 commit comments

Comments
 (0)