forked from lorenasandoval88/statsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainExtra.js
50 lines (44 loc) · 1.84 KB
/
mainExtra.js
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
const Plotly = (await import('https://cdn.jsdelivr.net/npm/[email protected]/plotly.min.js'))
// const plotly = (await import('https://cdn.jsdelivr.net/npm/[email protected]/+esm')).default
// import * as Plotly from 'https://cdn.jsdelivr.net/npm/[email protected]/plotly.min.js'
// const Plotly = (await import("https://cdn.plot.ly/plotly-latest.min.js"))
// const PCA = await import("https://esm.sh/pca-js")
// const PCA2 = (await import("https://esm.sh/pca-js")).default
// import PCA from "https://esm.sh/pca-js"
// import {PCA} from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'
// import * as PCA from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'
async function pcaPlotlyPlot(data, labels) {
//console.log("data", data)
// //console.log("pca",pca)
//console.log("pca", PCA.getComponents())
// Perform PCA using a library like PCA-js or implement it manually
const pca = new PCA(); // Assuming you are using PCA-js
const components = PCA.getComponents(data); // Get the first two principal components
// Prepare data for Plotly
const trace = {
x: components.map(c => c[0]),
y: components.map(c => c[1]),
mode: 'markers',
type: 'scatter',
text: labels, // Optional: Add labels to data points
marker: {
size: 8,
color: labels.map(label => (label === 'A' ? 'blue' : (label === 'B' ? 'red' : 'green'))) // Example coloring based on labels
}
};
const layout = {
title: 'PCA Plot',
xaxis: {
title: 'Principal Component 1'
},
yaxis: {
title: 'Principal Component 2'
}
};
// Create the plot div
const pca_plot = document.createElement("div")
pcaDiv.id = 'pca_plot'
document.body.appendChild(pcaDiv);
pcaDiv.append(document.createElement('br'));
Plotly.newPlot('pca_plot', [trace], layout); // 'pca-plot' is the ID of the div where the plot will be rendered
}