JSpectraViewer draws spectra on an HTML canvas inside any container element. Two viewer classes cover the supported data: SpectraViewer1D for X, Y data (1D NMR, IR, UV/Vis, EI-MS and LC-MS) and SpectraViewer2D for X, Y, Z data (2D NMR such as 1H-13C HSQC). Both support zooming, panning, a download menu and an optional peak assignment table with a 3D structure viewer (NMR).
| Spectrum | Viewer | chartType / DataParser dataType | Drawn as | Typical input files |
|---|---|---|---|---|
| 1D NMR | SpectraViewer1D | "1D-NMR" | Line | nmrML, JCAMP-DX |
| IR / IRIS | SpectraViewer1D | "1D-IR" | Line | XML (ms-ir-spectrum), JCAMP-DX |
| UV/Vis | SpectraViewer1D | "1D-UV" | Line | JCAMP-DX |
| EI-MS | SpectraViewer1D | "1D-EIMS" | Bars | mzML, XML |
| LC-MS | SpectraViewer1D | "1D-LCMS" | Bars | mzML |
| 2D NMR | SpectraViewer2D | "2D-NMR" | Contours, with 1D projections on the top and left edges | nmrML |
XY/CSV/TSV files can be loaded for any 1D type. See the DataParser documentation for how files are detected and parsed.
A 1D viewer takes one or more datasets of equal-length x and y arrays. It draws itself when constructed. The chartTitle, axis, grid, controls.options.zoom and dataset[].style objects are required.
import { SpectraViewer1D } from 'jsv2';
let viewer = new SpectraViewer1D("#graph", {
width: 1000,
height: 400,
chartType: "1D-IR",
chartTitle: { text: "IR Spectrum" },
dataset: [{ name: "Spectrum", dataArray: { x: [...], y: [...] }, style: { strokeColor: "black" } }],
axis: {
x: { reverse: true, label: { text: "Wavenumber (cm-1)" } },
y: { reverse: true, label: { text: "Transmittance (%T)" } }, // IR peaks point down
},
grid: { horizontal: {}, vertical: {} },
controls: { options: { zoom: {} } },
});Reversed axes are common: NMR chemical shift and IR wavenumber decrease from left to right ( axis.x.reverse), and IR spectra are conventionally drawn with peaks pointing down ( axis.y.reverse).
A second 1D spectrum can be drawn below the x-axis for comparison, either with viewer.add_mirror_spectrum({ x, y, name, color }) or by passing a dataset with isMirrorPlot: true. The mirror spectrum is scaled to the height of the primary spectrum. Remove it with viewer.remove_mirror_spectrum().
A 2D viewer takes the direct dimension ( x, e.g. 1H ppm), the indirect dimension ( y, e.g. 13C ppm) and a flat intensity grid z of length x.length × y.length. Positive contours are drawn in blue, negative contours in red, and the summed 1D projections along the top and left edges.
import { SpectraViewer2D } from 'jsv2';
let viewer = new SpectraViewer2D("#graph", {
width: 800,
height: 800,
dataset: { dataArray: { x: event.detail.x, y: event.detail.y, z: event.detail.z } },
});See the SpectraViewer2D documentation for all options. Note that SpectraViewer2D uses different option names from SpectraViewer1D.
The view can be moved and scaled with the menu buttons above the plot or with the mouse and keyboard. The ? button in the menu shows these shortcuts.
| Action | 1D viewer | 2D viewer |
|---|---|---|
| Zoom in/out on the x-axis | Scroll wheel | — |
| Zoom in/out on the y-axis | Option/Alt + scroll wheel | — |
| Change the contour level | — | Scroll wheel |
| Zoom in on an area | Option/Alt + click and drag around the area | Click and drag around the area |
| Zoom out completely | Option/Alt + click once anywhere on the viewer | Option/Alt + click once anywhere on the viewer |
| Move around | Click and drag | — |
The zoombox in the upper right corner shows the whole spectrum with the visible region highlighted in grey.
The download button in the menu saves the current view at the viewer's size. 1D viewers offer PNG, JPEG, SVG and CSV (the first spectrum's X, Y data); 2D viewers offer PNG and JPEG.
For NMR data with assignments, both viewers can show a peak assignment table linked to the spectrum and a 3D structure of the compound. See the Assignment Table and 3D Molecular Structure Viewer pages.