Demonstrates how to create a rotated JavaScript Chart with vertical X-Axis using SciChart.js, High Performance JavaScript Charts
import {
EAxisAlignment,
ECoordinateMode,
EHorizontalAnchorPoint,
EllipsePointMarker,
EMultiLineAlignment,
EVerticalAnchorPoint,
EWrapTo,
FastLineRenderableSeries,
MouseWheelZoomModifier,
NativeTextAnnotation,
NumberRange,
NumericAxis,
SciChartSurface,
XyDataSeries,
ZoomExtentsModifier,
ZoomPanModifier,
} from "scichart";
import { appTheme } from "./appTheme";
import { RandomWalkGenerator } from "./RandomWalkGenerator";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
// Setting an XAxis on the Left or Right
// and YAxis on the Top or Bottom
// causes the chart and series to be rotated vertically
sciChartSurface.xAxes.add(
new NumericAxis(wasmContext, {
axisTitle: "X Axis with Alignment = Left",
axisAlignment: EAxisAlignment.Left,
growBy: new NumberRange(0.15, 0.05),
})
);
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
axisTitle: "Y Axis with Alignment = Bottom",
axisAlignment: EAxisAlignment.Bottom,
growBy: new NumberRange(0.1, 0.1),
})
);
// Generate some data. xValues/yValues are just arrays of numbers
const generator = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(100);
const xValues = generator.xValues;
const yValues = generator.yValues;
// Add a line series to the chart. This will be drawn vertically.
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
pointMarker: new EllipsePointMarker(wasmContext, {
width: 9,
height: 9,
fill: appTheme.ForegroundColor,
stroke: appTheme.VividOrange,
}),
strokeThickness: 5,
stroke: appTheme.VividOrange,
})
);
// Add title / instructions
sciChartSurface.annotations.add(
new NativeTextAnnotation({
x1: 0.02,
y1: 0.02,
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
verticalAnchorPoint: EVerticalAnchorPoint.Top,
fontSize: 16,
opacity: 0.77,
textColor: appTheme.ForegroundColor,
text: "To rotate a chart in SciChart.js, set XAxis.alignment = Left/Right and YAxis.alignment=Top/Bottom",
wrapTo: EWrapTo.ViewRect,
multiLineAlignment: EMultiLineAlignment.Left,
})
);
// Add some interactivity modifiers
sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
sciChartSurface.zoomExtents();
return { wasmContext, sciChartSurface };
};
This example demonstrates how to create a rotated chart in JavaScript using SciChart.js. The chart can be rotated 90-degrees and rendered vertically with series drawn top to bottom by configuring the xAxis.axisAlignment = EAxisAlignment.Left and the yAxis.axisAlignment = EAxisAlignment.Bottom. This approach enables users to visualize data in a vertical orientation, offering a unique perspective for data analysis.
The implementation initializes a SciChartSurface asynchronously using the WebAssembly context provided by SciChart.js. NumericAxis can be configured with specific alignment properties to rotate the chart, as detailed in the Vertical Charts (Rotate, Transpose Axis) - SciChart guide. Data for the chart is generated using a random walk generator and visualized via a FastLineRenderableSeries which ensures high performance. Interactive features are added with modifiers such as ZoomPanModifier, ZoomExtentsModifier, and MouseWheelZoomModifier, following guidelines from the Tutorial 03 - Adding Zooming, Panning Behavior.
The chart takes advantage of SciChart.js capabilities such as real-time updates and advanced customizations. The use of the FastLineRenderableSeries significantly optimizes performance, a topic further explained in the Performance Tips & Tricks | JavaScript Chart Documentation resource. Additionally, custom point markers like the EllipsePointMarker provide visual enhancements, while native text annotations offer user guidance directly on the chart.
This implementation is tailored for JavaScript environments, illustrating best practices for asynchronous initialization and resource management. The example includes a cleanup function to properly dispose of the SciChartSurface, ensuring effective memory management as described in the Memory Best Practices documentation. Developers looking to integrate SciChart.js in JavaScript can refer to the Getting Started with SciChart JS guide to further streamline their development process. By following these guidelines, this example encapsulates critical aspects of performance optimization, interactivity setup, and architectural best practices in JavaScript.










