-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathChartSetting.js
More file actions
168 lines (148 loc) · 6.79 KB
/
ChartSetting.js
File metadata and controls
168 lines (148 loc) · 6.79 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : ChartSetting.js
* Author : Black Logic
* Note : Visualization > Setting
* License : GNU GPLv3 with Visual Python special exception
* Date : 2022. 03. 21
* Change Date :
*/
//============================================================================
// [CLASS] Chart Setting
//============================================================================
define([
__VP_TEXT_LOADER__('vp_base/html/m_visualize/chartSetting.html'), // INTEGRATION: unified version of text loader
'vp_base/js/com/com_String',
'vp_base/js/com/com_util',
'vp_base/js/com/component/PopupComponent',
'vp_base/js/com/component/SuggestInput'
], function(csHTML, com_String, com_util, PopupComponent, SuggestInput) {
class ChartSetting extends PopupComponent {
_init() {
super._init();
this.config.size = { width: 500, height: 400 };
this.config.importButton = true;
this.config.dataview = false;
this.state = {
figureWidth: '12',
figureHeight: '8',
styleSheet: '',
fontName: '',
fontSize: '10',
...this.state
}
}
_bindEvent() {
super._bindEvent();
let that = this;
// setting popup - set default
$(this.wrapSelector('#setDefault')).on('change', function() {
let checked = $(this).prop('checked');
if (checked) {
// disable input
$(that.wrapSelector('.vp-chart-setting-body input')).prop('disabled', true);
} else {
// enable input
$(that.wrapSelector('.vp-chart-setting-body input')).prop('disabled', false);
}
});
}
templateForBody() {
let page = $(csHTML);
return page;
}
render() {
super.render();
let that = this;
//====================================================================
// Stylesheet suggestinput
//====================================================================
var stylesheetTag = $(this.wrapSelector('#styleSheet'));
// search available stylesheet list
var code = new com_String();
// FIXME: convert it to kernelApi
code.appendLine('import matplotlib.pyplot as plt');
code.appendLine('import json');
code.append(`print(json.dumps([{ 'label': s, 'value': s } for s in plt.style.available]))`);
vpKernel.execute(code.toString()).then(function(resultObj) {
let { result } = resultObj;
// get available stylesheet list
var varList = JSON.parse(result);
var suggestInput = new SuggestInput();
suggestInput.setComponentID('styleSheet');
suggestInput.addClass('vp-input vp-state');
suggestInput.setSuggestList(function() { return varList; });
suggestInput.setPlaceholder('style name');
suggestInput.setValue(that.state.styleSheet);
// suggestInput.setNormalFilter(false);
$(stylesheetTag).replaceWith(function() {
return suggestInput.toTagString();
});
});
//====================================================================
// System font suggestinput
//====================================================================
var fontFamilyTag = $(this.wrapSelector('#fontName'));
// search system font list
var code = new com_String();
// FIXME: convert it to kernelApi
code.appendLine('import json');
code.appendLine("import matplotlib.font_manager as fm");
code.appendLine("_ttflist = fm.fontManager.ttflist");
code.append("print(json.dumps([{'label': f.name, 'value': f.name } for f in _ttflist]))");
vpKernel.execute(code.toString()).then(function(resultObj) {
let { result } = resultObj;
// get available font list
var varList = JSON.parse(result);
var suggestInput = new SuggestInput();
suggestInput.setComponentID('fontName');
suggestInput.addClass('vp-input vp-state');
suggestInput.setSuggestList(function() { return varList; });
suggestInput.setPlaceholder('font name');
suggestInput.setValue(that.state.fontName);
// suggestInput.setNormalFilter(false);
$(fontFamilyTag).replaceWith(function() {
return suggestInput.toTagString();
});
});
}
generateImportCode() {
var code = new com_String();
code.appendLine('import matplotlib.pyplot as plt');
code.appendLine('%matplotlib inline');
code.appendLine('import seaborn as sns');
return [code.toString()];
}
generateCode() {
var code = new com_String();
// get parameters
let setDefault = $(this.wrapSelector('#setDefault')).prop('checked');
if (setDefault == true) {
code.appendLine('from matplotlib import rcParams, rcParamsDefault');
code.append('rcParams.update(rcParamsDefault)');
} else {
// get parameters
let { figureWidth, figureHeight, styleSheet, fontName, fontSize } = this.state;
code.appendLine('import matplotlib.pyplot as plt');
code.appendLine('%matplotlib inline');
code.appendLine('import seaborn as sns');
if (styleSheet && styleSheet.length > 0) {
code.appendFormatLine("plt.style.use('{0}')", styleSheet);
}
code.appendFormatLine("plt.rc('figure', figsize=({0}, {1}))", figureWidth, figureHeight);
code.appendLine();
code.appendLine('from matplotlib import rcParams');
if (fontName && fontName.length > 0) {
code.appendFormatLine("rcParams['font.family'] = '{0}'", fontName);
}
if (fontSize && fontSize.length > 0) {
code.appendFormatLine("rcParams['font.size'] = {0}", fontSize);
}
code.append("rcParams['axes.unicode_minus'] = False");
}
return code.toString();
}
}
return ChartSetting;
});