Spectra Viewer

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).

Supported spectrum types

SpectrumViewerchartType / DataParser dataTypeDrawn asTypical input files
1D NMRSpectraViewer1D"1D-NMR"LinenmrML, JCAMP-DX
IR / IRISSpectraViewer1D"1D-IR"LineXML (ms-ir-spectrum), JCAMP-DX
UV/VisSpectraViewer1D"1D-UV"LineJCAMP-DX
EI-MSSpectraViewer1D"1D-EIMS"BarsmzML, XML
LC-MSSpectraViewer1D"1D-LCMS"BarsmzML
2D NMRSpectraViewer2D"2D-NMR"Contours, with 1D projections on the top and left edgesnmrML

XY/CSV/TSV files can be loaded for any 1D type. See the DataParser documentation for how files are detected and parsed.

1D spectra (X, Y)

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).

Mirror plots

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().

2D spectra (X, Y, Z)

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.

Viewer controls

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.

Action1D viewer2D viewer
Zoom in/out on the x-axisScroll wheel—
Zoom in/out on the y-axisOption/Alt + scroll wheel—
Change the contour level—Scroll wheel
Zoom in on an areaOption/Alt + click and drag around the areaClick and drag around the area
Zoom out completelyOption/Alt + click once anywhere on the viewerOption/Alt + click once anywhere on the viewer
Move aroundClick and drag—

Zoombox (1D)

The zoombox in the upper right corner shows the whole spectrum with the visible region highlighted in grey.

Downloading

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.

Assignments and structures

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.