Demonstrates how to use Rotation and Alignment of Axis Labels with SciChart.js, High Performance JavaScript Charts
import {
XyDataSeries,
NumericAxis,
WaveAnimation,
SciChartSurface,
GradientParams,
Point,
SplineMountainRenderableSeries,
ENumericFormat,
} from "scichart";
import { appTheme } from "./appTheme";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
// Add an X Axis
const xAxis = new NumericAxis(wasmContext, {
axisTitle: "X Axis with Rotated Labels",
labelFormat: ENumericFormat.Date_DDMMYYYY,
labelStyle: {
fontSize: 16,
},
// Rotation is in degrees clockwise
rotation: 90,
// Turn up the number of major ticks (default is 10)
maxAutoTicks: 30,
// Turn off minor gridlines, since majors are now closer together
drawMinorGridLines: false,
});
sciChartSurface.xAxes.add(xAxis);
// Add a Y Axis
const yAxis = new NumericAxis(wasmContext, { axisTitle: "Y Axis", labelStyle: { fontSize: 16 } });
sciChartSurface.yAxes.add(yAxis);
// Generate some data
const startTime = new Date(2020, 0, 1).getTime() / 1000;
let y = 110;
const xValues: number[] = [];
const yValues: number[] = [];
for (let i = 0; i < 50; i++) {
const x = startTime + i * 24 * 60 * 60;
y = y + 10 * (Math.random() - 0.8);
xValues.push(x);
yValues.push(y);
}
// Add a Spline Mountain series
const mountainSeries = new SplineMountainRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
stroke: appTheme.VividSkyBlue,
strokeThickness: 3,
zeroLineY: 0.0,
fill: "rgba(176, 196, 222, 0.7)", // when a solid color is required, use fill
// when a gradient is required, use fillLinearGradient
fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
{ color: appTheme.VividTeal + "77", offset: 0 },
{ color: "Transparent", offset: 1 },
]),
animation: new WaveAnimation({ duration: 1000, fadeEffect: true, zeroLine: 0 }),
});
sciChartSurface.renderableSeries.add(mountainSeries);
return { sciChartSurface, wasmContext };
};
This example demonstrates how to customize and rotate axis labels using SciChart.js in a pure JavaScript setting. The main objective is to rotate the X-Axis labels by 90 degrees and apply custom date formatting, which allows more labels to be displayed without overlap when visualizing time series data.
The chart is asynchronously initialized by creating a SciChartSurface along with its WebAssembly context. A NumericAxis is configured for the X-Axis with properties such as axis.labelFormat (using the date format ENumericFormat.Date_DDMMYYYY), increased tick counts, and a axis.rotation value set to 90 degrees. Dynamic time series data is generated and rendered using a SplineMountainRenderableSeries that incorporates both gradient fills and a wave animation effect. Developers looking for advanced axis customization can refer to the Rotating Axis Labels documentation, while additional details about date formatting are available in The DateTimeNumericAxis guide.
The implementation highlights several advanced features, including real-time chart updates due to dynamic data generation, visually appealing gradient fills, and smooth transition animations provided by the wave animation effect. For more insights into how such spline mountain charts are constructed, you can review the JavaScript Spline Mountain Area Chart documentation.
In this JavaScript example, the asynchronous initialization not only enhances performance through WebAssembly integration but also ensures proper resource management. A destructor function is provided to clean up the SciChartSurface when the chart is no longer needed, aligning with best practices detailed in Getting Started with SciChart JS. Moreover, developers interested in configuring animation effects will benefit from the Animations API, which explains how to fine-tune parameters such as duration and fade effects for an optimal display.


