Demonstrates how to create a JavaScript Chart with Logarithmic axis using SciChart.js, High Performance JavaScript Charts
import {
ENumericFormat,
EllipsePointMarker,
FastLineRenderableSeries,
LegendModifier,
LogarithmicAxis,
MouseWheelZoomModifier,
NumericAxis,
RubberBandXyZoomModifier,
SciChartSurface,
SweepAnimation,
XyDataSeries,
Thickness,
ZoomExtentsModifier,
} from "scichart";
import { appTheme, createChartTheme } from "./appTheme";
import { ExampleDataProvider } from "./ExampleDataProvider";
const Y_AXIS_LINEAR_ID = "Y_AXIS_LINEAR_ID";
const X_AXIS_LINEAR_ID = "X_AXIS_LINEAR_ID";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: createChartTheme({
majorGridLineBrush: appTheme.MutedSkyBlue + "55",
minorGridLineBrush: appTheme.MutedSkyBlue + "22",
}),
title: "Logarithmic X & Y Axis",
titleStyle: {
fontSize: 20,
fontWeight: "bold",
placeWithinChart: true,
color: appTheme.ForegroundColor + "C4",
padding: new Thickness(10, 0, 4),
},
});
// Create an X and Y Axis
const xAxisLogarithmic = new LogarithmicAxis(wasmContext, {
logBase: 10,
labelFormat: ENumericFormat.Scientific,
labelPrecision: 2,
minorsPerMajor: 10,
useNativeText: false,
});
sciChartSurface.xAxes.add(xAxisLogarithmic);
// The LogarithmicAxis will apply logarithmic scaling and labelling to your data.
// Simply replace a NumericAxis for a LogarithmicAxis on X or Y to apply this scaling
// Note options logBase, labelFormat which lets you specify exponent on labels
const yAxisLogarithmic = new LogarithmicAxis(wasmContext, {
logBase: 10,
labelFormat: ENumericFormat.Scientific,
labelPrecision: 2,
minorsPerMajor: 10,
useNativeText: false,
});
sciChartSurface.yAxes.add(yAxisLogarithmic);
const xAxisLinear = new NumericAxis(wasmContext, {
labelFormat: ENumericFormat.Decimal,
labelPrecision: 2,
isVisible: false,
id: X_AXIS_LINEAR_ID,
});
sciChartSurface.xAxes.add(xAxisLinear);
const yAxisLinear = new NumericAxis(wasmContext, {
labelFormat: ENumericFormat.Decimal,
labelPrecision: 2,
isVisible: false,
id: Y_AXIS_LINEAR_ID,
});
sciChartSurface.yAxes.add(yAxisLinear);
// Create some data
const data0 = ExampleDataProvider.getExponentialCurve(2, 100);
const data1 = ExampleDataProvider.getExponentialCurve(2.2, 100);
const data2 = ExampleDataProvider.getExponentialCurve(2.4, 100);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data0.xValues,
yValues: data0.yValues,
dataSeriesName: "y = x<sup>2</sup>",
}),
stroke: appTheme.VividSkyBlue,
strokeThickness: 3,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
fill: appTheme.VividSkyBlue,
strokeThickness: 0,
}),
animation: new SweepAnimation({ duration: 800, delay: 0 }),
})
);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data1.xValues,
yValues: data1.yValues,
dataSeriesName: "y = x<sup>2.2</sup>",
}),
stroke: appTheme.VividPink,
strokeThickness: 3,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
fill: appTheme.VividPink,
strokeThickness: 0,
}),
animation: new SweepAnimation({ duration: 800, delay: 0 }),
})
);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data2.xValues,
yValues: data2.yValues,
dataSeriesName: "y = x<sup>2.4</sup>",
}),
stroke: appTheme.VividOrange,
strokeThickness: 3,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
fill: appTheme.VividOrange,
strokeThickness: 0,
}),
animation: new SweepAnimation({ duration: 800, delay: 0 }),
})
);
// Add some interactivity modifiers
sciChartSurface.chartModifiers.add(
new RubberBandXyZoomModifier(),
new MouseWheelZoomModifier(),
new ZoomExtentsModifier(),
new LegendModifier({ showCheckboxes: false })
);
sciChartSurface.zoomExtents();
return {
sciChartSurface,
wasmContext,
yAxisLogarithmic,
yAxisLinear,
xAxisLinear,
xAxisLogarithmic,
};
};
This example demonstrates how to create a high-performance SciChart.js chart featuring LogarithmicAxis configuration using JavaScript. The implementation plots exponential curves on both the X and Y axes while allowing runtime toggling between logarithmic and linear axis configurations.
The chart is asynchronously initialized using SciChartSurface.create, which loads the WebAssembly context and applies a custom theme for styling. Two LogarithmicAxis are configured with properties such as logBase, labelFormat, and labelPrecision; details of these configurations can be found in the Logarithmic Axis Documentation. Renderable series are created using FastLineRenderableSeries in combination with XyDataSeries to plot exponential curves, and each series is enhanced with a SweepAnimation for smooth visual transitions.
The example showcases real-time update capabilities by enabling users to seamlessly switch between logarithmic and linear axis states at runtime. It integrates interactive modifiers such as RubberBandXyZoomModifier and MouseWheelZoomModifier to facilitate intuitive zooming and panning. Additionally, the application of custom theming—including grid line brushes and title styling—illustrates how visual aspects can be tailored to meet specific design requirements. For additional context on building high-performance charts, refer to Getting Started with SciChart JS.
Despite being a JavaScript example, the implementation follows best practices in asynchronous initialization, interactive behavior, and dynamic configuration. The runtime axis switching is handled by toggling the visibility and primary status of the axes, ensuring that the currently active axes are correctly bound to the series data; this approach is in line with the guidance provided in the SciChart.js Documentation. Developers looking to optimize performance and customize their charts further can benefit from exploring the available Performance Tips & Tricks provided by SciChart.js.









