Demonstrates how to use the Builder Api to create a Fully Configured Chart using SciChart.js, High Performance JavaScript Charts
import {
build2DChart,
registerAllTypes,
EAxisType,
ELabelProviderType,
EAxisAlignment,
NumberRange,
ESeriesType,
GradientParams,
EPointMarkerType,
EAnnotationType,
ECoordinateMode,
EHorizontalAnchorPoint,
EVerticalAnchorPoint,
EChart2DModifierType,
Point,
} from "scichart";
import { appTheme } from "./appTheme";
// The Builder API is lean by default in v6. This chart is defined with type-strings only,
// so register the built-in types before building it
registerAllTypes();
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a chart using the Builder-API, an api that allows defining a chart
// with javascript-objects or JSON
return await build2DChart(rootElement, {
// Set theme
surface: { theme: appTheme.SciChartJsTheme },
// Add XAxis
xAxes: [
{
type: EAxisType.NumericAxis,
options: {
axisTitle: "X Axis Title",
growBy: new NumberRange(0.1, 0.1),
autoTicks: false,
majorDelta: 1,
minorDelta: 0.2,
labelProvider: {
type: ELabelProviderType.Text,
options: {
labels: { 1: "one", 2: "two", 3: "three", 4: "four", 5: "five" },
},
},
},
},
],
// Add multiple Y-Axis
yAxes: [
{
type: EAxisType.NumericAxis,
options: {
id: "y1",
axisTitle: "Left Axis",
axisAlignment: EAxisAlignment.Left,
visibleRange: new NumberRange(0, 20),
zoomExtentsToInitialRange: true,
},
},
{
type: EAxisType.NumericAxis,
options: {
id: "y2",
axisTitle: "Right Axis",
axisAlignment: EAxisAlignment.Right,
visibleRange: new NumberRange(0, 800),
labelPrecision: 0,
zoomExtentsToInitialRange: true,
},
},
],
// Add series. More than one can be set in an array
series: [
{
// each series has type, options in the builder-API
type: ESeriesType.SplineMountainSeries,
options: {
yAxisId: "y1",
stroke: appTheme.VividSkyBlue,
strokeThickness: 5,
fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
{ color: appTheme.VividTeal, offset: 0.2 },
{ color: "Transparent", offset: 1 },
]),
},
xyData: { xValues: [1, 2, 3, 4, 5], yValues: [8, 6, 7, 2, 16] },
},
{
type: ESeriesType.BubbleSeries,
options: {
yAxisId: "y2",
pointMarker: {
type: EPointMarkerType.Ellipse,
options: {
width: 100,
height: 100,
strokeThickness: 10,
fill: appTheme.PaleSkyBlue,
stroke: appTheme.VividSkyBlue,
},
},
},
xyzData: {
xValues: [1, 2, 3, 4, 5],
yValues: [320, 240, 280, 80, 640],
zValues: [20, 40, 20, 30, 35],
},
},
],
// Add annotations
annotations: [
{
type: EAnnotationType.SVGTextAnnotation,
options: {
text: "Labels",
textColor: appTheme.ForegroundColor,
yAxisId: "y1",
x1: 1,
y1: 10,
yCoordinateMode: ECoordinateMode.DataValue,
},
},
{
type: EAnnotationType.SVGTextAnnotation,
options: {
text: "can be placed",
textColor: appTheme.ForegroundColor,
yAxisId: "y1",
x1: 2,
y1: 8,
yCoordinateMode: ECoordinateMode.DataValue,
},
},
{
type: EAnnotationType.SVGTextAnnotation,
options: {
text: "on the chart",
textColor: appTheme.ForegroundColor,
yAxisId: "y1",
x1: 3,
y1: 9,
yCoordinateMode: ECoordinateMode.DataValue,
},
},
{
type: EAnnotationType.SVGTextAnnotation,
options: {
text: "Builder API Demo",
textColor: appTheme.ForegroundColor,
x1: 0.5,
y1: 0.5,
opacity: 0.33,
yCoordShift: -52,
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
verticalAnchorPoint: EVerticalAnchorPoint.Center,
fontSize: 36,
fontWeight: "Bold",
},
},
{
type: EAnnotationType.SVGTextAnnotation,
options: {
text: "Create SciChart charts with JSON Objects",
textColor: appTheme.ForegroundColor,
x1: 0.5,
y1: 0.5,
yCoordShift: 0,
opacity: 0.33,
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
verticalAnchorPoint: EVerticalAnchorPoint.Center,
fontSize: 24,
fontWeight: "Bold",
},
},
],
// Add interaction (zooming, panning, tooltips)
modifiers: [
{
type: EChart2DModifierType.Rollover,
options: {
yAxisId: "y1",
rolloverLineStroke: appTheme.VividTeal,
},
},
{ type: EChart2DModifierType.MouseWheelZoom },
{ type: EChart2DModifierType.ZoomExtents },
],
});
};
This example demonstrates how to create a fully configured chart using the SciChart.js Builder API in a JavaScript environment. It utilizes a comprehensive JSON configuration to set up axes, series, annotations, and interaction modifiers, showcasing the high-performance capabilities of SciChart.js.
The chart is constructed by defining its components with JSON objects. For example, a numeric X-Axis is configured with custom text labels and two numeric Y-Axes are set up for different data scales, as highlighted in the Adding Multiple Axis tutorial. Series such as a Spline Mountain Series with a custom gradient fill—demonstrated in the JavaScript Spline Mountain Area Chart example—and a Bubble Series with custom point markers—see Custom Pointmarkers documentation—illustrate the advanced visual capabilities available. The asynchronous initialization process follows the guidelines described in Getting Started with SciChart JS, ensuring optimal performance and seamless rendering.
This implementation supports real-time updates and interactivity through features such as rollover, mouse wheel zoom, and zoom extents modifiers, enhancing the user experience. Annotations are flexibly positioned using both data and relative coordinate modes, as detailed in the Adding Annotations tutorial. The JSON-based configuration approach makes it simple to customize and extend the chart’s behavior.
While built with JavaScript, the same principles can be applied across different frameworks. The Builder API’s JSON configuration allows for clear separation of chart setup from rendering logic, a best practice that promotes maintainability and performance optimization. Developers are encouraged to refer to the Intro to the Builder API for an in-depth understanding of these techniques and to review Performance Optimisation of JavaScript Applications & Charts for strategies aimed at ensuring efficient chart rendering.



