Skip to content

Commit 4802116

Browse files
committed
chore: add advanced example
1 parent bfe2ac8 commit 4802116

File tree

4 files changed

+99
-29
lines changed

4 files changed

+99
-29
lines changed

examples/data.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

examples/example.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
1-
const gsd = require('../src/gsd');
2-
3-
let X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
4-
let Y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0];
5-
let peaks = gsd(X, Y, {
6-
noiseLevel: 0,
7-
minMaxRatio: 0.00025, // Threshold to determine if a given peak should be considered as a noise
8-
realTopDetection: true,
9-
maxCriteria: true, // inverted:false
1+
/*
2+
install vscode plugin `liveserver` and right click on index.html and `Open with liveserver`
3+
4+
start this code with automatic reload using
5+
`npm run example``
6+
*/
7+
8+
import { gsd, optimizePeaks } from '../src';
9+
import { writeFileSync } from 'fs'
10+
import { join } from 'path'
11+
import { generateSpectrum } from 'spectrum-generator'
12+
13+
const peaks = [
14+
{ x: -0.1, y: 0.2, width: 0.3 },
15+
{ x: 0.1, y: 0.2, width: 0.1 },
16+
];
17+
18+
const original = generateSpectrum(peaks, { from: -1, to: 1, nbPoints: 101 });
19+
20+
21+
let peakList = gsd(original, {
22+
minMaxRatio: 0,
23+
realTopDetection: false,
1024
smoothY: false,
11-
sgOptions: { windowSize: 7, polynomial: 3 },
25+
heightFactor: 1,
26+
shape: { kind: 'gaussian' },
1227
});
1328

14-
console.log(peaks);
29+
30+
let optimizedPeaks = optimizePeaks(original, peakList);
31+
32+
let labels = optimizedPeaks.map(peak => ({ x: peak.x, y: peak.y, dy: '-10px', dx: '3px', text: peak.x.toPrecision(4) + ' / ' + peak.y.toPrecision(4) }));
33+
let lines = optimizedPeaks.map(peak => ([{ x: peak.x, y: peak.y }, { x: peak.x, y: peak.y, dy: '-20px' }]));
34+
let polygons = [];
35+
for (const peak of optimizedPeaks) {
36+
const peaksSpectrum = generateSpectrum([peak], { from: peak.x - peak.width * 5, to: peak.x + peak.width * 5, nbPoints: 501 });
37+
const polygon = [];
38+
for (let i = 0; i < peaksSpectrum.x.length; i++) {
39+
polygon.push({ x: peaksSpectrum.x[i], y: peaksSpectrum.y[i] })
40+
}
41+
polygons.push(polygon);
42+
}
43+
44+
45+
46+
original.x = Array.from(original.x)
47+
original.y = Array.from(original.y)
48+
49+
writeFileSync(join(__dirname, 'data.json'), JSON.stringify({ data: original, labels, lines, polygons }), 'utf8')

examples/index.html

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ <h1>A spectrum-generator module usage example</h1>
2424

2525
<script>
2626
// fetching data
27+
const colors = ['red', 'green', 'blue'];
2728

2829
createGraph();
2930

3031
async function createGraph() {
31-
var reponse = await fetch('./data.json');
32-
var values = await reponse.json();
32+
const response = await fetch('./data.json');
33+
const json = await response.json();
34+
const data = json.data || json;
3335

3436
// options for zoom
3537
let options = {
@@ -65,28 +67,59 @@ <h1>A spectrum-generator module usage example</h1>
6567
graph.resize(800, 600); // Resizes the container
6668

6769
let waveForm = Graph.newWaveform();
68-
let arrayX = values.x;
69-
let arrayY = values.y;
70-
waveForm.setData(arrayY, arrayX);
70+
waveForm.setData(data.y, data.x);
7171

7272
graph
7373
.newSerie('line serie', {}, 'line') // Creates a new serie
7474
.autoAxis() // Assigns axes
7575
.setWaveform(waveForm);
7676

77-
var modifiers = []; // We can define a specific style for some points in the scatter plot
78-
// modifiers[20] = { shape: 'circle', r: 12, fill: 'rgba(0, 100, 255, 0.3)', stroke: 'rgb(0, 150, 255)' };
77+
if (false && json.labels) {
78+
for (let label of json.labels) {
79+
const shape = graph.newShape('label', {
80+
label: {
81+
text: label.text,
82+
position: {
83+
x: label.x,
84+
y: label.y,
85+
dx: label.dx,
86+
dy: label.dy,
87+
},
88+
},
89+
});
90+
shape.draw();
91+
}
92+
}
7993

80-
/*
81-
graph
82-
.newSerie('scatter serie', {}, 'scatter') // Creates a new serie
83-
.autoAxis() // Assigns axes
84-
.setWaveform(waveForm)
85-
.setStyle(
86-
{ shape: 'circle', r: 4, fill: 'red', stroke: 'blue' },
87-
modifiers,
88-
);
89-
*/
94+
if (false && json.lines) {
95+
for (let line of json.lines) {
96+
const shape = graph.newShape('line', {
97+
position: line,
98+
});
99+
console.log(line);
100+
shape.draw();
101+
}
102+
}
103+
104+
if (json.polygons) {
105+
for (let i = 0; i < json.polygons.length; i++) {
106+
const polygon = json.polygons[i];
107+
const shape = graph.newShape(
108+
'polygon',
109+
{
110+
position: polygon,
111+
},
112+
false,
113+
{
114+
fillColor: colors[i % colors.length],
115+
fillOpacity: 0.15,
116+
strokeWidth: 1,
117+
strokeColor: colors[i % colors.length],
118+
},
119+
);
120+
shape.draw();
121+
}
122+
}
90123

91124
graph.draw(); // Draw
92125
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"scripts": {
1515
"build": "rollup -c && cheminfo-build --entry src/index.js --root GSD",
16+
"example": "nodemon -w src -w examples/example.js -r esm examples/example.js",
1617
"eslint": "eslint src --cache",
1718
"eslint-fix": "npm run eslint -- --fix",
1819
"compile": "rollup -c",
@@ -66,6 +67,7 @@
6667
"jest-matcher-deep-close-to": "^2.0.1",
6768
"mf-global": "^1.3.0",
6869
"ml-stat": "^1.3.3",
70+
"nodemon": "^2.0.6",
6971
"prettier": "^2.2.1",
7072
"rollup": "^2.34.1",
7173
"spectrum-generator": "^4.4.2",

0 commit comments

Comments
 (0)