Demonstrates how to color areas of the chart surface using background Annotations using SciChart.js, High Performance JavaScript Charts
import { appTheme } from "./appTheme";
import { happinessData } from "./data";
import {
SciChartSurface,
NumericAxis,
NumberRange,
ZoomPanModifier,
BoxAnnotation,
EAnnotationLayer,
EAxisAlignment,
MouseWheelZoomModifier,
FastBubbleRenderableSeries,
XyzDataSeries,
IPointMetadata,
ZoomExtentsModifier,
EllipsePointMarker,
CursorModifier,
LogarithmicAxis,
ENumericFormat,
DefaultPaletteProvider,
TPointMarkerArgb,
parseColorToUIntArgb,
EHorizontalTextPosition,
EVerticalTextPosition,
SeriesInfo,
XyzSeriesInfo,
SweepAnimation,
ManualLegend,
} from "scichart";
class ContinentPaletteProvider extends DefaultPaletteProvider {
Europe = parseColorToUIntArgb(appTheme.VividBlue);
Asia = parseColorToUIntArgb(appTheme.VividPurple);
NorthAmerica = parseColorToUIntArgb(appTheme.VividPink);
Oceania = parseColorToUIntArgb(appTheme.VividTeal);
SouthAmerica = parseColorToUIntArgb(appTheme.VividGreen);
Africa = parseColorToUIntArgb(appTheme.VividOrange);
override overridePointMarkerArgb(
xValue: number,
yValue: number,
index: number,
opacity?: number,
metadata?: IPointMetadata
): TPointMarkerArgb {
let fill: number;
// @ts-ignore
switch (metadata.continent) {
case "Europe":
fill = this.Europe;
break;
case "Asia":
fill = this.Asia;
break;
case "North America":
fill = this.NorthAmerica;
break;
case "Oceania":
fill = this.Oceania;
break;
case "South America":
fill = this.SouthAmerica;
break;
case "Africa":
fill = this.Africa;
break;
default:
break;
}
return { fill, stroke: undefined };
}
}
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
title: "Happiness vs GDP",
titleStyle: { fontSize: 20 },
});
// Create an XAxis and YAxis
const xAxis = new LogarithmicAxis(wasmContext, {
growBy: new NumberRange(0.1, 0.1),
labelPrefix: "$",
labelFormat: ENumericFormat.SignificantFigures,
labelPrecision: 2,
cursorLabelFormat: ENumericFormat.Decimal,
logBase: 10,
drawMinorGridLines: false,
axisTitle: "GDP per Capita",
axisTitleStyle: { fontSize: 16 },
});
sciChartSurface.xAxes.add(xAxis);
const yAxis = new NumericAxis(wasmContext, {
growBy: new NumberRange(0.1, 0.1),
axisAlignment: EAxisAlignment.Left,
drawMinorGridLines: false,
axisTitle: "Happiness",
axisTitleStyle: { fontSize: 16 },
});
sciChartSurface.yAxes.add(yAxis);
// Optional: Add some interactivity modifiers
sciChartSurface.chartModifiers.add(
new ZoomPanModifier({ enableZoom: true }),
new MouseWheelZoomModifier(),
new ZoomExtentsModifier(),
new CursorModifier({
showTooltip: true,
hitTestRadius: 1,
tooltipContainerBackground: appTheme.MutedRed,
showAxisLabels: false,
showXLine: false,
showYLine: false,
tooltipDataTemplate: (seriesInfos: SeriesInfo[], tooltipTitle: string) => {
const valuesWithLabels: string[] = [];
const xyzSeriesInfo = seriesInfos[0] as XyzSeriesInfo;
if (xyzSeriesInfo?.isHit) {
// @ts-ignore
valuesWithLabels.push(`${xyzSeriesInfo.pointMetadata.name}`);
valuesWithLabels.push(`GDP: ${xyzSeriesInfo.formattedXValue}`);
valuesWithLabels.push(`Happiness: ${xyzSeriesInfo.formattedYValue}`);
valuesWithLabels.push(`Population: ${xyzSeriesInfo.formattedZValue}`);
}
return valuesWithLabels;
},
})
);
// These boxes are set up so that x1,y1 is the outer corner and x2,y2 is the centre of the data
const x2 = 10000;
const y2 = 5;
const box1 = new BoxAnnotation({
annotationLayer: EAnnotationLayer.Background,
fill: appTheme.PaleBlue,
strokeThickness: 0,
x1: -10,
x2,
y1: 10,
y2,
});
const box2 = new BoxAnnotation({
annotationLayer: EAnnotationLayer.Background,
fill: appTheme.PalePurple,
strokeThickness: 0,
x1: 10,
x2,
y1: 10,
y2,
});
const box3 = new BoxAnnotation({
annotationLayer: EAnnotationLayer.Background,
fill: appTheme.PalePink,
strokeThickness: 0,
x1: -10,
x2,
y1: -10,
y2,
});
const box4 = new BoxAnnotation({
annotationLayer: EAnnotationLayer.Background,
fill: appTheme.PaleTeal,
strokeThickness: 0,
x1: 10,
x2,
y1: -10,
y2,
});
// update the outer corners of each box before the chart draws so that they always fill the plane
sciChartSurface.preRender.subscribe((data) => {
box1.x1 = xAxis.visibleRange.min;
box2.x1 = xAxis.visibleRange.max;
box3.x1 = xAxis.visibleRange.min;
box4.x1 = xAxis.visibleRange.max;
box1.y1 = yAxis.visibleRange.min;
box2.y1 = yAxis.visibleRange.min;
box3.y1 = yAxis.visibleRange.max;
box4.y1 = yAxis.visibleRange.max;
});
sciChartSurface.annotations.add(box1, box2, box3, box4);
const xValues: number[] = [];
const yValues: number[] = [];
const zValues: number[] = [];
const metadata: any[] = [];
for (const item of happinessData) {
xValues.push(parseFloat(item.GDP));
yValues.push(parseFloat(item.Happiness));
zValues.push((Math.log2(parseInt(item.Population)) - 18) * 5);
//console.log(item.Entity, parseFloat(item.GDP), parseFloat(item.Happiness));
metadata.push({ isSelected: false, name: item.Entity, continent: item.Continent });
}
const dataSeries = new XyzDataSeries(wasmContext, { xValues, yValues, zValues, metadata });
const series = new FastBubbleRenderableSeries(wasmContext, {
dataSeries,
paletteProvider: new ContinentPaletteProvider(),
pointMarker: new EllipsePointMarker(wasmContext, {
width: 64,
height: 64,
opacity: 0.6,
}),
dataLabels: {
color: "#000000C0",
style: {
fontFamily: "Arial",
fontSize: 14,
},
horizontalTextPosition: EHorizontalTextPosition.Center,
verticalTextPosition: EVerticalTextPosition.Below,
metaDataSelector: (metadata) => (metadata as any).name,
},
animation: new SweepAnimation({ duration: 2000 }),
});
sciChartSurface.renderableSeries.add(series);
const legend = new ManualLegend(
{
textColor: "black",
backgroundColor: "#E0E0E077",
items: [
{
name: "Bubble size represents population",
color: "transparent",
id: "pop",
checked: false,
showMarker: false,
},
{
name: "Bubble color indicates continent",
color: "transparent",
id: "col",
checked: false,
showMarker: false,
},
{ name: "Europe", color: appTheme.VividBlue, id: "Europe", checked: false },
{ name: "Asia", color: appTheme.VividPurple, id: "Asia", checked: false },
{ name: "North America", color: appTheme.VividPink, id: "NorthAmerica", checked: false },
{ name: "South America", color: appTheme.VividGreen, id: "SouthAmerica", checked: false },
{ name: "Oceania", color: appTheme.VividBlue, id: "VividTeal", checked: false },
{ name: "Africa", color: appTheme.VividOrange, id: "Africa", checked: false },
],
},
sciChartSurface
);
sciChartSurface.zoomExtents();
return { sciChartSurface, wasmContext };
};
This example demonstrates a high-performance bubble chart implemented in React using SciChart.js. It visualizes data for GDP per capita versus happiness, with bubble sizes representing a logarithmic transformation of population data. The chart uses dynamic background annotations to create quadrant areas, offering a clear visual reference, and leverages the <SciChartReact/> component for seamless React integration. For more on React integration, refer to React Charts with SciChart.js and Setting up a project with scichart-react.
The chart is built by creating a SciChartSurface with a LogarithmicAxis for the X-axis and a NumericAxis for the Y-axis. Background annotations are implemented using multiple BoxAnnotations which update dynamically during the chart's preRender event to always fill the visible area. Furthermore, a custom palette provider named ContinentPaletteProvider is used to assign colors to bubbles based on continent information. Additionally, a legend is provided by the LegendModifier and Data Labels are enabled via the dataLabels property on FastBubbleRenderableSeries. The implementation details are compliant with the SciChart.js Annotations API Documentation and carefully utilizes event handling to update annotations just before rendering, ensuring dynamic and responsive chart behavior.
The example offers advanced interactive features such as zooming, panning, and custom tooltips via the CursorModifier. Data is bound using the XyzDataSeries, which maps GDP, happiness, and a logarithmically transformed population value to the chart. Further, the renderable series incorporates a SweepAnimation to smoothly animate the bubble data, and the custom palette provider dynamically colors bubbles by continent. These design choices showcase how real-time data updates and dynamic visualizations can be achieved, similar to the techniques outlined in the Custom PaletteProvider example and JavaScript Bubble Chart examples.
The integration leverages the <SciChartReact/> component to seamlessly render the chart within a React application. Developers are encouraged to follow best practices for event handling and performance optimization, taking advantage of SciChart.js's efficient WebGL rendering. The use of dynamic background annotations and interactive modifiers not only improves usability but also demonstrates advanced configuration techniques. For additional guidance on interactive features such as tooltips and optimized performance, explore the CursorModifier documentation and Performance Optimisation Tips.








































