Demonstrates how to use the Builder Api with Custom Types using SciChart.js, High Performance JavaScript Charts
import {
build2DChart,
buildSeries,
registerType,
registerAllTypes,
ESeriesType,
EAxisType,
ELineDrawMode,
XyDataSeries,
EAnimationType,
EAxisAlignment,
EPaletteProviderType,
EFillPaletteMode,
EStrokePaletteMode,
IFillPaletteProvider,
IStrokePaletteProvider,
IRenderableSeries,
EBaseType,
parseColorToUIntArgb,
NumberRange,
EAnnotationType,
ECoordinateMode,
EHorizontalAnchorPoint,
EVerticalAnchorPoint,
} 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();
// Define a custom PaletteProvider
export class ExampleMountainPaletteProvider implements IStrokePaletteProvider, IFillPaletteProvider {
public static Name = "ExampleMountain";
public readonly strokePaletteMode = EStrokePaletteMode.SOLID;
public readonly fillPaletteMode = EFillPaletteMode.SOLID;
private readonly palettedStroke: number;
private readonly palettedFill: number;
private readonly options: { stroke: string; fill: string };
constructor(options: { stroke: string; fill: string }) {
this.options = options;
this.palettedStroke = parseColorToUIntArgb(options.stroke);
this.palettedFill = parseColorToUIntArgb(options.fill);
}
// tslint:disable-next-line:no-empty
public onAttached(parentSeries: IRenderableSeries): void {}
// tslint:disable-next-line:no-empty
public onDetached(): void {}
public overrideFillArgb(xValue: number, yValue: number, index: number): number {
if (yValue > 0.5 && yValue < 0.75) {
return this.palettedFill;
} else {
return undefined;
}
}
public overrideStrokeArgb(xValue: number, yValue: number, index: number): number {
if (yValue > 0.5 && yValue < 0.75) {
return this.palettedStroke;
} else {
return undefined;
}
}
// Add a toJSON method so this can be serialized.
// @ts-ignore
public toJSON() {
return {
type: EPaletteProviderType.Custom,
customType: ExampleMountainPaletteProvider.Name,
options: this.options,
};
}
}
// Register it for use by the builder api
registerType(
EBaseType.PaletteProvider,
ExampleMountainPaletteProvider.Name,
(options: { stroke: string; fill: string }) => new ExampleMountainPaletteProvider(options)
);
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Build the surface
const { sciChartSurface, wasmContext } = await build2DChart(rootElement, {
surface: { theme: appTheme.SciChartJsTheme },
yAxes: {
type: EAxisType.NumericAxis,
options: { axisAlignment: EAxisAlignment.Left, visibleRange: new NumberRange(0, 1) },
},
// Add annotations
annotations: [
{
type: EAnnotationType.SVGTextAnnotation,
options: {
text: "Builder API Demo",
x1: 0.5,
y1: 0.5,
opacity: 0.33,
yCoordShift: -26,
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",
x1: 0.5,
y1: 0.5,
yCoordShift: 26,
opacity: 0.33,
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
verticalAnchorPoint: EVerticalAnchorPoint.Center,
fontSize: 24,
fontWeight: "Bold",
},
},
],
});
// Build the series.
// By doing this separately we can easily get the reference to the series so we can add generated data to it
const [mountainSeries] = buildSeries(wasmContext, {
type: ESeriesType.MountainSeries,
options: {
paletteProvider: {
type: EPaletteProviderType.Custom,
customType: ExampleMountainPaletteProvider.Name,
options: { stroke: appTheme.MutedRed, fill: appTheme.VividOrange },
},
fillLinearGradient: {
startPoint: { x: 0, y: 0 },
endPoint: { x: 0, y: 1 },
gradientStops: [
{ color: appTheme.VividBlue, offset: 0 },
{ color: "Transparent", offset: 1 },
],
},
stroke: appTheme.PaleSkyBlue,
strokeThickness: 3,
drawNaNAs: ELineDrawMode.PolyLine,
animation: { type: EAnimationType.Scale, options: { ease: "cubic" } },
},
});
// Create a dataSeries the normal way
const dataSeries = new XyDataSeries(wasmContext);
// Generate data
const POINTS = 1000;
const STEP = (3 * Math.PI) / POINTS;
for (let i = 0; i <= 1000; i++) {
let y = Math.abs(Math.sin(i * STEP));
if (y < 0.2) {
y = NaN;
}
dataSeries.append(i, y);
}
mountainSeries.dataSeries = dataSeries;
// Since we built the series separately, we have to manually add it to the surface
sciChartSurface.renderableSeries.add(mountainSeries);
return { sciChartSurface, wasmContext };
};
This example demonstrates how to extend the default functionalities of SciChart.js using JavaScript. It shows how to create a custom palette provider that dynamically overrides the fill and stroke of a Mountain Series chart based on specific data thresholds, while leveraging JSON configuration for flexible chart construction with the Builder API.
The implementation uses the SciChart.js Builder API to build a 2D chart using a JSON configuration. In the file drawExample.ts, the chart is configured with a numeric Y-Axis, custom annotations, and a Mountain Series whose look is dynamically altered by the custom palette provider (ExampleMountainPaletteProvider). This provider overrides the default rendering behavior for data points by checking if the Y value falls within a set range before applying custom colors. For a deeper dive into this approach, check out the Custom Types with Builder API - JavaScript Chart - SciChart documentation. Additionally, the use of JSON in defining chart properties is detailed in the Serialization and Deserialization of Charts - SciChart guide.
The example incorporates several advanced features, including real-time data updates, dynamic handling of NaN values, and gradient fill techniques. It leverages WebGL rendering for high-performance updates and demonstrates advanced customization by overriding fill and stroke colors via a custom palette provider. These optimizations align with best practices discussed in the Performance Tips & Tricks | JavaScript Chart Documentation.
For developers working purely in JavaScript, this example provides a clear blueprint for integrating SciChart.js into any web project without relying on frameworks like React or Angular. The approach emphasizes the use of the Builder API for streamlined chart creation and customization, as outlined in the JavaScript Builder API Documentation. By using JSON configuration and custom types, the example encourages modularity and adherence to performance optimizations, making it easy to manage dynamic data and real-time updates. This serves as a solid foundation for building high-performance charts with customized visual behavior.



