Skip to main content

Mounting in your framework

Altrady Charts is a class: new AltradyCharts({ container, … }) mounts a chart into any DOM element, and chart.dispose() removes it. That is all a framework has to provide, so it works the same in vanilla JS or TS, React, Vue, Angular, Svelte and Solid. You never write framework code against the library.

Each snippet below is the chart component of a small example app. Those apps install the package from its .tgz, the same way you do, and are built on every change to Altrady Charts, so these snippets compile against the current API.

In every framework the pattern is the same:

  1. create the chart once the container element exists (after mount);
  2. keep the instance, and call dispose() when the component goes away.

All snippets import a datafeed from ./datafeed: your source of bars, an object that satisfies the Datafeed contract (see Getting Started). The example apps use one that computes prices from a formula, so they run without a backend. For one fed by the Altrady Data API, see Getting started, step 3.

Import the stylesheet once, at app startup: import '@altrady/charts/styles'. In Angular, add node_modules/@altrady/charts/superchart.css to the styles of your build options in angular.json instead.

Vanilla JS / TS​

The page has the container, <div id="chart" style="width: 100%; height: 100vh"></div>; container takes its id or the element itself. From src/main.ts:

import { AltradyCharts, createDataLoader } from '@altrady/charts'
import '@altrady/charts/styles'
import { datafeed } from './datafeed'

const chart = new AltradyCharts({
container: 'chart',
symbol: { ticker: 'BTCUSDT', pricePrecision: 2, volumePrecision: 0 },
period: { type: 'hour', span: 1, text: '1H' },
dataLoader: createDataLoader(datafeed),
theme: 'dark',
})

// Dispose the chart when you remove it from the page.
window.addEventListener('pagehide', () => chart.dispose())

React​

From src/Chart.tsx:

import { useEffect, useRef } from 'react'
import { AltradyCharts, createDataLoader } from '@altrady/charts'
import '@altrady/charts/styles'
import { datafeed } from './datafeed'

export function Chart () {
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
if (!ref.current) return
const chart = new AltradyCharts({
container: ref.current,
symbol: { ticker: 'BTCUSDT', pricePrecision: 2, volumePrecision: 0 },
period: { type: 'hour', span: 1, text: '1H' },
dataLoader: createDataLoader(datafeed),
theme: 'dark',
})
return () => chart.dispose()
}, [])

return <div ref={ref} style={{ width: '100%', height: '100vh' }} />
}

Vue 3​

From src/Chart.vue:

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { AltradyCharts, createDataLoader } from '@altrady/charts'
import '@altrady/charts/styles'
import { datafeed } from './datafeed'

const container = ref<HTMLDivElement | null>(null)
let chart: AltradyCharts | null = null

onMounted(() => {
if (!container.value) return
chart = new AltradyCharts({
container: container.value,
symbol: { ticker: 'BTCUSDT', pricePrecision: 2, volumePrecision: 0 },
period: { type: 'hour', span: 1, text: '1H' },
dataLoader: createDataLoader(datafeed),
theme: 'dark',
})
})

onBeforeUnmount(() => chart?.dispose())
</script>

<template>
<div ref="container" style="width: 100%; height: 100vh" />
</template>

Angular​

From src/chart.component.ts:

import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core'
import { AltradyCharts, createDataLoader } from '@altrady/charts'
import { datafeed } from './datafeed'

@Component({
selector: 'app-chart',
template: '<div #container style="width: 100%; height: 100vh"></div>',
})
export class ChartComponent implements AfterViewInit, OnDestroy {
@ViewChild('container') container!: ElementRef<HTMLDivElement>
private chart?: AltradyCharts

ngAfterViewInit () {
this.chart = new AltradyCharts({
container: this.container.nativeElement,
symbol: { ticker: 'BTCUSDT', pricePrecision: 2, volumePrecision: 0 },
period: { type: 'hour', span: 1, text: '1H' },
dataLoader: createDataLoader(datafeed),
theme: 'dark',
})
}

ngOnDestroy () {
this.chart?.dispose()
}
}

Svelte 5​

From src/Chart.svelte:

<script lang="ts">
import { onMount } from 'svelte'
import { AltradyCharts, createDataLoader } from '@altrady/charts'
import '@altrady/charts/styles'
import { datafeed } from './datafeed'

let container: HTMLDivElement

onMount(() => {
const chart = new AltradyCharts({
container: container,
symbol: { ticker: 'BTCUSDT', pricePrecision: 2, volumePrecision: 0 },
period: { type: 'hour', span: 1, text: '1H' },
dataLoader: createDataLoader(datafeed),
theme: 'dark',
})
return () => chart.dispose()
})
</script>

<div bind:this={container} style="width: 100%; height: 100vh"></div>

Solid​

From src/Chart.tsx:

import { onCleanup, onMount } from 'solid-js'
import { AltradyCharts, createDataLoader } from '@altrady/charts'
import '@altrady/charts/styles'
import { datafeed } from './datafeed'

export function Chart () {
let container!: HTMLDivElement

onMount(() => {
const chart = new AltradyCharts({
container: container,
symbol: { ticker: 'BTCUSDT', pricePrecision: 2, volumePrecision: 0 },
period: { type: 'hour', span: 1, text: '1H' },
dataLoader: createDataLoader(datafeed),
theme: 'dark',
})
onCleanup(() => chart.dispose())
})

return <div ref={container} style={{ width: '100%', height: '100vh' }} />
}

Sizing the container​

The chart fills its container, so give the container a size: the snippets use height: 100vh. Scaffolded apps often constrain the root element (Vite's react-ts template, for instance, centres #root with max-width: 1280px and padding in its default index.css), which boxes the chart in. Remove those defaults, or give the chart's container explicit dimensions of your own.