-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhenotype
420 lines (393 loc) · 16.7 KB
/
Phenotype
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-Content-Type-Options" content="nosniff">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Energy Grass Phenotype Analysis</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/papaparse.min.js"></script>
<style>
/* 页面整体的样式设置,包括字体和颜色 */
* {
font-family: Arial, sans-serif;
color: black;
}
body {
/* 设置 body 的字体和颜色 */
font-family: Arial, sans-serif;
color: black;
}
select, button, label, input {
/* 设置表单元素的字体和颜色 */
font-family: Arial, sans-serif;
color: black;
}
.chart-container {
/* 设置图表容器的字体和颜色 */
font-family: Arial, sans-serif;
color: black;
}
h3 {
/* 设置标题的字体和颜色 */
font-family: Arial, sans-serif;
color: black;
}
</style>
<script>
// 定义下载SVG文件的函数
function downloadSVG(svgData, filename) {
// 创建一个 a 标签用于下载
const a = document.createElement('a');
a.href = svgData;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
</script>
</head>
<body>
<div class="container">
<div class="content">
<div class="sidebar">
<div class="search-container" style="margin-bottom: 20px;">
<!-- 选择年份的下拉框 -->
<select id="year-select" title="Select Year">
<option value="">Select Year</option>
</select>
<!-- 选择类型的下拉框 -->
<select id="type-select" title="Select Type">
<option value="">Select Type</option>
</select>
</div>
<h3>Filter Options</h3>
<div id="trait-filters">
<!-- 特征复选框将在此处动态添加 -->
</div>
<!-- 更新图表的按钮 -->
<button onclick="updateCharts()">Update Charts</button>
</div>
<div class="main-content">
<div class="chart-container">
<h3>Pairplot</h3>
<div id="pairplotContainer"></div>
</div>
<div class="chart-container">
<h3>Grouped Bar Chart</h3>
<canvas id="groupedBarChartCanvas"></canvas>
</div>
</div>
</div>
</div>
<script>
let csvData = [];
// 定义可用的特征列表
const traits = [
{id: 'Ht', name: 'Plant Height (Ht)'},
{id: 'CC', name: 'Compressed Circumference (CC)'},
{id: 'BC', name: 'Basal Circumference (BC)'},
{id: 'CC/BC', name: 'Compressed Circumference to Basal Circumference Ratio (CC/BC)'},
{id: 'YPP', name: 'Yield per Plant (YPP)'},
{id: 'YPF', name: 'Yield per Footprint (YPF)'},
{id: 'DBI', name: 'Culm Basal Internode Diameter (DBI)'},
{id: 'DTI', name: 'Culm Topmost Internode Diameter (DTI)'},
{id: 'CmDen', name: 'Culm Density (CmDen)'},
{id: 'CmDW', name: 'Culm Dry Weight (CmDW)'},
{id: 'CmNN', name: 'Culm Node Number (CmNN)'},
{id: 'CmVol', name: 'Culm Volume (CmVol)'}
];
let years = new Set();
let types = new Set();
let charts = {};
// 当文档加载完成时,加载CSV数据
document.addEventListener('DOMContentLoaded', function() {
loadCSV();
});
// 加载CSV文件
function loadCSV() {
// 设置CSV文件的路径
const filePath = 'http://localhost:8000/202401018phenotype.csv';
// 设置 PapaParse 的本地和远程块大小
Papa.LocalChunkSize = 1024 * 1024 * 5;
Papa.RemoteChunkSize = 1024 * 1024 * 5;
// 解析CSV文件
Papa.parse(filePath, {
download: true,
header: true,
skipEmptyLines: true,
complete: function(results) {
csvData = results.data;
processCSVData();
}
});
}
// 处理CSV数据,将年份和类型添加到集合中
function processCSVData() {
csvData.forEach(row => {
if (row['Year']) years.add(row['Year'].trim());
if (row['Type']) types.add(row['Type'].trim());
});
populateFilters();
populateDropdowns();
}
// 动态生成特征复选框
function populateFilters() {
const traitFilters = document.getElementById('trait-filters');
// 根据特征列表动态创建复选框
traitFilters.innerHTML = traits.map(trait =>
`<label><input type="checkbox" value="${trait.id}" id="input-id-{random_id}" name="input-name"> ${trait.name}</label><br>`
).join('');
}
// 填充年份和类型的下拉框选项
function populateDropdowns() {
const yearSelect = document.getElementById('year-select');
const typeSelect = document.getElementById('type-select');
// 初始化下拉框内容
yearSelect.innerHTML = '<option value="">Select Year</option>';
typeSelect.innerHTML = '<option value="">Select Type</option>';
// 填充年份下拉框
years.forEach(year => {
const option = document.createElement('option');
option.value = year;
option.textContent = year;
yearSelect.appendChild(option);
});
// 填充类型下拉框
types.forEach(type => {
const option = document.createElement('option');
option.value = type;
option.textContent = type;
typeSelect.appendChild(option);
});
}
// 更新图表,绘制用户选择的特征
function updateCharts() {
// 获取选中的特征
const selectedTraits = Array.from(document.querySelectorAll('#trait-filters input:checked')).map(input => input.value);
if (selectedTraits.length > 0) {
updatePairplot(selectedTraits, csvData);
updateGroupedBarChart(selectedTraits, csvData);
}
}
// 更新分组柱状图
function updateGroupedBarChart(selectedTraits, data) {
// 获取柱状图的画布上下文
const ctx = document.getElementById('groupedBarChartCanvas');
if (charts.groupedBarChart) charts.groupedBarChart.destroy();
// 设置图表默认字体和颜色
Chart.defaults.font.family = 'Arial';
Chart.defaults.color = 'black';
// 获取每个类型的标签以及每个特征的平均值
const labels = Array.from(new Set(data.map(row => row['Type'])));
const datasets = selectedTraits.map(trait => {
const datasetValues = labels.map(label => {
const filteredData = data.filter(row => row['Type'] === label);
const values = filteredData.map(row => parseFloat(row[trait])).filter(val => !isNaN(val));
const averageValue = values.length ? values.reduce((a, b) => a + b, 0) / values.length : 0;
return averageValue;
});
return {
label: trait,
data: datasetValues,
backgroundColor: `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, 0.5)`
};
});
// 创建分组柱状图
charts.groupedBarChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: datasets
},
options: {
responsive: true,
animation: false,
plugins: {
legend: {
display: true,
position: 'top',
labels: {
font: {
family: 'Arial'
},
color: 'black'
}
},
tooltip: {
enabled: true,
titleFont: {
family: 'Arial'
},
bodyFont: {
family: 'Arial'
}
}
},
title: {
display: true,
text: 'Grouped Bar Chart of Selected Traits',
font: {
family: 'Arial'
},
color: 'black'
},
scales: {
x: {
title: {
display: true,
text: 'Type',
font: {
family: 'Arial'
},
color: 'black'
},
ticks: {
font: {
family: 'Arial'
},
color: 'black'
}
},
y: {
title: {
display: true,
text: 'Average Value',
font: {
family: 'Arial'
},
color: 'black'
},
ticks: {
font: {
family: 'Arial'
},
color: 'black'
}
}
}
}
});
}
// 更新散点图 (pairplot)
function updatePairplot(selectedTraits, data) {
// 获取散点图的容器
const pairplotContainer = document.getElementById('pairplotContainer');
pairplotContainer.innerHTML = '';
// 设置图表默认字体和颜色
Chart.defaults.font.family = 'Arial';
Chart.defaults.color = 'black';
// 绘制选择特征之间的散点图
selectedTraits.forEach((traitX, i) => {
selectedTraits.slice(i + 1).forEach(traitY => {
const canvas = document.createElement('canvas');
pairplotContainer.appendChild(canvas);
// 延时执行,确保SVG元素正确生成后再进行下载
setTimeout(() => {
const svgElement = canvas.querySelector('svg');
if (svgElement) {
const svgData = new XMLSerializer().serializeToString(svgElement);
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const svgUrl = URL.createObjectURL(svgBlob);
downloadSVG(svgUrl, 'pairplot.svg');
}
}, 500);
// 获取特征X和Y的值,并过滤掉无效数据
const dataset = data.map(row => {
const x = parseFloat(row[traitX]);
const y = parseFloat(row[traitY]);
return { x, y };
}).filter(pair => !isNaN(pair.x) && !isNaN(pair.y));
// 创建散点图
const chartInstance = new Chart(canvas, {
type: 'scatter',
data: {
datasets: [{
label: `${traits.find(t => t.id === traitX).name} vs ${traits.find(t => t.id === traitY).name}`,
data: dataset,
backgroundColor: `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, 0.3)`,
pointRadius: 3
}]
},
options: {
responsive: true,
animation: false,
plugins: {
legend: {
display: true,
position: 'top',
labels: {
font: {
family: 'Arial'
},
color: 'black'
}
},
tooltip: {
enabled: true,
titleFont: {
family: 'Arial'
},
bodyFont: {
family: 'Arial'
}
}
},
title: {
display: true,
text: `${traitX} vs ${traitY}`,
font: {
family: 'Arial'
},
color: 'black'
},
scales: {
x: {
type: 'linear',
position: 'bottom',
title: {
display: true,
text: traits.find(t => t.id === traitX).name,
font: {
family: 'Arial'
},
color: 'black'
},
ticks: {
font: {
family: 'Arial'
},
color: 'black'
},
grid: {
color: 'rgba(200, 200, 200, 0.3)'
}
},
y: {
title: {
display: true,
text: traits.find(t => t.id === traitY).name,
font: {
family: 'Arial'
},
color: 'black'
},
ticks: {
font: {
family: 'Arial'
},
color: 'black'
},
grid: {
color: 'rgba(200, 200, 200, 0.3)'
}
}
}
}
});
});
});
}
</script>
</body>
</html>