SpectraViewer1D

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.

Usage:

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 } } },
});

Options:

id

A unique identifier for this viewer instance.

string | default: generated ("jsv-1", "jsv-2", ...)

width

Width of the viewer canvas in pixels.

number | default: 800

height

Height of the viewer canvas in pixels.

number | default: 400

chartType

Specifies 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"]

chartTitle

Required. chartTitle.text is rendered as a heading above the plot; leave it empty for no title.

object | text default: ""

simplifyTolerance

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's simplifyTolerance is used.number | default: null

legend.enabled

Show or hide the legend.

boolean | default: true

dataset

An 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" },
}]

dataset[].dataArray

The raw spectrum data. Both arrays must be the same length.

object | required

dataset[].style

Required. style.strokeColor sets the CSS color of the trace.

object | required

dataset[].normalize

When true, y values are normalized so the maximum equals 1.

boolean | default: false

dataset[].isMirrorPlot

When true, the dataset is rendered below the x-axis (y values negated) as a mirror spectrum for comparison.

boolean | default: false

dataset[].name

Display name for this trace, shown in the legend. Also used as the trace's id for regular (non-mirror) datasets.

string | default: undefined

dataset[].id

Identifier for a mirror dataset ( isMirrorPlot: true). Regular datasets use name as their id.

string | default: undefined

dataset[].simplifyTolerance

Douglas-Peucker tolerance. Only the first dataset's value is read, as the fallback for the top-level simplifyTolerance.

number | default: undefined

axis

axis.minBoundaries

The initial axis ranges. The x range (and, unless axis.y.reverse is set, the y range) is widened to fit the data.

object | default: { x: [-1, 10], y: [0, 1.1] }

axis.x / axis.y

Configuration for each axis. axis.x, axis.y and their label objects are required.

object

axis.x only

axis.y only

grid

grid.horizontal / grid.vertical

Required objects. enabled shows or hides the horizontal and vertical grid lines.

object | enabled default: true

zoomBox

An overview minimap rendered in a corner of the viewer showing the full spectrum with a highlighted window indicating the current zoom region.

zoomBox.enabled

Show or hide the zoombox.

boolean | default: true

zoomBox.size

Size of the zoombox as a percentage of the viewer size.

number | default: 15

controls

controls.options.zoom.max

Required object path. Maximum zoom level.

number | default: 100

Download menu

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

assignmentTable

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],
}

assignmentTable.containerID

CSS selector for the element that will contain the assignment table.

string | required

assignmentTable.clusterNavigationID

CSS selector for the cluster navigation element (multiplet navigator).

string | default: undefined

assignmentTable.data

Assignment data array from event.detail.assignmentData provided by DataParser.

array | required

assignmentTable.nmr_nucleus

The NMR nucleus of the spectrum.

string | default: "1H"

Methods

draw()

Redraws the viewer. Called automatically when the viewer is constructed.

viewer.draw();

add_mirror_spectrum(options)

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.

For chartType: "1D-IR" with axis.y.reverse, the primary spectrum is inverted so that both spectra are drawn on a normal y-axis.
viewer.add_mirror_spectrum({
  x: event.detail.x,
  y: event.detail.y,
  name: "Mirror Spectrum",
  color: "blue",
});

remove_mirror_spectrum()

Removes the mirror spectrum and resets the axis ranges.

For chartType: "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();