For this example, we demonstrate how to create a JavaScript OHLC Chart or Stock Chart using SciChart.js. This is our powerful JavaScript Chart Component.
import { closeValues, dateValues as xValues, highValues, lowValues, openValues } from "./data";
import {
ENumericFormat,
FastOhlcRenderableSeries,
MouseWheelZoomModifier,
NumericAxis,
NumberRange,
OhlcDataSeries,
SciChartSurface,
SmartDateLabelProvider,
SweepAnimation,
ZoomExtentsModifier,
ZoomPanModifier,
SciChartJsNavyTheme,
DiscontinuousDateAxis,
} from "scichart";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: new SciChartJsNavyTheme(),
});
// Add DiscontinuousDateAxis - which collapses gaps in stock market data
sciChartSurface.xAxes.add(
new DiscontinuousDateAxis(wasmContext, {
labelProvider: new SmartDateLabelProvider(),
growBy: new NumberRange(0.05, 0.05),
})
);
// Create a NumericAxis on the YAxis with 4 Decimal Places
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
visibleRange: new NumberRange(1.1, 1.2),
growBy: new NumberRange(0.1, 0.1),
labelFormat: ENumericFormat.Decimal,
labelPrecision: 4,
})
);
// Create the Ohlc series and add to the chart
sciChartSurface.renderableSeries.add(
new FastOhlcRenderableSeries(wasmContext, {
dataSeries: new OhlcDataSeries(wasmContext, { xValues, openValues, highValues, lowValues, closeValues }),
strokeThickness: 3,
dataPointWidth: 0.7,
strokeUp: "#50ff50",
strokeDown: "#ff5050",
animation: new SweepAnimation({ duration: 800, fadeEffect: true }),
})
);
// Optional: Add some interactivity modifiers
sciChartSurface.chartModifiers.add(
new ZoomExtentsModifier(),
new ZoomPanModifier({ enableZoom: true }),
new MouseWheelZoomModifier()
);
sciChartSurface.zoomExtents();
return { sciChartSurface, wasmContext };
};
This example demonstrates how to create an Ohlc Chart (Open-High-Low-Close Chart) using JavaScript with SciChart.js. It is designed to visually represent financial market data, making it ideal for stock trading applications and financial data analysis.
The chart is initialized by calling the asynchronous function that creates a SciChartSurface using SciChartSurface.create. A CategoryAxis is added and configured with a SmartDateLabelProvider to effectively handle non-contiguous stock market dates, and a NumericAxis is used to display financial values with precise formatting. Financial data is managed via an OhlcDataSeries that feeds into a FastOhlcRenderableSeries, which renders the OHLC data points efficiently. A SweepAnimation enhances the visual presentation by animating the appearance of the series.
This implementation includes several advanced features such as interactive chart modifiers. The chart supports intuitive user interactions through ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier, allowing for seamless zooming and panning operations. Additionally, performance optimizations are achieved with WebGL rendering, ensuring smooth animations and responsiveness even with large datasets. For detailed performance insights, refer to the Getting Started with SciChart JS documentation.
The example employs JavaScript integration, highlighting best practices for resource management. A cleanup function is provided by calling sciChartSurface.delete() to properly dispose of the chart resources when they are no longer needed, aligning with the guidelines in the Memory Best Practices documentation. This approach ensures that applications remain performant and free of memory leaks. The example serves as a robust reference for developers looking to integrate high-performance financial charts without additional framework dependencies.
Overall, this Ohlc Chart example using JavaScript with SciChart.js encapsulates best practices for chart initialization, axis configuration, animation, interactivity, and resource management, providing an excellent starting point for building scalable financial data visualization applications.












