Skip to content

SpectrumViewer

UV/CD/MS spectrum viewer with peak annotation.

Demo

Usage

svelte
<script>
  import { SpectrumViewer } from '@molbiohive/hatchlings';
</script>

<SpectrumViewer data={spectrumData} width={600} height={350} />

Data Type — SpectrumData

FieldTypeRequiredDescription
xnumber[]yesX values (wavelength, m/z, etc.)
ynumber[]yesY values (absorbance, intensity)
peaksSpectrumPeak[]noAnnotated peaks
xLabelstringnoX-axis label
yLabelstringnoY-axis label
titlestringnoChart title

Props

PropTypeDefaultDescription
dataSpectrumDataPrimary data prop
widthnumber600SVG width
heightnumber350SVG height

Example — Constructing Data

ts
import type { SpectrumData } from '@molbiohive/hatchlings';

const data: SpectrumData = {
  x: Array.from({ length: 200 }, (_, i) => 200 + i * 2),
  y: Array.from({ length: 200 }, (_, i) => {
    const w = 200 + i * 2;
    return 0.8 * Math.exp(-((w - 280) ** 2) / 400) +
      0.3 * Math.exp(-((w - 220) ** 2) / 200);
  }),
  peaks: [
    { x: 220, y: 0.3, label: '220 nm' },
    { x: 280, y: 0.8, label: '280 nm' },
  ],
  xLabel: 'Wavelength (nm)',
  yLabel: 'Absorbance',
  title: 'UV Absorption Spectrum',
};

This is the data powering the demo above. See docs/data/charts.ts for the full source.