@molecule/app-sparkline-angular
Feature · sparkline · App (browser) · v1.1.3 · Apache-2.0
Tiny inline trend chart (line, bar, dot variants) with no library dep
npm install @molecule/app-sparkline-angularHow it works
@molecule/app-sparkline-angular is a ready-made sparkline feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { Component } from '@angular/core'
import { Sparkline } from '@molecule/app-sparkline-angular'
@Component({
selector: 'revenue-tile',
standalone: true,
imports: [Sparkline],
template: `
<mol-sparkline
[values]="[12, 18, 15, 22, 30, 27, 35]"
variant="line"
[width]="80"
[height]="24"
ariaLabel="Weekly revenue trend"
/>
`,
})
export class RevenueTile {}Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular tiny inline trend chart — line/bar/dot variants, SVG only.
Exports <Sparkline> — the Angular counterpart of
@molecule/app-sparkline-react, with the same props (React names, bound
as inputs) and the same DOM for the same input, so an app switches
framework by changing only the package name in its imports.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { Component } from '@angular/core'
import { Sparkline } from '@molecule/app-sparkline-angular'
@Component({
selector: 'revenue-tile',
standalone: true,
imports: [Sparkline],
template: `
<mol-sparkline
[values]="[12, 18, 15, 22, 30, 27, 35]"
variant="line"
[width]="80"
[height]="24"
ariaLabel="Weekly revenue trend"
/>
`,
})
export class RevenueTile {}
Type
feature
Installation
npm install @molecule/app-sparkline-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
API
Interfaces
SparklineProps
Props accepted by Sparkline — the same props
@molecule/app-sparkline-react accepts, bound as inputs on the host
element.
interface SparklineProps {
/** Numeric series — length determines the segment count. */
values: number[]
/** Visual variant. */
variant?: 'line' | 'bar' | 'dot'
/** SVG width in px. Defaults to 80. */
width?: number
/** SVG height in px. Defaults to 24. */
height?: number
/** Stroke / fill color. Defaults to currentColor. */
color?: string
/**
* Optional accessible label. Overrides the translated / `defaultValue`
* `'Trend sparkline'` (prop > `t()` > default).
*/
ariaLabel?: string
/** Extra classes. */
className?: string
}
Classes
Sparkline
Sparkline component.
Tiny inline trend chart — line, bar, or dot variants, SVG with no
external library. Same props, same DOM as
@molecule/app-sparkline-react's Sparkline. React's root is the
<svg> when the series is non-empty and nothing when empty, so the
<svg> renders in front of the host and the host itself leaves the page
(placeElementBefore + setElementPresent, React's fragment root); the
series renders with track $index, matching React's per-item keys, so a
new array of equal values keeps the rendered nodes.
@Component({
selector: 'mol-sparkline',
standalone: true,
hostDirectives: [MolPlacement],
inputs: ['values', 'variant', 'width', 'height', 'color', 'ariaLabel', 'className'],
template: `@if (hasValues) {
<svg
#root
role="img"
[attr.width]="svgWidth()"
[attr.height]="svgHeight()"
[attr.viewBox]="viewBox()"
[attr.aria-label]="label()"
[attr.class]="classes()"
>
@if (isLine()) {
<polyline
fill="none"
[attr.stroke]="colorValue()"
[attr.stroke-width]="1.5"
[attr.points]="points()"
/>
} @else if (isBar()) {
@for (bar of bars(); track $index) {
<rect
[attr.x]="bar.x"
[attr.y]="bar.y"
[attr.width]="bar.width"
[attr.height]="bar.height"
[attr.fill]="colorValue()"
/>
}
} @else if (isDot()) {
@for (dot of dots(); track $index) {
<circle [attr.cx]="dot.cx" [attr.cy]="dot.cy" [attr.r]="1.5" [attr.fill]="colorValue()" />
}
}
</svg>
}`,
})
export class Sparkline {
/** Numeric series — length determines the segment count. */
values: number[] = []
/** Visual variant. */
variant?: 'line' | 'bar' | 'dot'
/** SVG width in px. */
width?: number
/** SVG height in px. */
height?: number
/** Stroke / fill color. */
color?: string
/** Optional accessible label. */
ariaLabel?: string
/** Extra classes. */
className?: string
/** The i18n handle (locale-reactive), read in an injection context. */
private readonly i18n = useTranslation()
/** The `<svg>` — React's root while the series is non-empty. */
@ViewChild('root') private root?: ElementRef<SVGSVGElement>
private readonly host: ElementRef<HTMLElement> = inject(ElementRef)
constructor() {
// React's root is the `<svg>` itself (`values: []` renders nothing): it renders in
// front of the host's place and the host leaves the page.
usePlacement(() => {
const root = this.root?.nativeElement
if (root) placeElementBefore(root, this.host.nativeElement)
setElementPresent(this.host.nativeElement, false)
})
}
/**
* The current class map.
* @returns The UIClassMap.
*/
cm(): UIClassMap {
return getClassMap()
}
/**
* Whether the series renders at all (React returns null for an empty one).
* @returns True when `values` is non-empty.
*/
get hasValues(): boolean {
return this.values.length > 0
}
/**
* The effective variant (React's default parameter: an absent or
* later-bound write of `undefined` must not erase the `'line'` default).
* @returns `'line'`, `'bar'` or `'dot'`.
*/
variantValue(): 'line' | 'bar' | 'dot' {
return this.variant ?? 'line'
}
/**
* Whether the line variant renders.
* @returns True for `'line'`.
*/
isLine(): boolean {
return this.variantValue() === 'line'
}
/**
* Whether the bar variant renders.
* @returns True for `'bar'`.
*/
isBar(): boolean {
return this.variantValue() === 'bar'
}
/**
* Whether the dot variant renders.
* @returns True for `'dot'`.
*/
isDot(): boolean {
return this.variantValue() === 'dot'
}
/**
* The SVG width (React's default parameter).
* @returns The width in px.
*/
svgWidth(): number {
return this.width ?? 80
}
/**
* The SVG height (React's default parameter).
* @returns The height in px.
*/
svgHeight(): number {
return this.height ?? 24
}
/**
* The viewBox of the SVG.
* @returns The `min-x min-y width height` string.
*/
viewBox(): string {
return `0 0 ${this.svgWidth()} ${this.svgHeight()}`
}
/**
* The stroke / fill color (React's default parameter).
* @returns The CSS color.
*/
colorValue(): string {
return this.color ?? 'currentColor'
}
/**
* The accessible label (React's default parameter): the prop when given,
* otherwise the translated default.
* @returns The label text.
*/
label(): string {
return (
this.ariaLabel ??
this.i18n.t('sparkline.aria.trendSparkline', undefined, { defaultValue: 'Trend sparkline' })
)
}
/**
* Computes the SVG's class list.
* @returns The class string.
*/
classes(): string {
return this.cm().cn(this.className)
}
/**
* The smallest value of the series.
* @returns The minimum.
*/
min(): number {
return Math.min(...this.values)
}
/**
* The largest value of the series.
* @returns The maximum.
*/
max(): number {
return Math.max(...this.values)
}
/**
* The normalized value range (1 for a constant series, so it renders along
* the bottom edge instead of dividing by zero).
* @returns `max - min`, or 1 when that is 0.
*/
range(): number {
return this.max() - this.min() || 1
}
/**
* The horizontal distance between two points (0 for a single value).
* @returns The x step.
*/
stepX(): number {
return this.values.length === 1 ? 0 : this.svgWidth() / (this.values.length - 1)
}
/**
* The vertical pixel of a value: the bottom edge minus its normalized height.
* @param v - The value.
* @returns The y coordinate inside the viewBox.
*/
y(v: number): number {
return this.svgHeight() - ((v - this.min()) / this.range()) * this.svgHeight()
}
/**
* The `<polyline>` points of the line variant.
* @returns The `x,y` pairs, space-separated.
*/
points(): string {
return this.values.map((v, i) => `${i * this.stepX()},${this.y(v)}`).join(' ')
}
/**
* The bar width (at least 2 px, so a wide series stays visible).
* @returns The bar width in px.
*/
barWidth(): number {
return Math.max(2, this.svgWidth() / this.values.length - 2)
}
/**
* The `<rect>` specs of the bar variant.
* @returns One spec per value.
*/
bars(): BarSpec[] {
return this.values.map((v, i) => {
const height = ((v - this.min()) / this.range()) * this.svgHeight()
return {
x: i * (this.barWidth() + 2),
y: this.svgHeight() - height,
width: this.barWidth(),
height,
}
})
}
/**
* The `<circle>` specs of the dot variant.
* @returns One spec per value.
*/
dots(): DotSpec[] {
return this.values.map((v, i) => ({ cx: i * this.stepX(), cy: this.y(v) }))
}
}
Injection Notes
Requirements
Peer dependencies:
@angular/common^22.0.0@angular/core^22.0.0@angular/platform-browser^22.0.0@molecule/app-angular^3.13.0 || ^4.0.0@molecule/app-ui^1.0.1@molecule/app-ui-angular^1.1.0
Runtime Dependencies
-
@angular/common -
@angular/core -
@angular/platform-browser -
@molecule/app-angular -
@molecule/app-ui -
@molecule/app-ui-angular -
Requires a wired ClassMap bond (
getClassMap()throws before bonding). The defaultaria-labelreads throughuseTranslation(), so it follows the active locale. -
The SVG is fixed-pixel (
width/heightinputs, defaults 80×24) — it does not stretch to its container. Compute px from layout if you need responsiveness. -
coloraccepts any CSS color and defaults tocurrentColor, so the sparkline inherits the surrounding text color — set a text color on a parent (e.g. success/error) to theme it. -
Values are min-max normalized per series; a constant series renders as a line along the bottom edge, and
values: []renders nothing. -
Pass
ariaLabelwith your own translated string to override the built-in one (t('sparkline.aria.trendSparkline', …, { defaultValue: 'Trend sparkline' })). -
React's root is the
<svg>when the series is non-empty and nothing when empty, so the<svg>renders in front of the host and the host itself leaves the page, contributing no DOM of its own.
Translations
Translation strings are provided by @molecule/app-locales-sparkline.