Demonstrates the Numeric, Category, Date and Logarithmic axis types available SciChart.js, High Performance JavaScript Charts
import {
CategoryAxis,
DateTimeNumericAxis,
EFillPaletteMode,
EStrokePaletteMode,
EAutoRange,
EAxisAlignment,
ELabelAlignment,
ENumericFormat,
FastColumnRenderableSeries,
FastLineRenderableSeries,
IFillPaletteProvider,
IStrokePaletteProvider,
IPointMetadata,
IRenderableSeries,
LogarithmicAxis,
MouseWheelZoomModifier,
NumericAxis,
NumberRange,
parseColorToUIntArgb,
SciChartSurface,
TextLabelProvider,
Thickness,
XyDataSeries,
ZoomExtentsModifier,
ZoomPanModifier,
} from "scichart";
import { appTheme } from "./appTheme";
import { TBinanceCandleData } from "./TBinanceCandleData";
const colorStrings = [
appTheme.VividSkyBlue,
appTheme.VividPink,
appTheme.MutedTeal,
appTheme.VividOrange,
appTheme.VividBlue,
];
const colors = colorStrings.map((c) => parseColorToUIntArgb(c + "AA"));
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface with Theme
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
const labelProvider = new TextLabelProvider({
labels: ["Bitcoin", "Ethereum", "XRP", "Cardano", "Dogecoin"],
});
// Category Axis - measures using index not value.
const xAxis = new CategoryAxis(wasmContext, { id: "XCategory", labelProvider });
xAxis.labelStyle.fontSize = 18;
xAxis.labelStyle.alignment = ELabelAlignment.Center;
xAxis.labelStyle.padding = new Thickness(2, 1, 2, 1);
// Allow rotated labels to overlap
xAxis.axisRenderer.hideOverlappingLabels = false;
// Keep first and last labels aligned to their ticks
xAxis.axisRenderer.keepLabelsWithinAxis = false;
xAxis.axisTitle = ["Top 5 Coins - Category Axis", "Custom labels using TextLabelProvider"];
xAxis.axisTitleStyle.fontSize = 14;
sciChartSurface.xAxes.add(xAxis);
// Numeric Y-Axis. measures using value
const yAxis = new NumericAxis(wasmContext, {
id: "YNumeric",
autoRange: EAutoRange.Always,
labelPrefix: "$",
labelPostfix: "B",
labelPrecision: 0,
axisAlignment: EAxisAlignment.Left,
labelStyle: { fontSize: 14 },
});
// Pass array to axisTitle to make it multiline
yAxis.axisTitle = ["Market Cap - Numeric Axis", "formatting using prefix and postfix"];
yAxis.axisTitleStyle.fontSize = 14;
sciChartSurface.yAxes.add(yAxis);
const columnSeries = new FastColumnRenderableSeries(wasmContext, {
strokeThickness: 0,
dataPointWidth: 0.5,
paletteProvider: new AxisTypesPaletteProvider(),
xAxisId: xAxis.id,
yAxisId: yAxis.id,
});
sciChartSurface.renderableSeries.add(columnSeries);
const dataSeries = new XyDataSeries(wasmContext);
dataSeries.appendRange([0, 1, 2, 3, 4], [380.9, 162.1, 23.87, 14.56, 8.372]);
columnSeries.dataSeries = dataSeries;
const endDate = new Date(2022, 10, 5);
const startTime = endDate.getTime() / 1000 - 500 * 7 * 24 * 60 * 60;
const dateXAxis = new DateTimeNumericAxis(wasmContext, {
axisAlignment: EAxisAlignment.Top,
id: "XDate",
labelStyle: { fontSize: 18 },
axisTitle: ["Date Axis", "Auto formats based on the date range"],
axisTitleStyle: { fontSize: 18 },
visibleRangeLimit: new NumberRange(startTime, endDate.getTime() / 1000),
});
sciChartSurface.xAxes.add(dateXAxis);
// Logarithmic Y Axis - measures on log scale using value
const logYAxis = new LogarithmicAxis(wasmContext, {
id: "YLog",
logBase: 2,
labelFormat: ENumericFormat.SignificantFigures,
labelPrefix: "$",
axisAlignment: EAxisAlignment.Right,
labelStyle: { fontSize: 14 },
axisTitle: ["Price - Logarithmic Axis", "base 2, labelFormat: SignificantFigures"],
axisTitleStyle: { fontSize: 14 },
});
sciChartSurface.yAxes.add(logYAxis);
const symbols = ["BTCUSDT", "ETHUSDT", "XRPUSDT", "ADAUSDT", "DOGEUSDT"];
for (let index = 0; index < symbols.length; index++) {
const symbol = symbols[index];
const priceDataSeries = new XyDataSeries(wasmContext, { dataSeriesName: symbol });
const series = new FastLineRenderableSeries(wasmContext, {
id: symbol,
strokeThickness: 3,
xAxisId: dateXAxis.id,
yAxisId: logYAxis.id,
stroke: colorStrings[index],
dataSeries: priceDataSeries,
});
sciChartSurface.renderableSeries.add(series);
(async () => {
const response = await fetch(
`api/get-binance-candles?interval=1w&symbol=${symbol}&limit=500&endTime=${endDate}`
);
const data: TBinanceCandleData = await response.json();
priceDataSeries.appendRange(data.xValues, data.closeValues);
sciChartSurface.zoomExtents();
})();
}
sciChartSurface.chartModifiers.add(
new ZoomPanModifier({ includedXAxisIds: [dateXAxis.id], includedYAxisIds: [logYAxis.id] }),
new MouseWheelZoomModifier({ includedXAxisIds: [dateXAxis.id], includedYAxisIds: [logYAxis.id] }),
new ZoomExtentsModifier()
);
return { sciChartSurface, wasmContext };
};
class AxisTypesPaletteProvider implements IStrokePaletteProvider, IFillPaletteProvider {
public readonly strokePaletteMode = EStrokePaletteMode.SOLID;
public readonly fillPaletteMode = EFillPaletteMode.SOLID;
// 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 {
return colors[xValue];
}
public overrideStrokeArgb(
xValue: number,
yValue: number,
index: number,
opacity?: number,
metadata?: IPointMetadata
): number {
return undefined;
}
}
This example demonstrates the integration of SciChart.js within a React application using the <SciChartReact/> component. It showcases multiple axis types including CategoryAxis, NumericAxis, DateTimeNumericAxis and LogarithmicAxis. Custom axis labeling is implemented using the TextLabelProvider to display arbitrary string text labels, and a custom palette provider (AxisTypesPaletteProvider) is used for dynamic coloring of columns.
The chart is initialized in a React component via the <SciChartReact/> element, which encapsulates the setup within React lifecycle methods. All axis types are instantiated directly and configured with specific properties such as custom font sizes, alignments, and prefixes/postfixes for labels. Developers can refer to the SciChart.js Axis Documentation for a detailed explanation on the various axis types. Custom axis labeling is achieved through the TextLabelProvider as explained in the Axis LabelProviders Documentation. Additionally, the example utilizes a custom palette provider by extending IStrokePaletteProvider and IFillPaletteProvider for dynamic series styling.
The example includes several advanced features and capabilities such as real-time data updates that fetch and render data asynchronously, interactive chart behaviors with zoom, pan and mouse wheel actions, and multi-axis configurations that allow parallel display of datasets with different scales. Real-time updates are managed in a React-friendly way, consolidating state management with SciChart.js’s high-performance WebGL rendering, ensuring smooth updates even with large datasets, as detailed in the Adding Realtime Updates guide.
The integration with React follows best practices by utilizing the <SciChartReact/> component which ensures proper initialization and cleanup through React’s lifecycle, as discussed in Creating a SciChart React Component from the Ground Up. Furthermore, the example employs performance optimization techniques by leveraging WebGL rendering capabilities, as outlined in the Performance Tips & Tricks documentation. Developers building production-grade charting solutions in React can benefit from the modular design of this example which promotes reusability and efficient resource management, making it easier to integrate complex chart configurations within larger applications. For further insights into the integration of SciChart.js with React, check out React Charts with SciChart.js: Introducing “SciChart React”.










