JSpectraViewer requires the following dependencies:
JSpectraViewer (jsv2) can be installed in your Node.js project using npm or yarn. You can also include it via a CDN like jsdeliver or unpkg link in your HTML file. These should also include the dependencies mentioned above.
npm install jsv2yarn add jsv2 <script src="https://cdn.jsdelivr.net/npm/jsv2@latest/dist/index.umd.js"></script><script src="https://unpkg.com/jsv2@latest/dist/index.umd.js"></script>After installation, you can use JSpectraViewer in your JavaScript code. The API provides three main classes: DataParser, SpectraViewer1D, and SpectraViewer2D.
The DataParser class is used to parse various data formats, including nmrML (1D and 2D NMR), JCAMP-DX, mzML, XY/CSV/TSV, and XML spectra (IR, EI-MS).The `dataType` option must specify the type of visualization you expect to create, such as `1D-NMR`, `2D-NMR`, `1D-EIMS`, etc.For full list of supported data types, refer to the DataParser API documentation
import { DataParser } from 'jsv2';
// Initialize the DataParser with the selected file type
// Note: The HTML file selector should be an <input> element with type="file" and id="file-selector"
let dataParser = new DataParser("#file-selector", {
dataType: "1D-NMR", // Specify the type of data you expect
expectedDataTag: "spectrumDataArray",
})Once parsing is completed, it emits a `fileParsed` event with the parsed data. Event listeners can be added for visualizing this data.
dataParser.addEventListener("fileParsed", (event) => {
// Access the parsed data
let parsedData = event.detail;
});The SpectraViewer1D class is used to visualize 1D spectra data. It can be initialized with any X, Y based data arrays or the parsed data from DataParser. The viewer draws itself when it is constructed. The chartTitle, axis (including axis.x.label and axis.y.label), grid, controls.options.zoom and dataset[].style objects shown below are required (their fields may be left out to use the defaults); omitting any of them makes the constructor throw an error. For more details on the SpectraViewer1D API options, refer to theSpectraViewer1D API documentation
import { SpectraViewer1D } from 'jsv2';
dataParser.addEventListener("fileParsed", (event) => {
// Initialize the SpectraViewer1D with the parsed data
let spectraViewer1D = new SpectraViewer1D("#spectra-viewer-1d", {
width: 1650,
height: 400,
chartTitle: { text: "Spectrum Viewer" },
chartType: event.detail.dataType, // e.g. "1D-NMR"
dataset: [
{
name: "My Spectrum",
dataArray: { x: event.detail.x, y: event.detail.y },
style: { strokeColor: "black" },
}
],
axis: {
x: { reverse: true, label: { text: "Chemical Shift (ppm)" } },
y: { label: { text: "Intensity" } },
},
grid: { horizontal: {}, vertical: {} },
controls: { options: { zoom: {} } },
});
});Similarly, the SpectraViewer2D API documentation can be used for visualizing 2D NMR spectra data.