Getting started
Install
npm install @kumo-charts/coreWith Vue or React, add the package of its components too: see Vue and React.
A first chart
The chart fills its container and follows its size, so the container needs a height:
<div id="chart" style="height: 480px"></div>Create the chart and give it bars:
Show codeHide code
import { createChart } from '@kumo-charts/core';
import { loadBars } from '@docs/data';
const chart = createChart(document.getElementById('chart')!);
chart.data.set(await loadBars());A bar is an object:
const bar = {
time: 1767225600000, // epoch milliseconds, or an ISO string or a Date
open: 87512.3,
high: 87650,
low: 87480.1,
close: 87601.5,
volume: 132.4 // optional, 0 by default
};Bars go from the oldest to the newest, each at a time of its own.
Timeframes
Tell the chart how long its bars are and it builds longer ones in the browser, without asking your server again:
Show codeHide code
import { createChart } from '@kumo-charts/core';
import { loadBars } from '@docs/data';
const chart = createChart(document.getElementById('chart')!);
chart.data.set(await loadBars(), { interval: '1m' });
chart.data.setTimeframe('1h');setTimeframe(null) shows the bars as given again. How to write intervals, trading sessions and time zones: Timeframes and calendars.
Indicators and drawings
Indicators and drawings are modules, given when the chart is created:
Show codeHide code
import { createChart } from '@kumo-charts/core';
import { indicators, MA, MACD } from '@kumo-charts/core/indicators';
import { drawings, trendLine, horizontalLine } from '@kumo-charts/core/drawings';
import { loadBars } from '@docs/data';
const chart = createChart(document.getElementById('chart')!, {
modules: [indicators([MA, MACD]), drawings([trendLine, horizontalLine])]
});
chart.data.set(await loadBars(), { interval: '1m' });
chart.data.setTimeframe('15m');
chart.indicators.add({ type: 'MA' }); // on the main pane
chart.indicators.add({ type: 'MACD' }); // in a pane of its own
// Let the user draw a trend line: two clicks
chart.drawings.startTool('trend-line');Load only the few you use and only those end up in your bundle; or load a whole module, see Modules.
Real time
Push new bars with update(): a bar at the time of the last one replaces it, a later one is appended:
socket.onmessage = (message) => {
const bar = JSON.parse(message.data);
chart.data.update(bar);
};If you receive trades, give them to the chart as they are: it folds them into the current bar and starts the next one when its time comes. This needs the interval of the bars:
chart.data.addTrades({ time: trade.time, price: trade.price, size: trade.size });Loading history
When the user scrolls back near the first bar, the chart emits loadMore; add the older bars in front with prepend():
chart.on('loadMore', async ({ before }) => {
const older = await fetchBars({ endTime: before - 1 });
chart.data.prepend(older);
});before is the time of the current first bar. Once older bars are in, scrolling back further emits it again.
Without a bundler
Include one script and every module is in the global KumoChart:
<div id="chart" style="height: 480px"></div>
<script src="https://cdn.jsdelivr.net/npm/@kumo-charts/core/dist/kumo-charts.iife.js"></script>
<script>
const chart = KumoChart.createChart(document.getElementById('chart'), {
modules: KumoChart.allModules()
});
chart.data.set(bars);
</script>The script holds every feature, about 112 kB gzipped. The words of the indicators in Chinese, Japanese and Korean (see Indicators) are in a script of their own, about 6 kB, to add when you want them, as the global KumoChartIndicatorTexts:
<script src="https://cdn.jsdelivr.net/npm/@kumo-charts/core/dist/kumo-charts.iife.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@kumo-charts/core/dist/kumo-charts-indicator-texts.iife.js"></script>
<script>
const chart = KumoChart.createChart(document.getElementById('chart'), {
modules: KumoChart.allModules({
indicators: { texts: [KumoChartIndicatorTexts.indicatorTexts] }
}),
locale: 'zh-CN'
});
</script>Destroying
Call destroy() when the chart leaves the page: it removes its element and releases every listener and timer.
chart.destroy();Once destroyed, the chart throws for any call, a read as well as a change; calling destroy() again does nothing.
Next
- Modules: which feature is in which module, and loading what you need.
- Data: bars, trades and timeframes in detail.
- Appearance: themes, colors, scales and the crosshair.