SpectraViewer1D is the primary class for rendering 1D spectra in JSpectraViewer. It supports NMR, IR, UV/Vis, LC-MS, and EI-MS spectrum types. Initialize it with a container selector and an options object; the spectrum is drawn as soon as the viewer is constructed. It supports mirror plots, zoom/pan, a zoombox overview, a download menu, and — for NMR — an integrated assignment table and 3D structure viewer.
Initialize with a CSS selector pointing to the container element and an options object. The chartTitle, axis.x.label, axis.y.label, grid.horizontal, grid.vertical, controls.options.zoom and dataset[].style objects are required (their fields can be left out to use the defaults); omitting any of them makes the constructor throw an error.
import { SpectraViewer1D } from 'jsv2';
let viewer = new SpectraViewer1D("#graph-container", {
width: 1000,
height: 400,
chartType: "1D-NMR",
chartTitle: { text: "1D NMR Spectrum" },
dataset: [{
name: "My Spectrum",
dataArray: { x: [...], y: [...] },
normalize: true,
style: { strokeColor: "red" },
}],
axis: {
x: { enabled: true, reverse: true, label: { text: "Chemical Shift (ppm)" } },
y: { enabled: true, label: { text: "Intensity" } },
},
grid: { horizontal: { enabled: true }, vertical: { enabled: true } },
controls: { options: { zoom: { max: 100 } } },
});A unique identifier for this viewer instance.
string | default: generated ("jsv-1", "jsv-2", ...)Width of the viewer canvas in pixels.
number | default: 800Height of the viewer canvas in pixels.
number | default: 400Specifies the spectrum type. Controls axis defaults, bar vs. line rendering, and peak handling.
string | default: "1D-NMR" | ["1D-NMR", "1D-EIMS", "1D-LCMS", "1D-IR", "1D-UV"]Required. chartTitle.text is rendered as a heading above the plot; leave it empty for no title.
Tolerance for the Douglas-Peucker line simplification used when drawing.
Lower values preserve more detail; higher values improve render performance. If not set, the first dataset'ssimplifyTolerance is used.number | default: nullShow or hide the legend.
boolean | default: trueAn array of dataset objects to render. Each object describes one spectrum trace.
dataset: [{
dataArray: { x: [...], y: [...] },
isMirrorPlot: false,
normalize: true,
name: "My Spectrum",
id: "trace-1",
simplifyTolerance: 0.001,
style: { strokeColor: "red" },
}]The raw spectrum data. Both arrays must be the same length.
x: array of x values (ppm, m/z, wavenumber, wavelength)y: array of y intensity valuesRequired. style.strokeColor sets the CSS color of the trace.
When true, y values are normalized so the maximum equals 1.
boolean | default: falseWhen true, the dataset is rendered below the x-axis (y values negated) as a mirror spectrum for comparison.
boolean | default: falseDisplay name for this trace, shown in the legend. Also used as the trace's id for regular (non-mirror) datasets.
string | default: undefinedIdentifier for a mirror dataset ( isMirrorPlot: true). Regular datasets use name as their id.
Douglas-Peucker tolerance. Only the first dataset's value is read, as the fallback for the top-level simplifyTolerance.
The initial axis ranges. The x range (and, unless axis.y.reverse is set, the y range) is widened to fit the data.
x: [min, max]y: [min, max]{ x: [-1, 10], y: [0, 1.1] }Configuration for each axis. axis.x, axis.y and their label objects are required.
enabled: boolean — show or hide this axis (default: true)reverse: boolean — reverse the axis direction, e.g. NMR ppm and IR wavenumber on x, IR peaks pointing down on y (default: false)label.text: axis label string (default: "ppm" for x, "Intensity" for y)tickFormat: d3-format string or a formatting function (default: "~g")gutterSpace: space reserved for the axis in pixels (default: 60 for x, 65 for y)ticks: approximate number of tick marks; applies to both axes (default: 10)tickLength: tick length in pixels (default: 10)tickPrecision: tick label precision (default: 3)lock: true locks the y-axis minimum to its lowest boundary while zooming; a number locks it that fraction of the visible y range below 0 (default: false)tickFontOffset: adjusts the y tick label font size in points, e.g. -2 (default: 0)Required objects. enabled shows or hides the horizontal and vertical grid lines.
An overview minimap rendered in a corner of the viewer showing the full spectrum with a highlighted window indicating the current zoom region.
Show or hide the zoombox.
boolean | default: trueSize of the zoombox as a percentage of the viewer size.
number | default: 15Required object path. Maximum zoom level.
number | default: 100Every viewer has a download menu that exports the current view as PNG, JPEG, SVG, or the first spectrum's data as CSV, at a chosen width and height.
NMR only. Renders a peak assignment table linked to the spectrum. Providing an assignmentTable object enables it. Requires NMR assignment data from the DataParser. Adding a structure key also enables the 3D structure viewer — see the Molecular Structure Viewer page.
assignmentTable: {
containerID: "#assignments",
clusterNavigationID: "#cluster-navigation-1",
data: [event.detail.assignmentData],
}CSS selector for the element that will contain the assignment table.
string | requiredCSS selector for the cluster navigation element (multiplet navigator).
string | default: undefinedAssignment data array from event.detail.assignmentData provided by DataParser.
The NMR nucleus of the spectrum.
string | default: "1H"Redraws the viewer. Called automatically when the viewer is constructed.
viewer.draw();Adds a second spectrum rendered below the x-axis for side-by-side comparison. Its y values are scaled to the primary spectrum's height.
ForchartType: "1D-IR" with axis.y.reverse, the primary spectrum is inverted so that both spectra are drawn on a normal y-axis.x: array of x valuesy: array of y valuesname: display name for the mirror tracecolor: CSS color stringviewer.add_mirror_spectrum({
x: event.detail.x,
y: event.detail.y,
name: "Mirror Spectrum",
color: "blue",
});Removes the mirror spectrum and resets the axis ranges.
ForchartType: "1D-IR" with axis.y.reverse, the primary spectrum's inversion is not undone; create the viewer again to restore it.viewer.remove_mirror_spectrum();