Shows how you can add Data Labels to a chart using SciChart.js
import { appTheme } from "./appTheme";
import { ExampleDataProvider } from "./ExampleDataProvider";
import {
DataLabelProvider,
EllipsePointMarker,
ENumericFormat,
EVerticalTextPosition,
EWrapTo,
EXyDirection,
FastColumnRenderableSeries,
FastLineRenderableSeries,
formatNumber,
IPointMetadata,
MouseWheelZoomModifier,
NativeTextAnnotation,
NumberRange,
NumericAxis,
parseColorToUIntArgb,
SciChartSurface,
SplineLineRenderableSeries,
Thickness,
XyDataSeries,
ZoomExtentsModifier,
ZoomPanModifier,
} from "scichart";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
// Add an X, Y Axis
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { visibleRangeLimit: new NumberRange(0, 20) }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.05, 0.05) }));
// normal labels
const data1 = ExampleDataProvider.getSpectrumData(0, 20, 5, 1, 0.01);
const colSeries = new FastColumnRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, data1),
fill: appTheme.VividOrange + "33",
stroke: appTheme.VividOrange,
dataPointWidth: 0.7,
strokeThickness: 1,
dataLabels: {
// To enable datalabels, set fontFamily and size
style: { fontFamily: "Arial", fontSize: 16, padding: new Thickness(5, 0, 5, 0) },
color: appTheme.VividOrange,
// Normal label format and precision options are supported
precision: 2,
},
});
const highCol = parseColorToUIntArgb(appTheme.VividGreen);
const lowCol = parseColorToUIntArgb(appTheme.VividOrange);
(colSeries.dataLabelProvider as DataLabelProvider).getColor = (state, text) => {
if (state.yVal() > 0) return highCol;
else return lowCol;
};
sciChartSurface.renderableSeries.add(colSeries);
const labels = ["Data", "Labels", "can", "come", "from", "values", "in", "metadata"];
sciChartSurface.renderableSeries.add(
new SplineLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data1.xValues,
yValues: data1.yValues.map((y) => y * 0.8 + 4),
metadata: data1.xValues.map(
(x, i) => ({ isSelected: false, text: labels[(i - 1) / 2] } as IPointMetadata)
),
}),
stroke: appTheme.VividSkyBlue,
strokeThickness: 3,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
fill: appTheme.ForegroundColor,
strokeThickness: 0,
}),
dataLabels: {
style: { fontFamily: "Arial", fontSize: 16 },
color: appTheme.ForegroundColor,
// @ts-ignore
metaDataSelector: (md) => md.text,
},
})
);
sciChartSurface.annotations.add(
new NativeTextAnnotation({
x1: 1,
y1: 10,
text: "Series with 1000 points, using custom getText function to only show labels for peaks, with x and y",
textColor: appTheme.VividGreen,
wrapTo: EWrapTo.ViewRect,
})
);
// Custom getText
const data2 = ExampleDataProvider.getSpectrumData(10, 1000, 10, 100, 0.02);
const series = new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data2.xValues.map((x) => x / 50),
yValues: data2.yValues.map((y) => y * 0.3 + 8),
}),
stroke: appTheme.VividGreen,
strokeThickness: 3,
dataLabels: {
style: { fontFamily: "Arial", fontSize: 14, padding: new Thickness(0, 0, 3, 0) },
color: appTheme.ForegroundColor,
aboveBelow: false,
verticalTextPosition: EVerticalTextPosition.Above,
},
});
(series.dataLabelProvider as DataLabelProvider).getText = (state) => {
const i = state.index;
if (
i > state.indexStart &&
i < state.indexEnd &&
state.yVal() > state.yVal(i - 1) &&
state.yVal() > state.yVal(i + 1)
) {
return `X: ${formatNumber(state.xVal(), ENumericFormat.Decimal, 2)}\nY: ${formatNumber(
state.yVal(),
ENumericFormat.Decimal,
3
)}`;
}
return undefined;
};
sciChartSurface.renderableSeries.add(series);
sciChartSurface.annotations.add(
new NativeTextAnnotation({
x1: 1,
y1: 14,
text: "Series with 200 points. Using pointGapThreshold = 1 to show labels when zoomed in enough for there to be space for them",
textColor: appTheme.VividPink,
wrapTo: EWrapTo.ViewRect,
})
);
// Show labels when zoomed in
const data3 = ExampleDataProvider.getSpectrumData(0, 200, 10, 20, 0.02);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data3.xValues.map((x) => x / 10),
yValues: data3.yValues.map((y) => y * 0.3 + 12),
}),
stroke: appTheme.VividPink,
strokeThickness: 3,
dataLabels: {
style: { fontFamily: "Arial", fontSize: 12 },
color: appTheme.ForegroundColor,
pointGapThreshold: 1,
},
})
);
// Optional: Add some interactivity modifiers
sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier({ xyDirection: EXyDirection.XDirection }));
sciChartSurface.zoomExtents();
return { sciChartSurface, wasmContext };
};
This example demonstrates how to create a chart with advanced Data Labels customization using SciChart.js in a React application. The example shows how to add data labels to various series types such as column, spline, and line series, and illustrates how labels can be dynamically configured based on data values and metadata.
The chart is initialized through the <SciChartReact/> component, which leverages an initChart callback (implemented in the drawExample function) to set up axes, series, and custom data labels. The example employs the DataLabelProvider to define label styles and dynamic colors based on data values, and uses custom callback functions to format text (see Custom DataLabel Formatting with getText() - SciChart and Adding DataLabels to a Chart Series - SciChart for detailed documentation). The configuration of the chart follows the practices highlighted in Tutorial 02 - Creating a Chart with scichart-react, ensuring a clear separation of chart logic and React component structure.
The example showcases several advanced features:
These capabilities are enhanced by interactive annotations and chart modifiers such as zooming and panning, contributing to a highly interactive experience.
The integration leverages the <SciChartReact/> component to seamlessly embed high performance SciChart.js charts in a React application. Developers can follow the best practices outlined in React Charts with SciChart.js: Introducing “SciChart React” and Tutorial 01 - Setting up a project with scichart-react and config object for guidance on setting up chart components and optimizing performance. The use of efficient data series and interactive modifiers ensures the chart remains responsive even with large datasets, making it an excellent reference for building robust data visualization applications.










