-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathfigure.js
More file actions
306 lines (268 loc) · 12.9 KB
/
Copy pathfigure.js
File metadata and controls
306 lines (268 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : figure.js
* Author : Black Logic
* Note : Library Component
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 11. 18
* Change Date :
*/
//============================================================================
// [CLASS] Figure
//============================================================================
define([
'vp_base/js/com/component/LibraryComponent',
'vp_base/js/com/component/VarSelector'
], function(LibraryComponent, VarSelector) {
/**
* Figure
*/
class Figure extends LibraryComponent {
_init() {
super._init();
this.state = {
subplotsRows: '',
subplotsCols: '',
o0: 'figure',
figsize: '',
...this.state
}
this.package = {
name: 'plt.subplots()',
code: '${o0}, ax = plt.subplots(${subplotsRows}, ${subplotsCols}${v})',
input: [
{
name: 'subplotsRows',
type: 'int',
label: 'Number of Rows',
component: 'input_single'
},
{
name: 'subplotsCols',
type: 'int',
label: 'Number of Columns',
component: 'input_single'
}
],
output: [
{
name: 'o0',
type: 'var',
label: 'Figure Variable',
component: 'input_single',
required: true
}
],
variable: [
{
name: 'figsize',
type: 'var'
}
]
};
}
_bindEvent() {
super._bindEvent();
var that = this;
// add subplot box on changing subplots row/column
$(this.wrapSelector('#subplotsRows')).change(function() {
var row = $(that.wrapSelector('#subplotsRows')).val() * 1;
var col = $(that.wrapSelector('#subplotsCols')).val() * 1;
// nothing entered, set 1
if (row == 0) row = 1;
if (col == 0) col = 1;
$(that.wrapSelector('#subplotsRowsRange')).val(row);
$(that.wrapSelector('#vp_subplotsGrid')).attr('data-row', row);
$(that.wrapSelector('#vp_subplotsGrid')).css('height', 80 * row + 'px');
$(that.wrapSelector('#vp_subplotsGrid')).css('grid-template-rows', 'repeat(' + row + ', 1fr)');
$(that.wrapSelector('#vp_subplotsGrid')).html('');
for (var i = 0; i < row * col; i++) {
var div = document.createElement('div');
var r = parseInt(i / col);
var c = i % col;
$(div).attr({
'class': 'grid-item',
'data-idx': i,
'data-row': r,
'data-col': c
});
var position = i;
if (row > 1 && col > 1) {
position = r + ', ' + c;
}
$(div).text('[' + position + ']');
$(div).click(function(evt) { that.subplotBoxClickHandler(that, evt); });
$(that.wrapSelector('#vp_subplotsGrid')).append(div);
}
// initialize subplot value
that.selectedIdx = '0';
if (row > 1 && col > 1) {
that.selectedIdx = '0, 0';
}
that.subplotOption = [];
// add space for subplot
for (var i = 0; i < row * col; i++) {
that.subplotOption.push({idx: i});
}
});
$(this.wrapSelector('#subplotsCols')).change(function() {
var row = $(that.wrapSelector('#subplotsRows')).val() * 1;
var col = $(that.wrapSelector('#subplotsCols')).val() * 1;
// nothing entered, set 1
if (row == 0) row = 1;
if (col == 0) col = 1;
$(that.wrapSelector('#subplotsColsRange')).val(col);
$(that.wrapSelector('#vp_subplotsGrid')).attr('data-col', col);
$(that.wrapSelector('#vp_subplotsGrid')).css('width', 80 * col + 'px');
$(that.wrapSelector('#vp_subplotsGrid')).css('grid-template-columns', 'repeat(' + col + ', 1fr)');
$(that.wrapSelector('#vp_subplotsGrid')).html('');
for (var i = 0; i < row * col; i++) {
var div = document.createElement('div');
var r = parseInt(i / col);
var c = i % col;
$(div).attr({
'class': 'grid-item',
'data-idx': i,
'data-row': r,
'data-col': c
});
var position = i;
if (row > 1 && col > 1) {
position = r + ', ' + c;
}
$(div).text('[' + position + ']');
$(div).click(function(evt) { that.subplotBoxClickHandler(that, evt); });
$(that.wrapSelector('#vp_subplotsGrid')).append(div);
}
// initialize subplot value
that.selectedIdx = '0';
if (row > 1 && col > 1) {
that.selectedIdx = '0, 0';
}
that.subplotOption = [];
// add space for subplot
for (var i = 0; i < row * col; i++) {
that.subplotOption.push({idx: i});
}
});
// subplot 위치 버튼 클릭 시 해당 subplot의 옵션 설정을 위해 다음 페이지로 이동
// 참고 : vpContainer.js > tabPageShow($(this).index());
// - #vp_subplot span[data-caption-id="vp_functionDetail"]
// - #vp_subplotOptional span[data-caption-id="vp_functionDetail"]
$(this.wrapSelector('#vp_subplotsGrid div')).click(function (evt) {
that.subplotBoxClickHandler(that, evt);
});
// range 변경 시 값 표시
$(this.wrapSelector('#subplotsRowsRange')).change(function() {
var value = $(this).val();
$(that.wrapSelector('#subplotsRows')).val(value);
$(that.wrapSelector('#subplotsRows')).change();
});
$(this.wrapSelector('#subplotsColsRange')).change(function() {
var value = $(this).val();
$(that.wrapSelector('#subplotsCols')).val(value);
$(that.wrapSelector('#subplotsCols')).change();
});
}
bindCmapSelector() {
// 기존 cmap 선택하는 select 태그 안보이게
var cmapSelector = this.wrapSelector('#cmap');
$(cmapSelector).hide();
// cmap 데이터로 팔레트 div 동적 구성
this.cmap.forEach(ctype => {
var divColor = document.createElement('div');
$(divColor).attr({
'class': 'vp-plot-cmap-item',
'data-cmap': ctype,
'data-url': 'pandas/cmap/' + ctype + '.JPG',
'title': ctype
});
$(divColor).text(ctype);
// 이미지 url 바인딩
var url = Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.RESOURCE_PATH + 'pandas/cmap/' + ctype + '.JPG';
$(divColor).css({
'background-image' : 'url(' + url + ')'
})
var selectedCmap = this.wrapSelector('#vp_selectedCmap');
// 선택 이벤트 등록
$(divColor).click(function() {
if (!$(this).hasClass('selected')) {
$(this).parent().find('.vp-plot-cmap-item.selected').removeClass('selected');
$(this).addClass('selected');
// 선택된 cmap 이름 표시
$(selectedCmap).text(ctype);
// 선택된 cmap data-caption-id 변경
$(selectedCmap).attr('data-caption-id', ctype);
// select 태그 강제 선택
$(cmapSelector).val(ctype).prop('selected', true);
}
});
$(this.wrapSelector('#vp_plotCmapSelector')).append(divColor);
});
// 선택 이벤트
$(this.wrapSelector('.vp-plot-cmap-wrapper')).click(function() {
$(this).toggleClass('open');
});
}
templateForBody() {
return `
<div class="vp-grid-box">
<div class="vp-grid-box vp-grid-col-95">
<label for="o0" class="vp-orange-text">Allocate to</label>
<input type="text" class="vp-input input-single" id="o0" index=0 value="fig"/>
</div>
<div class="vp-grid-box vp-grid-col-95">
<label for="subplotsRows">Row</label>
<div class="vp-grid-col-p50">
<input type="range" class="subplot-range" id="subplotsRowsRange" min="0" max="30" value="1"/>
<input type="number" class="vp-input s input-range number-only" id="subplotsRows" index=0 placeholder="row" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" value="1"/>
</div>
</div>
<div class="vp-grid-box vp-grid-col-95">
<label for="subplotsCols">Column</label>
<div class="vp-grid-col-p50">
<input type="range" class="subplot-range" id="subplotsColsRange" min="0" max="30" value="1"/>
<input type="number" class="vp-input s input-range number-only" id="subplotsCols" index=1 placeholder="column" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" value="1"/>
</div>
</div>
<div class="vp-grid-box vp-grid-col-95">
<label for="figsize">Figure Size</label>
<input type="text" class="vp-input input-single" id="figsize" index=0 placeholder="(width, height)"/>
</div>
</div>`;
}
render() {
super.render();
// add var selector
var varSelector = new VarSelector(['DataFrame', 'Series', 'Index'], 'DataFrame', false);
varSelector.setComponentId('i0');
varSelector.addClass('vp-state');
varSelector.setUseColumn(true);
varSelector.setValue(this.state.i0);
$(this.wrapSelector('#i0')).replaceWith(varSelector.render());
}
subplotBoxClickHandler(that, event) {
var target = event.target;
var parent = $(target).parent();
// 다음 옵션 페이지 이동
var thisPageIdx = $(target).closest(vpConst.OPTION_PAGE).index();
vpContainer.tabPageShow(thisPageIdx + 1);
// 다음 옵션 페이지의 위치 설명란에 [0,0] 표기
var row = $(target).data('row');
var col = $(target).data('col');
// 표기 : row나 col이 하나라도 1이면 1차원, 그 이상이면 2차원 배열로 접근
var position = $(target).data('idx');
var rowLen = $(parent).data('row') * 1;
var colLen = $(parent).data('col') * 1;
if (rowLen > 1 && colLen > 1) { // TODO: parent().data-row / data-col 받아와야댐
position = row + ', ' + col;
}
// 선택한 서브플롯 인덱스 설정
that.selectedIdx = position + '';
$(that.wrapSelector('span[data-caption-id="pageTitle"]'))[1].innerText = 'Subplot [' + position + '] Setting';
$(that.wrapSelector('span[data-caption-id="pageTitle"]'))[2].innerText = 'Subplot [' + position + '] Add Chart';
}
}
return Figure;
});